Skip to content

Commit

Permalink
Merge pull request #35 from dynamiccast/non-intrusive-error-handling
Browse files Browse the repository at this point in the history
Non intrusive error handling
  • Loading branch information
dynamiccast authored Aug 29, 2016
2 parents 3870cef + 13db1df commit d2d150e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
26 changes: 7 additions & 19 deletions lib/api/responses/negotiate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,18 @@

module.exports = function negotiate (err) {

// Get access to response object (`res`)
var res = this.res;
var statusCode = err.status || 500;

var statusCode = 500;
var body = err;

try {

statusCode = err.status || 500;

// Set the status
// (should be taken care of by res.* methods, but this sets a default just in case)
res.status(statusCode);

} catch (e) {}
res.status(statusCode);

// Respond using the appropriate custom response
if (statusCode === 403) return res.forbidden(body);
if (statusCode === 404) return res.notFound(body);
if (statusCode === 403) return res.forbidden(err);
if (statusCode === 404) return res.notFound(err);

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

if (statusCode >= 400 && statusCode < 500) return res.badRequest(body);
return res.serverError(body);
return res.json(err);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-json-api-blueprints",
"version": "0.11.6",
"version": "0.12.0",
"description": "Blueprints to turn a Sails.js API into a JSON API",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit d2d150e

Please sign in to comment.