Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zmian/xcore-express

A lightweight library that brings middlewares to handle error reporting in web applications using Node and Express.
https://github.com/zmian/xcore-express

Last synced: about 1 month ago
JSON representation

A lightweight library that brings middlewares to handle error reporting in web applications using Node and Express.

Awesome Lists containing this project

README

        

# xcore-express [![NPM version][npm-version-image]][npm-url] [![MIT License][license-image]][license-url]

A lightweight library that brings middlewares to handle error reporting in web applications using [Node](https://nodejs.org) and [Express](http://expressjs.com).

## Dependencies

- [Exception.io: xcore-exceptions](https://github.com/zmian/Exception.io)

## Installation

```sh
$ npm install xcore-express
```

```js
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);
```

## Usage

A reference guide to middlewares and helper functions included in xcore.

### [Exception Classes](https://github.com/zmian/Exception.io)


Class NameDescriptionDefault Message


ExceptionThe exception that is thrown when an error occurs.No Description
ArgumentExceptionThe exception that is thrown when one of the arguments provided to a method is not valid.Invalid Argument
InvalidInputExceptionThe exception that is thrown when one of the input provided to a method is not valid.Invalid Input
NotImplementedExceptionThe exception that is thrown when a requested method or operation is not implemented.Not Implemented
NotSupportedExceptionThe exception that is thrown when a requested method or operation is not supported.Not Supported
NotFoundExceptionThe exception that is thrown when an attempt is made to find something that does not exist.Not Found
UnauthenticatedExceptionThe exception that is thrown when a requested method or operation requires authentication.Not Authenticated
UnauthorizedExceptionThe exception that is thrown when the current user is not allowed to perform an attempted operation.Not Authorized

### Middlewares

#### Exception:

Use any of the aforementioned classes to throw an execption and the middlewares will automagically catch and format them appropriately.

```js
var xcore = require('xcore-express');

if (!req.isAuthenticated()) {
throw new xcore.UnauthenticatedException("Authentication is required.");
}
```

#### ErrorHandler

A middleware to detect an error type and send a JSON response with approperiate HTTP Status code and message.

Example response:

```js
{
"meta": {
"code": 401,
"success": false,
"message": "Authentication is required."
}
}
```

![Browser Screenshot](/resources/browser-screenshot.png)

#### Logger

A middleware to format and log errors to console.

Sample console log:

```sh
>> GET /api/users/me
>> [UnauthenticatedException] Authentication is required.
>> [Error]
>> ... stack trace ...
```

![Console Screenshot](/resources/console-screenshot.png)

## Creator

- [Zeeshan Mian](https://github.com/zmian) ([@zmian](https://twitter.com/zmian))

## License

xcore-express is released under the MIT license. See LICENSE for details.

[license-image]: http://img.shields.io/badge/license-MIT-blue.svg
[license-url]: LICENSE

[npm-url]: https://www.npmjs.com/package/xcore-express
[npm-version-image]: http://img.shields.io/npm/v/xcore-express.svg