Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

thrown

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

thrown

Generic Error Handler

  • 0.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Thrown

Generic Error handler

npm i thrown

Example

// logger function is optional
// logger => internal logger
import errorHandler, { genericError, logger } from 'thrown'

// without options
function thisIsFunction() {
  try {
    throw genericError('this is message', { /* ... */ })
  } catch (err) {
    errorHandler(err)
  }
}

// with options : case 1
function thisIsFunction() {
  try {
    throw genericError.create('prefix >')('this is message', { /* ... */ })
    // or
    throw genericError.create({ prefix: 'prefix >' })('this is message', { /* ... */ })
  } catch (err) {
    errorHandler(err)
  }
}

// with options : case 2
function thisIsFunction() {
  try {
    throw genericError('this is message', { /* ... */ })
  } catch (err) {
    errorHandler.extra({
      // if set `externalLogger`, no use internal logger
      // internal logger uses `console.group`
      externalLogger: (message, err, stack) => {
        console.log(message, err);
        console.log(stack);
      }
    })(err)
  }
}

// with options : case 3
function thisIsFunction() {
  try {
    throw genericError.('this is message', { /* ... */ })
  } catch (err) {
    errorHandler.extra({
      // additional information
      handler: (err) => {
        // compute something

        // this object will combine with original error object
        // should be an object (not array)
        return { computedErr }
      }
    })(err)
  }
}

API

import errorHandler from 'thrown'

try {
  throw new Error('this is error')
} catch (err) {
  // all options are optional
  errorHandler.extra({
    // no display in production
    mode: process.env.NODE_ENV,

    // css text eg. 'font-size: 10px;'
    style: '',

    // default <thrown>
    name: 'any name',

    // default true (group or groupCollapsed)
    expanded: true,

    // default false (console.trace), if trace false, it uses error.stack
    trace: false,

    // use external logger
    externalLogger: (message, err, stack) => {
      console.log(message, err);
      console.log(stack);
    },

    // combine additional information with original error
    handler: (err) => {
      const computedErr = {

      return { computedErr }
    }

  })(err)
}

License

MIT

Keywords

FAQs

Package last updated on 11 Apr 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc