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

global-error-handler

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

global-error-handler

This package is for handling error in any type of Javascript application in an aspect oriented way.

latest
Source
npmnpm
Version
0.0.7
Version published
Maintainers
1
Created
Source

Coverage Status npm version

global-error-handler

This package is for handling error in any type of Javascript application in an aspect oriented way.

Installation

npm install global-error-handler

Why would you need this?

Main idea of this implementation is to handle different types of errors in their own custom way without introducing handler implementation to the code where you throw the error.

You would reap the benefit of this package in an application that you would have multiple types of errors and each of them has different way of being handled.

To understand better, see the Usage section.

Usage

There are two ways of registering an ErrorHandler.

Register with a unique 'string' key

// someMain.js
import { GlobalErrorHandler } from 'global-error-handler';

const globalErrorHandler = new GlobalErrorHandler();

const someHandler = error => {
 // processing error code comes here
}

globalErrorHandler.register({
    key: 'someKey',
    handler: someHandler
})

// someComponent.js

const globalErrorHandler = require('./path/to/global-error-handler/instance')

globalErrorHandler.handle(new Error('Some Error'), 'someError');

...

const someError = { message: 'someError' };
globalErrorHandler.handle(someError, 'someError');

...

Register with an Error class

You can also reigster an ErrorHandler by passing a class which inherits Error class which is defined by default in Javascript.

This way you can have the benefit of being able to use throw keyword and make your app catch the error and pass it to GlobalErrorHandler to handle it.

// someMain.js
import { GlobalErrorHandler } from 'global-error-handler';

const globalErrorHandler = new GlobalErrorHandler();

class SomeError extends Error {}

const someHandler = error => {
 // processing error code comes here
}

globalErrorHandler.register({
    key: SomeError,
    handler: someHandler
})

// in a browser app
window.onerror = (message, url, line, column, error) => {
  globalErrorHandler.handle(error);
};

// someComponent.js
const SomeError = require('./path/to/SomeError');

...
throw new SomeError('Some message');
...

Keywords

error

FAQs

Package last updated on 18 May 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