Nonlinear Programming Methods

    restify-errors-thrower

    2.0.0 • Public • Published

    Restify Errors Thrower

    Travis CI Codecov npm npm version npm dependencies npm dev dependencies

    💥 Throw Restify errors easily and consistently!

    Install

    $ npm install --save restify-errors-thrower
    

    Usage

    const restify = require('restify');
    const thrower = require('restify-errors-thrower');
     
    // Creates a Restify server
    const server = restify.createServer({
      name: 'myapp',
      version: '1.0.0'
    });
     
    // Creates a foo endpoint
    server.get('/foo', function(req, res, next) {
      if (!req.query.foo) {
        return next(thrower.throw('BadRequestError', 'foo undefined', 'R.FOO.0');
      }
      if (req.query.foo !== 'unicorn') {
        return next(thrower.throw('BadRequestError', 'foo should be an 🦄!', 'R.FOO.1');
      }
     
      // Adds debug info
      const keys = req.query.keys();
      if (keys.length > 1) {
        return next(thrower.throw('BadRequestError', 'Only foo allowed', 'R.FOO.2', keys);
      }
     
      res.send(200, 'ok!');
      return next();
    });
     
    // Logs errors and debug info
    server.on('after', function(req, res, route, err) {
      if (err && err.context) {
        const method = req.method.toUpperCase();
        const uri = req._url.path;
        const endpoint = method + '\t' + uri;
     
        console.log('[API]', endpoint, err.toString() + ' (' + err.context.errno + ')');
        if(err.context.debug) {
          debug.forEach(d => console.log('[DEBUG]', endpoint, d);
        }
      }
    });

    API

    throw(type, message, errno, [debug])

    Throw a specific Restify error.

    type

    Type: string

    The type of error to throw. The list of types available can be found here

    message

    Type: string

    An human-friendly error message sent to the client.
    Never sent error messages that comes from other modules!!! (E.g: your database)
    This may expose you to undesired hackers attack!
    Use the debug parameter instead for sensitive errors!

    errno

    Type: string|number

    An unique error id code to send to clients.
    This will help your client to programmatically handle the error your API will throw.
    Choose a style and be consistent with it!

    debug

    Type: string|number

    An indefinite number of contex information to collect.
    This is particular useful to send contex details to your logger!
    This will never sent to the client so you can store server critical messages. (E.g; errors coming from third pary APIs or errors coming from your DB)

    thrown(err, [type])

    Checks if a specific Restify error was thrown.

    err

    Type: object

    The object to check

    type

    Type: string Default: undefined

    The type of error that the object should be instance of.

    Authors

    See also the list of contributors who participated in this project.

    License

    This project is licensed under the MIT License - see the LICENSE file for details.

    Install

    npm i restify-errors-thrower

    DownloadsWeekly Downloads

    2

    Version

    2.0.0

    License

    MIT

    Last publish

    Collaborators

    • simonepri