Skip to content

heXeo/node-abstract-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AbstractError Build Status

Description

Provide a sexy way to extends the default javascript Error object.

Install

npm install @hexeo/abstract-error

Usages

Define a new error type

Simple

import AbstractError from '@hexeo/abstract-error';

class MyError extends AbstractError {
  constructor (message) {
    super (message);
  }
}

With custom properties

import AbstractError from '@hexeo/abstract-error';

class MyError extends AbstractError {
  constructor (message) {
    super (message);

    this.code = 500;
    this.reason = 'oups';
  }
}

With predefined message

import AbstractError from '@hexeo/abstract-error';

class MyError extends AbstractError {
  constructor () {
    super ();
  }

  this.message = 'Oups, something went wrong';
}

With custom constructor parameters

import AbstractError from '@hexeo/abstract-error';

class MyError extends AbstractError {
  constructor (message, foo, bar) {
    super (message);

    this.foo = foo;
    this.bar = bar;
  }
}

Throw and catch the error

import AbstractError from '@hexeo/abstract-error';

class MyError extends AbstractError {
  constructor () {
    super ();

    this.message = 'Oups, something went wrong.';
    this.code = 500;
    this.reason = 'oups';
  }
}

try {
  throw new MyError();
} catch (error) {
  if (error instanceof MyError) {
    console.log(error.code); // 500
  }
}

About

Abstract Error Class for Node.js

Resources

Stars

Watchers

Forks

Packages

No packages published