@ehrelein/nothrow
Version:
Type-safe error handling
34 lines (26 loc) • 1.17 kB
Markdown
[](https://github.com/Ehrelein/nothrow?tab=readme-ov-file)
[](https://github.com/Ehrelein/nothrow?tab=readme-ov-file)
```typescript
let file = safeCall(readFileSync, "test.txt") // <-- calls any function
let wrapperReadFile = wrap(readFile) // <-- wraps any function
log(file.value) // <-- error, no value field in file yet
if (file.ok) log(file.value) // <-- here it exists
file.ok ? log(file.value) : log(file.error)
file.invert ?? log(file.value)
log(file.flat) // <-- has either value or error
file.log() // <-- logs the value of flat
file.log(0, 10) // <-- converts to string and clices
file.slice(0, 10) // <-- same but does not log
file.map((v) => v.toUpperCase()) // <-- maps the value to uppercase
function canFail() {
// <-- returns Result<string>
const shouldFail = Math.random() > 0.5
if (shouldFail) return Err(new Error("Failed")) // <-- has call stack
return Ok("Success")
}
canFail().ok
const wrapped = wrap(readFile) // <-- function wrapper
const res = await wrapped("test.txt")
res.flat // <-- has the value or error
```