A lightweight library that brings middlewares to handle error reporting in web applications using Node and Express.
$ npm install xcore-express
var express = require('express');
var app = express();
var xcore = require('xcore-express');
// Load the xcore error logger and handler middlewares
app.use(xcore.Logger);
app.use(xcore.ErrorHandler);
A reference guide to middlewares and helper functions included in xcore.
Class Name | Description | Default Message |
---|---|---|
Exception | The exception that is thrown when an error occurs. | No Description |
ArgumentException | The exception that is thrown when one of the arguments provided to a method is not valid. | Invalid Argument |
InvalidInputException | The exception that is thrown when one of the input provided to a method is not valid. | Invalid Input |
NotImplementedException | The exception that is thrown when a requested method or operation is not implemented. | Not Implemented |
NotSupportedException | The exception that is thrown when a requested method or operation is not supported. | Not Supported |
NotFoundException | The exception that is thrown when an attempt is made to find something that does not exist. | Not Found |
UnauthenticatedException | The exception that is thrown when a requested method or operation requires authentication. | Not Authenticated |
UnauthorizedException | The exception that is thrown when the current user is not allowed to perform an attempted operation. | Not Authorized |
Use any of the aforementioned classes to throw an execption and the middlewares will automagically catch and format them appropriately.
var xcore = require('xcore-express');
if (!req.isAuthenticated()) {
throw new xcore.UnauthenticatedException("Authentication is required.");
}
A middleware to detect an error type and send a JSON response with approperiate HTTP Status code and message.
Example response:
{
"meta": {
"code": 401,
"success": false,
"message": "Authentication is required."
}
}
A middleware to format and log errors to console.
Sample console log:
>> GET /api/users/me
>> [UnauthenticatedException] Authentication is required.
>> [Error]
>> ... stack trace ...
xcore-express is released under the MIT license. See LICENSE for details.