Comparing version 0.0.3 to 0.0.4
27
index.js
@@ -13,13 +13,24 @@ 'use strict'; | ||
function errorHandler(req, error) { | ||
let message = 'Something went wrong', | ||
statusCode = 500; | ||
let body, | ||
statusCode = 500, | ||
headers = {}; | ||
if (error.name === 'HTTPError' || error.name === 'WebError') { | ||
message = error.message; | ||
statusCode = error.statusCode || error.status || 500; | ||
if (error.isBoom && error.output) { | ||
statusCode = error.output.statusCode; | ||
headers = error.output.headers; | ||
body = error.output.payload; | ||
} else { | ||
body = { | ||
statusCode: 500, | ||
error: 'Internal Server Error', | ||
message: 'Something went wrong' | ||
}; | ||
} | ||
const body = { error: { message: message, statusCode: statusCode } }; | ||
return serialize(body).status(statusCode); | ||
const res = serialize(body); | ||
res.statusCode = statusCode; | ||
for (const name of Object.keys(headers)) { | ||
res.setHeader(name, headers[name]); | ||
} | ||
return res; | ||
} | ||
@@ -26,0 +37,0 @@ |
{ | ||
"name": "cass", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "It rhymes with jax-rs", | ||
@@ -17,4 +17,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"boom": "^2.7.1", | ||
"concat-stream": "^1.4.8", | ||
"node-http-error": "^0.2.0", | ||
"tape": "^4.0.0" | ||
@@ -21,0 +21,0 @@ }, |
@@ -10,7 +10,7 @@ # Cass | ||
const cass = require('cass'), | ||
// HTTPError is whitelisted for providing status codes | ||
HTTPError = require('node-http-error'); | ||
// `boom` is whitelisted for providing status codes | ||
Boom = require('boom'); | ||
const handler = cass(req => { | ||
if (req.method !== 'GET') throw new HTTPError(405); | ||
if (req.method !== 'GET') throw Boom.create(405); | ||
@@ -17,0 +17,0 @@ return { |
@@ -5,3 +5,3 @@ 'use strict'; | ||
const respond = require('quinn/respond'); | ||
const HTTPError = require('node-http-error'); | ||
const Boom = require('boom'); | ||
@@ -23,4 +23,4 @@ const cass = require('../'); | ||
const body = JSON.parse(res.bodyString); | ||
t.equal(body.error.message, 'Something went wrong'); // hides the actual error | ||
t.equal(body.error.statusCode, 500); | ||
t.equal(body.message, 'Something went wrong'); // hides the actual error | ||
t.equal(body.statusCode, 500); | ||
t.end(); | ||
@@ -37,3 +37,3 @@ } | ||
function throwError() { | ||
throw new HTTPError(422, 'Invalid thing'); | ||
throw Boom.create(422, 'Invalid thing'); | ||
} | ||
@@ -45,4 +45,4 @@ | ||
const body = JSON.parse(res.bodyString); | ||
t.equal(body.error.message, 'Invalid thing'); | ||
t.equal(body.error.statusCode, 422); | ||
t.equal(body.message, 'Invalid thing'); | ||
t.equal(body.statusCode, 422); | ||
t.end(); | ||
@@ -49,0 +49,0 @@ } |
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
7491
152