Skip to content

Commit d2d150e

Browse files
authored
Merge pull request #35 from dynamiccast/non-intrusive-error-handling
Non intrusive error handling
2 parents 3870cef + 13db1df commit d2d150e

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

lib/api/responses/negotiate.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,18 @@
2020

2121
module.exports = function negotiate (err) {
2222

23-
// Get access to response object (`res`)
2423
var res = this.res;
24+
var statusCode = err.status || 500;
2525

26-
var statusCode = 500;
27-
var body = err;
28-
29-
try {
30-
31-
statusCode = err.status || 500;
32-
33-
// Set the status
34-
// (should be taken care of by res.* methods, but this sets a default just in case)
35-
res.status(statusCode);
36-
37-
} catch (e) {}
26+
res.status(statusCode);
3827

3928
// Respond using the appropriate custom response
40-
if (statusCode === 403) return res.forbidden(body);
41-
if (statusCode === 404) return res.notFound(body);
29+
if (statusCode === 403) return res.forbidden(err);
30+
if (statusCode === 404) return res.notFound(err);
4231

43-
console.log(body);
4432
// This check is specific to sails-json-api-blueprints
45-
if (statusCode === 400 && err.code === "E_VALIDATION") return res.invalid(body);
33+
if (statusCode === 400 && err.code === "E_VALIDATION") return res.invalid(err);
34+
if (statusCode === 400) return res.badRequest(err);
4635

47-
if (statusCode >= 400 && statusCode < 500) return res.badRequest(body);
48-
return res.serverError(body);
36+
return res.json(err);
4937
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sails-json-api-blueprints",
3-
"version": "0.11.6",
3+
"version": "0.12.0",
44
"description": "Blueprints to turn a Sails.js API into a JSON API",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)