safe-error
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Safe Error

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

Example

  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

  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<T> = {error: Error|null, result: T|null}

safe<T>( () => T) => Result

safeAsync<T>( () => Promise<T>) => Promise<Result>

Readme

Keywords

Package Sidebar

Install

npm i safe-error

Weekly Downloads

2

Version

1.0.2

License

ISC

Unpacked Size

15.7 kB

Total Files

17

Last publish

Collaborators

  • mehdi.zonjy