@hexeo/abstract-error

    1.0.3 • Public • Published

    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
      }
    }

    Keywords

    none

    Install

    npm i @hexeo/abstract-error

    Homepage

    www.hexeo.be

    DownloadsWeekly Downloads

    2

    Version

    1.0.3

    License

    MIT

    Last publish

    Collaborators

    • hexeo