Tiny Error handler
Tiny and plugeable error handler with zero dependencies.
Quick usage
const ApplicationError = ; { supermessage status code; } const dbError = 'Critical error.' 500 3500; // Console output// { date: 'Mon, 04 May 2020 17:04:50 GMT',// http_status: 500,// message: 'Critical error.',// code: 3500,// stack:// 'Error\n at DatabaseError.ApplicationError ...' }
Base class
The package contains a base class named ApplicationError
with the follow properties and methods:
Property | Type | Description |
---|---|---|
_message |
private | Custom error message. |
_code |
private | Application specific error code. |
_status |
private | HTTP status code. |
_debug |
private | Debug flag. Default true |
_error |
private | Error in JSON format with properties: name, message, status and code. |
getError() |
public | Returns the _error object. |
API
The base class contains a couple of static methods to make it plugeable.
$config
This static method allows change, for now, the debug flag.
ApplicationError;
$addEvent
The base class contains an events stack that executes when a instance occur. You can add custom events for all the subclasses or for a specific subclass, and the method can access to the private properties and methods shown in the previous table.
For all subclasses:
const ApplicationError = ;{console;}ApplicationError;{supermessage status code;}const dbError = 'Critical error.' 500 3500;// Console output// { date: 'Mon, 04 May 2020 17:04:50 GMT',// http_status: 200,// message: 'Critical error.',// code: 500,// stack:// 'Error\n at DatabaseError.ApplicationError ...' }// My HTTP status is 500
For a specific subclass:
const ApplicationError = ;{console;}{supermessage status code;}{supermessage status code;}DatabaseError;const dbError = 'Critical error.' 500 3500;const dbError2 = 'Critical error 2.' 509 3100;// Console output// { date: 'Mon, 04 May 2020 17:04:50 GMT',// http_status: 500,// message: 'Critical error.',// code: 3500,// stack:// 'Error\n at DatabaseError.ApplicationError ...' }// My HTTP status is 500// { date: 'Mon, 04 May 2020 17:04:50 GMT',// http_status: 509,// message: 'Critical error.',// code: 3100,// stack:// 'Error\n at DatabaseError2.ApplicationError ...' }