@rnskv/terror

1.0.10 • Public • Published

Build Status Scrutinizer Code Quality

tError

Extended from common error, class TError allows create and cofigurate custom errors. Error's configuration allow using logging, user alert, debugging simple and fast.

Why?

Because i want unify work with errors. Make it simple and clean.

Installing

In terminal:

$ npm install @rnskv/terror

In code:

import { TErrorGroup } from '@rnskv/terror';

Setup

For working with errors you must did next steps:

  • Create errors white list;
  • Create Errors Groups, using list from 1 step;
  • Generate new errors in your code :).

First

For example, create list with 404 and 500 errors codes. It's object where key is error's name, value - object with next fields: message, status, code.

const errorsList = {
  PAGE_NOT_FOUND: {
    message: 'Page not found :(', //Message for user
    name: 'REQUEST_ERROR', //Name for logging
    code: 404 //HTTP code
  },
  INTERNAL_SERVER_ERROR: {
    message: 'Internal Server Error', //Message for user
    name: 'RESPONSE_ERROR', //Name for logging
    code: 500 //HTTP code
  }
};

Second

Next create new Errors Group. You must specify type!

const params = {
  type: 'SERVER_ERROR'
};

const ServerErrors = new TErrorGroup(params, errorsList)

Great! Now you have new Errors Group and you can generate new errors.

Third

It's simple :)

  try {
    throw ServerErrors.create('PAGE_NOT_FOUND');
    // Or
    throw ServerErrors.create('INTERNAL_SERVER_ERROR');
  } catch(error) {
    console.log('Catch error:', error)
  }

Logging

You can connect your logger function to Errors Group. For example, use console.error. Connect it to ServerErrors.

ServerErrors.setLogger(console.error);

Now every errors throw in this group will call console.error.

Package Sidebar

Install

npm i @rnskv/terror

Weekly Downloads

0

Version

1.0.10

License

ISC

Unpacked Size

16.1 kB

Total Files

7

Last publish

Collaborators

  • rnskv