An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

![](https://github.com/NathanPB/Safer/workflows/Java%20CI%20with%20Gradle/badge.svg)

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)