Extended from common error, class TError allows create and cofigurate custom errors. Error's configuration allow using logging, user alert, debugging simple and fast.
Because i want unify work with errors. Make it simple and clean.
$ npm install @rnskv/terror
import { TErrorGroup } from '@rnskv/terror';
- Create errors white list;
- Create Errors Groups, using list from 1 step;
- Generate new errors in your code :).
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
}
};
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.
It's simple :)
try {
throw ServerErrors.create('PAGE_NOT_FOUND');
// Or
throw ServerErrors.create('INTERNAL_SERVER_ERROR');
} catch(error) {
console.log('Catch error:', error)
}
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.