https://github.com/nathanpb/safer
JVM Error Recovering library for stupid devs (like me) who can't write safe code
https://github.com/nathanpb/safer
error-handling jvm
Last synced: 10 months ago
JSON representation
JVM Error Recovering library for stupid devs (like me) who can't write safe code
- Host: GitHub
- URL: https://github.com/nathanpb/safer
- Owner: NathanPB
- License: mit
- Created: 2020-09-04T22:38:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-14T06:53:54.000Z (over 5 years ago)
- Last Synced: 2025-03-16T03:41:29.973Z (about 1 year ago)
- Topics: error-handling, jvm
- Language: Java
- Homepage:
- Size: 68.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

Safer is a little JVM error handling library made to catch errors in critical points of the program execution when they are not expected to happen.
# Whats the point
Safer is designed to prevent fatal errors in an application when you don't expect an Exception to be thrown.
It's designed to be used with [Mixin](https://github.com/SpongePowered/Mixin), a framework to make runtime bytecode injectons. Since it requires a lot of unsafe class casts, the Type Inference is damaged and a lot of different exceptions may appear and set the production on fire.
When to use Safer
- When you are implementing a critical code fragment
- When you are injecting a code fragment into another class' bytecode
When **not** to use Safer
- **When you are sure something will go wrong:** Safer is meant to be used as **the last resource** of recovering from a fatal error. If you know an error will be throw, ``try/catch`` it.
# Installation
build.gradle
```groovy
repositories {
...
maven { url 'https://jitpack.io' }
}
```
```groovy
dependencies {
implementation 'com.github.NathanPB:Safer:VERSION'
}
```
See the versions [in the releases page](https://github.com/NathanPB/Safer/releases)
# How to use
```java
public void unsafeCodeFragment() {
Safer.run(() -> {
// Critical and unsafe code fragment
})
}
```
```java
public String unsafeCodeFragment() {
return Safer.run("default value to return if something goes wrong", () -> {
// Critical and unsafe code fragment
})
}
```
If your default value is something a bit expensive, do it lazy
```java
public String unsafeCodeFragment() {
return Safer.runLazy(() -> factorial(12), () -> {
// Critical and unsafe code fragment
})
}
```
For more examples check [the tests files](https://github.com/NathanPB/Safer/tree/master/src/test/java)