Skip to content

An utility class to generate and handle RESTful api errors in web service.

License

Notifications You must be signed in to change notification settings

rockyliyanlok/node-restful-errors

Repository files navigation

node-restful-errors

Build Status Download Stats

An utility class to generate and handle RESTful api errors in web service.

Installation

To install the random generator, use npm:

npm install --save restful-errors

Usage

const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const RestfulErrors = require('restful-errors');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/bad-request', (req, res, next) => {
  next(new RestfulErrors.BadRequestError());
})

app.get('/customized-messages', (req, res, next) => {
  next(new RestfulErrors.BadRequestError([
    { 
      location: 'query', 
      param: 'uid', 
      value: undefined, 
      msg: 'The uid field is required.' 
    }, 
    { 
      location: 'body', 
      param: 'name', 
      value: 'A', 
      msg: 'The name field must be between 2 and 255 characters in length.' 
    }
  ]));
})

app.use(RestfulErrors.handler());

const server = http.createServer(app);
server.listen(3000);

module.exports = server;

API

Tests

npm install
npm run test

LICENSE

node-resful-errors is licensed under the MIT license.

About

An utility class to generate and handle RESTful api errors in web service.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published