0.1.4 • Published 3 years ago

rest-error-handler v0.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

rest-error-handler

This project defines error formats of RESTful APIs. By following my guideline, one's APIs will be consistent to RESTful standards. This project is in its very early experimental stage, thus it is highly recommended not to implement in production.

At the moment only three types of errors have formalised: 401, 403, 404, 422 and 500.

Installation

rest-error-handler is available as an npm package.

yarn add rest-error-handler
npm install --save rest-error-handler

Docs

Step One: Import rest-error-handler and attach it after all your routes

// index.js
import express from 'express';
import restErrorHandler from 'rest-error-handler';

import login from './login.js';

const app = express();
app.get('/login', login);
app.use(restErrorHandler());

Step Two: Create an error instance with status code and other configurable properties.

// login.js
export default function(req, res, next) {
    const {
        username,
    } = req.query;
    
    if (username === void 0) {
        const err = new Error('Unprocessable Entity');
        err.status = 422; // HTTP status code 422
        err.details = [{
            code: 'invalid',
            field: 'username', // Declare which field is required
            resource: 'Login',
        }];
        return next(err);
    }
    
    if (username !== 'correct name') {
        const err = new Error();
        err.status = 401; // HTTP status code 401
        return next(err);
    }
    
    res.send('you are logged in');
}
0.1.4

3 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.2

6 years ago

0.0.1

6 years ago