express-router-api
Advanced tools
Comparing version 1.0.3 to 1.0.4
81
index.js
@@ -40,3 +40,47 @@ 'use strict'; | ||
}; | ||
let handleErrors = (promiseChain, req, res) => { | ||
let apiErrorHandler = err => { | ||
res.status(err.statusCode || 500).json(err.message); | ||
return Promise.resolve(); | ||
}; | ||
promiseChain | ||
.catch(ExpressApiRouterError, err => { | ||
res.emit('expressApiRouterError', err); | ||
if(!silenceExpressApiRouterError) { | ||
console.error(err.stack); | ||
} | ||
}) | ||
.catch(ApiError, apiErrorHandler) | ||
.catch(err => { | ||
let formatError = err => { | ||
if(this.options.errorFormatter) { | ||
return Promise.resolve().then(() => this.options.errorFormatter(err, req, res)); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
return formatError(err).then(jsonError => { | ||
res.status(500).json(jsonError || this.options.internalServerError | ||
|| {error: 'Internal server error'}); | ||
if(!jsonError) { // rethrow only not-formatted errors | ||
throw err; | ||
} | ||
}) | ||
// support re-thrown ApiError from error formatter | ||
.catch(ApiError, apiErrorHandler); | ||
}); | ||
}; | ||
let oldImplementation = this.param; | ||
this.param = function(name, cb) { | ||
oldImplementation.call(this, name, (req, res, next, value) => { | ||
let promiseChain = Promise.resolve() | ||
.then(() => cb(req, res, value)) | ||
.then(next); | ||
handleErrors(promiseChain, req, res); | ||
}); | ||
}; | ||
methods.forEach(method => { | ||
@@ -49,13 +93,7 @@ let oldImplementation = this[method]; | ||
return (req, res, next) => { | ||
let apiErrorHandler = err => { | ||
res.status(err.statusCode || 500).json(err.message); | ||
return Promise.resolve(); | ||
}; | ||
Promise.resolve().then(() => origHandler(req, res, next)) | ||
.then((returnValue) => { | ||
let promiseChain = Promise.resolve().then(() => origHandler(req, res, next)) | ||
.tap((returnValue) => { | ||
if(typeof returnValue === 'undefined' && index === callbacks.length - 1) { | ||
throw new ExpressApiRouterError('Warning: Route for ' + path.toString() + ' did not return a promise - this happens when normal route handler is attached to express-router-api. Everything most likely works but you are advised to return API data through a promise.'); | ||
} | ||
return Promise.resolve(returnValue); | ||
}) | ||
@@ -75,27 +113,4 @@ .then(Promise.resolveDeep) | ||
} | ||
}).catch(ExpressApiRouterError, err => { | ||
res.emit('expressApiRouterError', err); | ||
if(!silenceExpressApiRouterError) { | ||
console.error(err.stack); | ||
} | ||
}) | ||
.catch(ApiError, apiErrorHandler) | ||
.catch(err => { | ||
let formatError = err => { | ||
if(this.options.errorFormatter) { | ||
return Promise.resolve().then(() => this.options.errorFormatter(err, req, res)); | ||
} | ||
return Promise.resolve(); | ||
}; | ||
return formatError(err).then(jsonError => { | ||
res.status(500).json(jsonError || this.options.internalServerError | ||
|| {error: 'Internal server error'}); | ||
if(!jsonError) { // rethrow only not-formatted errors | ||
throw err; | ||
} | ||
}) | ||
// support re-thrown ApiError from error formatter | ||
.catch(ApiError, apiErrorHandler); | ||
}); | ||
handleErrors(promiseChain, req, res); | ||
}; | ||
@@ -102,0 +117,0 @@ }); |
{ | ||
"name": "express-router-api", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Express router that lets you construct your API entirely on promises", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
37
test.js
@@ -27,4 +27,11 @@ 'use strict'; | ||
function requestTest(data, statusCode) { | ||
return rp(`http://localhost:${port}/foo`).then(data => { | ||
function paramTest() { | ||
let args = Array.prototype.slice.call(arguments); | ||
let paramHandler = args.pop(); | ||
router.get.apply(router, ['/foo/:param'].concat(args)); | ||
router.param('param', paramHandler); | ||
} | ||
function requestTest(data, statusCode, extra) { | ||
return rp(`http://localhost:${port}/foo${extra||''}`).then(data => { | ||
if(data[0] === '[' || data[0] === '{') { | ||
@@ -226,2 +233,28 @@ return JSON.parse(data); | ||
}); | ||
it('should support param handler', () => { | ||
paramTest((req, res) => { | ||
return req.paramData; | ||
}, (req, res, param) => { | ||
return Promise.delay(20).then(() => { | ||
req.paramData = {foo: req.params.param}; | ||
}); | ||
}); | ||
return requestTest({ | ||
foo: 'xxx' | ||
}, 200, '/xxx'); | ||
}); | ||
it('should support reporting JSON errors from promise when thrown from param handler', () => { | ||
paramTest((req, res) => {}, (req, res, param) => { | ||
return Promise.delay(10).then(() => { | ||
throw new ApiError({error: 'test'}, 403); | ||
}); | ||
}); | ||
return requestTest({ | ||
error: 'test' | ||
}, 403, '/xxx'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12236
323