Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/mehdizonjy/safe-error


https://github.com/mehdizonjy/safe-error

Last synced: 9 days ago
JSON representation

Awesome Lists containing this project

README

        

# Safe Error
This module is inspired by how golang handles errors, and allows you catch errors in a similar manner

## Example
```javascript
const {safe} = require('safe-error')
const safeJsonParse = (str) => safe( () => JSON.parse(str))

const {result, error} = safeJsonParse('{"msg": "Hello World}')
if(error) {
console.log('Invalid Json')
}
else {
console.log(result)
}

```
It can also handle promise functions

```javascript
const axios = require('axios')
const {safeAsync} = require('safe-error')
const safeGET = (url) => safeAsync( () => axios.get(url))

const main = async () => {
const {result, error} = await safeGET('http://invalidurl')
if(error) {
console.log('Failed to call endpoint', error)
}
else {
console.log(result.data)
}
}
main()

```

## API
`type Result = {error: Error|null, result: T|null}`

`safe( () => T) => Result`

`safeAsync( () => Promise) => Promise`