Skip to content

Commit 3499212

Browse files
committed
Merge pull request #106 from digitalsadhu/fix/http-status-codes
Fix/http status codes
2 parents 02bb00b + 3ace31d commit 3499212

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

lib/create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var url = require('url');
22
var utils = require('./utils');
3+
var statusCodes = require('http-status-codes');
34

45
module.exports = function (app, options) {
56
//get remote methods.
@@ -18,7 +19,7 @@ module.exports = function (app, options) {
1819
if (ctx.method.name === 'create') {
1920
//JSON API specifies that created resources should have the
2021
//http status code of 201
21-
ctx.res.statusCode = 201;
22+
ctx.res.statusCode = statusCodes.CREATED;
2223

2324
//build the location url for the created resource.
2425
var location = url.format({

lib/delete.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var utils = require('./utils');
2+
var statusCodes = require('http-status-codes');
23

34
module.exports = function (app, options) {
45
//get remote methods.
@@ -17,8 +18,8 @@ module.exports = function (app, options) {
1718
if (ctx.method.name === 'deleteById') {
1819
//JSON API specifies that successful
1920
//deletes with no content returned should be 204
20-
if (ctx.res.statusCode === 200) {
21-
ctx.res.status(204);
21+
if (ctx.res.statusCode === statusCodes.OK) {
22+
ctx.res.status(statusCodes.NO_CONTENT);
2223
}
2324
}
2425
next();

lib/errors.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var debug;
2+
var statusCodes = require('http-status-codes');
23

34
module.exports = function (app, options) {
45
debug = options.debug;
@@ -26,7 +27,7 @@ function JSONAPIErrorHandler (err, req, res, next) {
2627
res.set('Content-Type', 'application/vnd.api+json');
2728

2829
var errors = [];
29-
var statusCode = err.statusCode || err.status || 500;
30+
var statusCode = err.statusCode || err.status || statusCodes.INTERNAL_SERVER_ERROR;
3031
debug('Error thrown:');
3132
debug('Raw error object:', err);
3233

@@ -61,14 +62,14 @@ function JSONAPIErrorHandler (err, req, res, next) {
6162

6263
if (additionalValidationErrors.indexOf(err.message) !== -1) {
6364
debug('Recasting error as a validation error.');
64-
statusCode = 422;
65+
statusCode = statusCodes.UNPROCESSABLE_ENTITY;
6566
err.code = 'presence';
6667
err.name = 'ValidationError';
6768
}
6869

6970
debug('Handling invalid relationship specified in url');
7071
if (/Relation (.*) is not defined for (.*) model/.test(err.message)) {
71-
statusCode = 400;
72+
statusCode = statusCodes.BAD_REQUEST;
7273
err.message = 'Bad Request';
7374
err.code = 'INVALID_INCLUDE_TARGET';
7475
err.name = 'BadRequest';
@@ -78,7 +79,7 @@ function JSONAPIErrorHandler (err, req, res, next) {
7879
} else {
7980
debug('Unable to determin error type. Treating error as a general 500 server error.');
8081
//catch all server 500 error if we were unable to understand the error.
81-
errors.push(buildErrorResponse(500, 'Internal Server error', 'GENERAL_SERVER_ERROR'));
82+
errors.push(buildErrorResponse(statusCodes.INTERNAL_SERVER_ERROR, 'Internal Server error', 'GENERAL_SERVER_ERROR'));
8283
}
8384

8485
//generalise the error code. More specific error codes are kept
@@ -104,9 +105,9 @@ function JSONAPIErrorHandler (err, req, res, next) {
104105
function generalizeStatusCode (statusCode) {
105106
switch (String(statusCode)[0]) {
106107
case '4':
107-
return 400;
108+
return statusCodes.BAD_REQUEST;
108109
case '5':
109-
return 500;
110+
return statusCodes.INTERNAL_SERVER_ERROR;
110111
}
111112

112113
return statusCode;
@@ -125,7 +126,7 @@ function generalizeStatusCode (statusCode) {
125126
*/
126127
function buildErrorResponse (httpStatusCode, errorDetail, errorCode, errorName, propertyName) {
127128
return {
128-
status: httpStatusCode || 500,
129+
status: httpStatusCode || statusCodes.INTERNAL_SERVER_ERROR,
129130
source: (propertyName) ? { pointer: 'data/attributes/' + propertyName } : '',
130131
title: errorName || '',
131132
code: errorCode || '',

lib/patch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var statusCodes = require('http-status-codes');
2+
13
module.exports = function (app, options) {
24
//iterate over all loopback models. This gives us a constructor function
35
//and allows us to overwrite the relationship methods:
@@ -57,7 +59,7 @@ function convertNullToNotFoundError (toModelName, ctx, cb) {
5759
var fk = ctx.getArgByName('fk');
5860
var msg = 'Unknown "' + toModelName + '" id "' + fk + '".';
5961
var error = new Error(msg);
60-
error.statusCode = error.status = 404;
62+
error.statusCode = error.status = statusCodes.NOT_FOUND;
6163
error.code = 'MODEL_NOT_FOUND';
6264
cb(error);
6365
}

lib/utilities/relationship-utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('lodash');
2+
var statusCodes = require('http-status-codes');
23

34
/* global module */
45
module.exports = {
@@ -18,9 +19,9 @@ module.exports = {
1819
*/
1920
function getInvalidIncludesError (message) {
2021
var error = new Error(message || 'JSON API resource does not support `include`');
21-
error.statusCode = 400;
22-
error.code = 400;
23-
error.status = 400;
22+
error.statusCode = statusCodes.BAD_REQUEST;
23+
error.code = statusCodes.BAD_REQUEST;
24+
error.status = statusCodes.BAD_REQUEST;
2425

2526
return error;
2627
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"dependencies": {
3434
"body-parser": "^1.14.1",
3535
"debug": "^2.2.0",
36+
"http-status-codes": "^1.0.5",
3637
"inflection": "^1.7.2",
3738
"lodash": "^4.0.0",
3839
"type-is": "^1.6.9"

0 commit comments

Comments
 (0)