New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

safe-error

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-error

a safe error handler inspired by golang approach

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

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>

Keywords

error

FAQs

Package last updated on 14 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts