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
This commit is contained in:
committed by
Florent Vilmart
parent
faa04f7209
commit
2c316ceaad
66
spec/EnableExpressErrorHandler.spec.js
Normal file
66
spec/EnableExpressErrorHandler.spec.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const ParseServer = require("../lib/index");
|
||||
const express = require('express');
|
||||
const rp = require('request-promise');
|
||||
|
||||
describe('Enable express error handler', () => {
|
||||
|
||||
it('should call the default handler in case of error, like updating a non existing object', done => {
|
||||
const serverUrl = "http://localhost:12667/parse"
|
||||
const appId = "anOtherTestApp";
|
||||
const masterKey = "anOtherTestMasterKey";
|
||||
let server;
|
||||
|
||||
let lastError;
|
||||
|
||||
const parseServer = ParseServer.ParseServer(Object.assign({},
|
||||
defaultConfiguration, {
|
||||
appId: appId,
|
||||
masterKey: masterKey,
|
||||
serverURL: serverUrl,
|
||||
enableExpressErrorHandler: true,
|
||||
__indexBuildCompletionCallbackForTests: promise => {
|
||||
promise.then(() => {
|
||||
expect(Parse.applicationId).toEqual("anOtherTestApp");
|
||||
const app = express();
|
||||
app.use('/parse', parseServer);
|
||||
|
||||
server = app.listen(12667);
|
||||
|
||||
app.use(function (err, req, res, next) {
|
||||
next;
|
||||
lastError = err;
|
||||
})
|
||||
|
||||
rp({
|
||||
method: 'PUT',
|
||||
uri: serverUrl + '/classes/AnyClass/nonExistingId',
|
||||
headers: {
|
||||
'X-Parse-Application-Id': appId,
|
||||
'X-Parse-Master-Key': masterKey
|
||||
},
|
||||
body: { someField: "blablabla" },
|
||||
json: true
|
||||
}).then(() => {
|
||||
fail('Should throw error');
|
||||
}).catch(e => {
|
||||
expect(e).toBeDefined();
|
||||
const reqError = e.error;
|
||||
expect(reqError).toBeDefined();
|
||||
expect(lastError).toBeDefined();
|
||||
|
||||
expect(lastError.code).toEqual(101)
|
||||
expect(lastError.message).toEqual('Object not found.')
|
||||
|
||||
expect(lastError.code).toEqual(reqError.code);
|
||||
expect(lastError.message).toEqual(reqError.error);
|
||||
}).then(() => {
|
||||
server.close(done);
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -262,6 +262,12 @@ module.exports.ParseServerOptions = {
|
||||
"action": parsers.booleanParser,
|
||||
"default": false
|
||||
},
|
||||
"enableExpressErrorHandler": {
|
||||
"env": "PARSE_SERVER_ENABLE_EXPRESS_ERROR_HANDLER",
|
||||
"help": "Enables the default express error handler for all errors",
|
||||
"action": parsers.booleanParser,
|
||||
"default": false
|
||||
},
|
||||
"objectIdSize": {
|
||||
"env": "PARSE_SERVER_OBJECT_ID_SIZE",
|
||||
"help": "Sets the number of characters in generated object id's, default 10",
|
||||
|
||||
@@ -114,6 +114,8 @@ export interface ParseServerOptions {
|
||||
cacheMaxSize : ?number; // = 10000
|
||||
/* 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. */
|
||||
enableSingleSchemaCache: ?boolean; // = false
|
||||
/* Enables the default express error handler for all errors */
|
||||
enableExpressErrorHandler: ?boolean; // = false
|
||||
/* Sets the number of characters in generated object id's, default 10 */
|
||||
objectIdSize: ?number; // = 10
|
||||
/* The port to run the ParseServer. defaults to 1337.
|
||||
|
||||
@@ -284,6 +284,9 @@ export function handleParseErrors(err, req, res, next) {
|
||||
res.status(httpStatus);
|
||||
res.json({ code: err.code, error: err.message });
|
||||
log.error(err.message, err);
|
||||
if (req.config && req.config.enableExpressErrorHandler) {
|
||||
next(err);
|
||||
}
|
||||
} else if (err.status && err.message) {
|
||||
res.status(err.status);
|
||||
res.json({ error: err.message });
|
||||
|
||||
Reference in New Issue
Block a user