Categories
Restify

Restify — Error Handling with Restify-Errors

Spread the love

Restify is a simple Node back end framework.

In this article, we’ll look at how to format error messages with Restify.

Formatting Errors

We can format errors with the error handler.

For example, we can write:

var restify = require('restify');
var errors = require('restify-errors');

var server = restify.createServer();

server.get('/hello/:foo', function(req, res, next) {
  var err = new errors.InternalServerError('not found');
  return next(err);
});

server.on('InternalServer', function (req, res, err, cb) {
  err.toString = function() {
    return 'an internal server error occurred!';
  };

  err.toJSON = function() {
    return {
      message: 'an internal server error occurred!',
      code: 'error'
    }
  };

  return cb();
});

server.on('restifyError', function (req, res, err, cb) {
  return cb();
});

server.listen(8080);

We have the InternalServer handler with the toString and toJSON methods to format string and JSON errors.

The restifyError error handler has the error handler.

Also, we can create formatters for other content types.

For example, we can write:

var restify = require('restify');
var errors = require('restify-errors');

const server = restify.createServer({
  formatters: {
    ['text/html'](req, res, body) {
      if (body instanceof Error) {
        return `<html><body>${body.message}</body></html>`;
      }
    }
  }
});

server.get('/', function(req, res, next) {
  res.header('content-type', 'text/html');
  return next(new errors.InternalServerError('error!'));
});

server.listen(8080);

to create a content formatter for the text/html content type.

restify-errors

The restify-errors module exposes a suite of error constrictors for many common HTTP requests and REST related errors.

For example, we can create errors by writing:

var restify = require('restify');
var errors = require('restify-errors');

const server = restify.createServer();

server.get('/', function(req, res, next) {
  return next(new errors.ConflictError("conflict"));
});

server.listen(8080);

Then when we go to http://localhost:8080 , we get:

{"code":"Conflict","message":"conflict"}

We can also pass in Restify errors as an argument of res.send .

For example, we can write:

var restify = require('restify');
var errors = require('restify-errors');

const server = restify.createServer();

server.get('/', function(req, res, next) {
  res.send(new errors.GoneError('gone'));
  return next();
});

server.listen(8080);

We pass in the GoneError instance into the res.send method.

Then when we go to http://localhost:8080 , we get:

{"code":"Gone","message":"gone girl"}

The automatic serialization to JSON is done by calling JSON.stringify on the Error object.

They all have the toJSON method defined.

If the object doesn’t have the toJSON method defined, then we would get an empty object.

HttpError

HttpError is a generic HTTP error that we can return with the statusCode and body properties.

The statusCode will be automatically set with the HTTP status code, and the body will be set to the message by default.

All status codes between 400 error 500s are automatically be converted to an HttpError with the name being in PascalCase and spaces removed.

RestError

Restify provides us with built it errors with the code and message properties/

For example, if we have:

var restify = require('restify');
var errors = require('restify-errors');

const server = restify.createServer();

server.get('/', function(req, res, next) {
  return next(new errors.InvalidArgumentError("error"));
});

server.listen(8080);

Any 400 or 500 series errors can be created with restify-errors .

Conclusion

We can create error objects with restify-errors .

They’ll be automatically be serialized into JSON if we use it in our response.

Leave a Reply

Your email address will not be published. Required fields are marked *