+16
-21
@@ -5,4 +5,2 @@ 'use strict'; | ||
| const WebError = require('./web-error'); | ||
| function ptry(f, self, args) { | ||
@@ -14,10 +12,10 @@ return new Promise(function(resolve) { | ||
| function makeDefaultErrorHandler(serialize) { | ||
| function defaultErrorHandler(req, error) { | ||
| function createErrorHandler(serialize) { | ||
| function errorHandler(req, error) { | ||
| let message = 'Something went wrong', | ||
| statusCode = 500; | ||
| if (error instanceof WebError) { | ||
| if (error.name === 'HTTPError' || error.name === 'WebError') { | ||
| message = error.message; | ||
| statusCode = error.statusCode; | ||
| statusCode = error.statusCode || error.status || 500; | ||
| } | ||
@@ -30,11 +28,12 @@ | ||
| return defaultErrorHandler; | ||
| return errorHandler; | ||
| } | ||
| const defaultErrorHandler = makeDefaultErrorHandler(json); | ||
| function cass(options) { | ||
| function cass(options, inner) { | ||
| if (inner === undefined) { | ||
| inner = options, options = {}; | ||
| } | ||
| options = options || {}; | ||
| const serialize = options.serialize || json; | ||
| const errorHandler = options.errorHandler || makeDefaultErrorHandler(serialize); | ||
| const errorHandler = options.errorHandler || createErrorHandler(serialize); | ||
@@ -46,11 +45,9 @@ function resolveResponse(res) { | ||
| function withHandler(inner) { | ||
| return function (req) { | ||
| return ptry(inner, this, arguments) | ||
| .then(resolveResponse) | ||
| .catch(errorHandler.bind(null, req)); | ||
| }; | ||
| function handler(req) { | ||
| return ptry(inner, this, arguments) | ||
| .then(resolveResponse) | ||
| .catch(errorHandler.bind(null, req)); | ||
| } | ||
| return withHandler; | ||
| return handler; | ||
| } | ||
@@ -60,4 +57,2 @@ | ||
| cass['default'] = cass; | ||
| cass['defaultErrorHandler'] = defaultErrorHandler; | ||
| cass['makeDefaultErrorHandler'] = makeDefaultErrorHandler; | ||
| cass['WebError'] = WebError; | ||
| cass['createErrorHandler'] = createErrorHandler; |
+2
-2
| { | ||
| "name": "cass", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "It rhymes with jax-rs", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
| "concat-stream": "^1.4.8", | ||
| "quinn": "^3.2.0", | ||
| "node-http-error": "^0.2.0", | ||
| "tape": "^4.0.0" | ||
@@ -21,0 +21,0 @@ }, |
+7
-5
@@ -5,10 +5,12 @@ # Cass | ||
| A convenience layer to build JSON APIs using [`Quinn`](https://github.com/quinnjs/quinn). | ||
| A convenience layer to build JSON APIs using | ||
| [`Quinn`](https://github.com/quinnjs/quinn). | ||
| ```js | ||
| const cass = require('cass'), | ||
| WebError = cass.WebError; | ||
| // HTTPError is whitelisted for providing status codes | ||
| HTTPError = require('node-http-error'); | ||
| const handler = cass()(req => { | ||
| if (req.method !== 'GET') throw new WebError(405); | ||
| const handler = cass(req => { | ||
| if (req.method !== 'GET') throw new HTTPError(405); | ||
@@ -55,4 +57,4 @@ return { | ||
| // node style request listener -> node http server | ||
| createServer(createApp(cass()(createRouter(routes)))) | ||
| createServer(createApp(cass(createRouter(routes)))) | ||
| .listen(3000); | ||
| ``` |
+6
-6
@@ -5,5 +5,5 @@ 'use strict'; | ||
| const respond = require('quinn/respond'); | ||
| const HTTPError = require('node-http-error'); | ||
| const cass = require('../'), | ||
| WebError = cass.WebError; | ||
| const cass = require('../'); | ||
@@ -28,3 +28,3 @@ const testQuinnHandler = require('./test-quinn-handler'); | ||
| testQuinnHandler(cass()(throwError)) | ||
| testQuinnHandler(cass(throwError)) | ||
| .then(verify, t.end); | ||
@@ -37,3 +37,3 @@ }); | ||
| function throwError() { | ||
| throw new WebError('Invalid thing', 422); | ||
| throw new HTTPError(422, 'Invalid thing'); | ||
| } | ||
@@ -50,3 +50,3 @@ | ||
| testQuinnHandler(cass()(throwError)) | ||
| testQuinnHandler(cass(throwError)) | ||
| .then(verify, t.end); | ||
@@ -77,4 +77,4 @@ }); | ||
| const req = { url: '/foo', method: 'GET' }; | ||
| testQuinnHandler(cass({ errorHandler: customErrorHandler })(throwError), req) | ||
| testQuinnHandler(cass({ errorHandler: customErrorHandler }, throwError), req) | ||
| .then(verify, t.end); | ||
| }); |
@@ -23,4 +23,4 @@ 'use strict'; | ||
| testQuinnHandler(cass()(renderHelloWorld)) | ||
| testQuinnHandler(cass(renderHelloWorld)) | ||
| .then(verify, t.end); | ||
| }); |
| 'use strict'; | ||
| const test = require('tape'); | ||
| const WebError = require('../web-error'); | ||
| test('WebError(string, number)', function(t) { | ||
| const e = new WebError('msg', 403); | ||
| t.equal(e.message, 'msg'); | ||
| t.equal(e.statusCode, 403); | ||
| t.end(); | ||
| }); | ||
| test('WebError(string)', function(t) { | ||
| const e = new WebError('msg'); | ||
| t.equal(e.message, 'msg'); | ||
| t.equal(e.statusCode, 500); | ||
| t.end(); | ||
| }); | ||
| test('WebError(number)', function(t) { | ||
| const e = new WebError(404); | ||
| t.equal(e.message, 'Not Found'); | ||
| t.equal(e.statusCode, 404); | ||
| t.end(); | ||
| }); |
-19
| 'use strict'; | ||
| const http = require('http'); | ||
| function WebError(message, statusCode) { | ||
| if (typeof message === 'number') { | ||
| statusCode = message; | ||
| message = http.STATUS_CODES[statusCode]; | ||
| } | ||
| this.message = message; | ||
| this.statusCode = statusCode || 500; | ||
| this.name = 'WebError'; | ||
| Error.captureStackTrace(this, WebError); | ||
| } | ||
| WebError.prototype = Object.create(Error.prototype); | ||
| WebError.prototype.constructor = WebError; | ||
| module.exports = WebError; |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
59
3.51%0
-100%7375
-12.5%9
-18.18%140
-21.79%