Skip to content

Commit 416c81d

Browse files
author
Jorge Calderon
committed
v0.1.4 - add fix for res.headerssent, serialize errors in the response so that properties like 'stack' are included in the response
1 parent b7b0548 commit 416c81d

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ module.exports = getUsers;
5252

5353
##### 0.1.3
5454
- fix circular json in response
55+
56+
##### 0.1.4
57+
- add fix for res.headerssent
58+
- serialize errors in the response so that properties like 'stack' are included in the response

lib/methods/jsonResponseError.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const HTTP_STATUS_CODE_SERVER_ERROR = 500;
99

1010
const stringify = require('json-stringify-safe');
1111

12+
const serializeError = require('serialize-error');
13+
1214
/**
1315
* Respond to the client with an unsuccessful JSON payload.
1416
* @param {Object} req
@@ -17,8 +19,10 @@ const stringify = require('json-stringify-safe');
1719
* @param {Object} err
1820
* @returns {*}
1921
*/
20-
const jsonResponseError = R.curry((req, res, err) => {
21-
22+
const jsonResponseError = R.curry((req, res, next, err) => {
23+
if (res.headersSent) {
24+
return next(err);
25+
}
2226
const statusCode = R.propOr(HTTP_STATUS_CODE_SERVER_ERROR, 'statusCode', err);
2327

2428
const response = R.mergeRight(R.apply(R.bind(makeJsonResponseBody, req), [statusCode, req.flash]), { 'data' : R.omit(['statusCode'], err) });
@@ -28,7 +32,7 @@ const jsonResponseError = R.curry((req, res, err) => {
2832
}
2933

3034
res.set('Content-Type', 'application/json');
31-
return res.status(statusCode).send(stringify(response));
35+
return res.status(statusCode).send(stringify(serializeError(response)));
3236
});
3337

3438
module.exports = jsonResponseError;

package.json

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
{
2-
"name" : "alien-node-api-utils",
3-
"version" : "0.1.3",
4-
"description" : "Helper functions for API handling on NodeJS",
5-
"main" : "lib/API.js",
6-
"dependencies" : {
7-
"ramda" : "^0.x.x",
8-
"aybabtu" : "*"
2+
"name": "alien-node-api-utils",
3+
"version": "0.1.4",
4+
"description": "Helper functions for API handling on NodeJS",
5+
"main": "lib/API.js",
6+
"dependencies": {
7+
"aybabtu": "*",
8+
"ramda": "^0.x.x",
9+
"serialize-error": "^4.1.0"
910
},
10-
"devDependencies" : {
11-
"coveralls" : "^2.11.2",
12-
"istanbul" : "^0.3.13",
13-
"jasmine" : "^2.3.1"
11+
"devDependencies": {
12+
"coveralls": "^2.11.2",
13+
"istanbul": "^0.3.13",
14+
"jasmine": "^2.3.1"
1415
},
15-
"scripts" : {
16-
"test" : "./node_modules/.bin/istanbul cover ./node_modules/.bin/jasmine"
16+
"scripts": {
17+
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/jasmine"
1718
},
18-
"repository" : {
19-
"type" : "git",
20-
"url" : "https://github.com/aliencreations/alien-node-api-utils.git"
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/aliencreations/alien-node-api-utils.git"
2122
},
22-
"keywords" : [
23+
"keywords": [
2324
"aliencreations",
2425
"api",
2526
"util",
2627
"node",
2728
"ramda"
2829
],
29-
"author" : "Sean Cannon",
30-
"license" : "MIT",
31-
"bugs" : {
32-
"url" : "https://github.com/aliencreations/alien-node-api-utils/issues"
30+
"author": "Sean Cannon",
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/aliencreations/alien-node-api-utils/issues"
3334
},
34-
"homepage" : "https://github.com/aliencreations/alien-node-api-utils"
35+
"homepage": "https://github.com/aliencreations/alien-node-api-utils"
3536
}

spec/jsonResponseErrorSpec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ const EXPECTED_RESPONSE_DATA_NO_SESSION = JSON.stringify({
5757
data : FAKE_ERROR_RESPONSE_TRIMMED
5858
});
5959

60+
const FAKE_NEXT = jasmine.createSpy('next');
61+
6062
describe('makeJsonResponseError without session', () => {
6163

6264
let response = {};
6365

6466
beforeEach(() => {
6567
spyOn(mockRes, 'send');
66-
response = jsonResponseError(mockReqNoSession, mockRes, FAKE_ERROR_RESPONSE);
68+
response = jsonResponseError(mockReqNoSession, mockRes, FAKE_NEXT, FAKE_ERROR_RESPONSE);
6769
});
6870

6971
it('executes the mock res.send function', () => {
@@ -78,7 +80,7 @@ describe('makeJsonResponseError without flash', () => {
7880

7981
beforeEach(() => {
8082
spyOn(mockRes, 'send');
81-
response = jsonResponseError(mockReqNoFlash, mockRes, FAKE_ERROR_RESPONSE);
83+
response = jsonResponseError(mockReqNoFlash, mockRes, FAKE_NEXT, FAKE_ERROR_RESPONSE);
8284
});
8385

8486
it('executes the mock res.send function', () => {
@@ -93,7 +95,7 @@ describe('makeJsonResponseError with session', () => {
9395

9496
beforeEach(() => {
9597
spyOn(mockRes, 'send');
96-
response = jsonResponseError(mockReqWithSession, mockRes, FAKE_ERROR_RESPONSE);
98+
response = jsonResponseError(mockReqWithSession, mockRes, FAKE_NEXT, FAKE_ERROR_RESPONSE);
9799
});
98100

99101
it('executes the mock res.send function', () => {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,13 @@ safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0:
696696
resolved "https://registry.businesswire.npme.io/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
697697
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
698698

699+
serialize-error@^4.1.0:
700+
version "4.1.0"
701+
resolved "https://registry.businesswire.npme.io/serialize-error/-/serialize-error-4.1.0.tgz#63e1e33ede20bcd89d9f0528ea4c15fbf0f2b78a"
702+
integrity sha512-5j9GgyGsP9vV9Uj1S0lDCvlsd+gc2LEPVK7HHHte7IyPwOD4lVQFeaX143gx3U5AnoCi+wbcb3mvaxVysjpxEw==
703+
dependencies:
704+
type-fest "^0.3.0"
705+
699706
700707
version "1.0.9"
701708
resolved "https://registry.businesswire.npme.io/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"
@@ -783,6 +790,11 @@ type-check@~0.3.1:
783790
dependencies:
784791
prelude-ls "~1.1.2"
785792

793+
type-fest@^0.3.0:
794+
version "0.3.1"
795+
resolved "https://registry.businesswire.npme.io/type-fest/-/type-fest-0.3.1.tgz#63d00d204e059474fe5e1b7c011112bbd1dc29e1"
796+
integrity sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==
797+
786798
uglify-js@^3.1.4:
787799
version "3.6.0"
788800
resolved "https://registry.businesswire.npme.io/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"

0 commit comments

Comments
 (0)