Skip to content

Commit b22947d

Browse files
saulogtflovilmart
authored andcommitted
Enable express error handler (#4697)
* Propagate error to express handler in all situations * Call the default error handler if `enableExpressErrorHandler` is truthy * Updating options interface and definitions * Testing express error handler * Test spec fixes * Fix test
1 parent ced6b76 commit b22947d

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const ParseServer = require("../lib/index");
2+
const express = require('express');
3+
const rp = require('request-promise');
4+
5+
describe('Enable express error handler', () => {
6+
7+
it('should call the default handler in case of error, like updating a non existing object', done => {
8+
const serverUrl = "http://localhost:12667/parse"
9+
const appId = "anOtherTestApp";
10+
const masterKey = "anOtherTestMasterKey";
11+
let server;
12+
13+
let lastError;
14+
15+
const parseServer = ParseServer.ParseServer(Object.assign({},
16+
defaultConfiguration, {
17+
appId: appId,
18+
masterKey: masterKey,
19+
serverURL: serverUrl,
20+
enableExpressErrorHandler: true,
21+
__indexBuildCompletionCallbackForTests: promise => {
22+
promise.then(() => {
23+
expect(Parse.applicationId).toEqual("anOtherTestApp");
24+
const app = express();
25+
app.use('/parse', parseServer);
26+
27+
server = app.listen(12667);
28+
29+
app.use(function (err, req, res, next) {
30+
next;
31+
lastError = err;
32+
})
33+
34+
rp({
35+
method: 'PUT',
36+
uri: serverUrl + '/classes/AnyClass/nonExistingId',
37+
headers: {
38+
'X-Parse-Application-Id': appId,
39+
'X-Parse-Master-Key': masterKey
40+
},
41+
body: { someField: "blablabla" },
42+
json: true
43+
}).then(() => {
44+
fail('Should throw error');
45+
}).catch(e => {
46+
expect(e).toBeDefined();
47+
const reqError = e.error;
48+
expect(reqError).toBeDefined();
49+
expect(lastError).toBeDefined();
50+
51+
expect(lastError.code).toEqual(101)
52+
expect(lastError.message).toEqual('Object not found.')
53+
54+
expect(lastError.code).toEqual(reqError.code);
55+
expect(lastError.message).toEqual(reqError.error);
56+
}).then(() => {
57+
server.close(done);
58+
});
59+
})
60+
}
61+
}
62+
));
63+
});
64+
65+
});
66+

src/Options/Definitions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ module.exports.ParseServerOptions = {
262262
"action": parsers.booleanParser,
263263
"default": false
264264
},
265+
"enableExpressErrorHandler": {
266+
"env": "PARSE_SERVER_ENABLE_EXPRESS_ERROR_HANDLER",
267+
"help": "Enables the default express error handler for all errors",
268+
"action": parsers.booleanParser,
269+
"default": false
270+
},
265271
"objectIdSize": {
266272
"env": "PARSE_SERVER_OBJECT_ID_SIZE",
267273
"help": "Sets the number of characters in generated object id's, default 10",

src/Options/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ export interface ParseServerOptions {
114114
cacheMaxSize : ?number; // = 10000
115115
/* Use a single schema cache shared across requests. Reduces number of queries made to _SCHEMA. Defaults to false, i.e. unique schema cache per request. */
116116
enableSingleSchemaCache: ?boolean; // = false
117+
/* Enables the default express error handler for all errors */
118+
enableExpressErrorHandler: ?boolean; // = false
117119
/* Sets the number of characters in generated object id's, default 10 */
118120
objectIdSize: ?number; // = 10
119121
/* The port to run the ParseServer. defaults to 1337.

src/middlewares.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ export function handleParseErrors(err, req, res, next) {
284284
res.status(httpStatus);
285285
res.json({ code: err.code, error: err.message });
286286
log.error(err.message, err);
287+
if (req.config && req.config.enableExpressErrorHandler) {
288+
next(err);
289+
}
287290
} else if (err.status && err.message) {
288291
res.status(err.status);
289292
res.json({ error: err.message });

0 commit comments

Comments
 (0)