@gilbertco/restify-async-wrap
v0.1.2
Published
Wrap Restify ES7 Await functions to maintain Restify default error handling.
Downloads
25
Readme
Node Restify Async Wrap
npm i -S @gilbertco/restify-async-wrap
The only issue with using ES7 Async functions within Restify is dealing with un-caught exceptions. This can cause the route to hang, the client then timing out without error notification, which can also lead to possible denial of service attacks by blocking the Node.js event loop.
This module is mentioned within the following blog entry: https://blog.gilbertco.xyz/node-js-cleaner-api-routes-with-async-await/
CI Build Status
Basic Example
const restifyAsyncWrap = require('@gilbertco/restify-async-wrap');
const restify = require('restify');
const server = restify.createServer();
// Default Restify Error Handling
server.get('/', restifyAsyncWrap(async () => {
throw new Error('Caught error and used default 500 Restify error handling method.');
}));
// Custom Error Handling
server.get('/test', restifyAsyncWrap(async () => {
throw new Error('Caught error and returned 404.');
}), customErrorHandler);
function customErrorHandler(request, response, next, error) {
response.send(404, {error});
return next();
}