Skip to content

Enable express error handler #4697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions spec/EnableExpressErrorHandler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const ParseServer = require("../src/index");
const express = require('express');
const rp = require('request-promise');

describe('Enable express error handler', () => {
beforeEach((done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you need this, as you create a new server separately. Please remove if not required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Really not needed

reconfigureServer({
enableExpressErrorHandler: true,
schemaCacheTTL: 30000
}).then(() => {
done();
});
});

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems written to, but never read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used to store the server to be closed later

.then(() => {
                  server.close(done);
                });


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line as no effect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to silence the linter.
The function needs to have 4 arguments to be recognized as an error handler, but I don't want to call next again

lastError = err
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you follow style and conventions and add the ; at the expected places please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep the alignment consistent with the rest of the code? Better, use async / await

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping the alignment :)

fail('Should throw error');
})
.catch(e => {
expect(e).toBeDefined();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do some tests on the expected error here? As well as lastError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

expect(lastError).toBeDefined();
})
.then(() => {
server.close(done);
});
})
}}
));
});

});

6 changes: 6 additions & 0 deletions src/Options/Definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down