Skip to content

fixing enableExpressErrorHandler logic #6423

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
Show file tree
Hide file tree
Changes from all 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
17 changes: 4 additions & 13 deletions spec/EnableExpressErrorHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ describe('Enable express error handler', () => {
const masterKey = 'anOtherTestMasterKey';
let server;

let lastError;

const parseServer = ParseServer.ParseServer(
Object.assign({}, defaultConfiguration, {
appId: appId,
Expand All @@ -25,8 +23,8 @@ describe('Enable express error handler', () => {
server = app.listen(12667);

app.use(function(err, req, res, next) {
next;
lastError = err;
expect(err.message).toBe('Object not found.');
next(err);
});

request({
Expand All @@ -43,15 +41,8 @@ describe('Enable express error handler', () => {
fail('Should throw error');
})
.catch(response => {
const reqError = response.data;
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);
expect(response).toBeDefined();
expect(response.status).toEqual(500);
})
.then(() => {
server.close(done);
Expand Down
7 changes: 3 additions & 4 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ export function allowMethodOverride(req, res, next) {
export function handleParseErrors(err, req, res, next) {
const log = (req.config && req.config.loggerController) || defaultLogger;
if (err instanceof Parse.Error) {
if (req.config && req.config.enableExpressErrorHandler) {
return next(err);
}
let httpStatus;
// TODO: fill out this mapping
switch (err.code) {
Expand All @@ -333,13 +336,9 @@ export function handleParseErrors(err, req, res, next) {
default:
httpStatus = 400;
}

res.status(httpStatus);
res.json({ code: err.code, error: err.message });
log.error('Parse error: ', 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