Skip to content
Open
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
34 changes: 27 additions & 7 deletions Framework/Backend/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,41 @@
res.status(404).sendFile(path.join(__dirname, '../../Frontend/404.html'));
});

/**
* Ensures important information is saved in an error.
* @param {*} err - The error to serialize.

Check warning on line 238 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Test on windows-latest

Prefer a more specific type to `*`

Check warning on line 238 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Tests & coverage on ubuntu-latest

Prefer a more specific type to `*`

Check warning on line 238 in Framework/Backend/http/server.js

View workflow job for this annotation

GitHub Actions / Tests on macos-latest

Prefer a more specific type to `*`
*/
function serializeError(err) {
if (err && typeof err === 'object') {
return {
...err, // Keeps any custom enumerable properties
name: err.name,
message: err.message,
stack: err.stack,
};
}
return { message: String(err) };
}

// Error handler when an API controller crashes
this.app.use('/api', (err, req, res, next) => {
this.logger.errorMessage(`Request ${this._parseOriginalUrl(req)} failed: ${err.message || err}`);
this.logger.trace(err);

if (res.headersSent) {
return next(err);
}

if (process.env.NODE_ENV === 'development') {
res.status(500).json({
error: err,
});
} else {
res.status(500).json({
error: '500 - Server error',
message: 'Something went wrong, please try again or contact an administrator.',
return res.status(500).json({
error: serializeError(err),
});
}

res.status(500).json({
error: '500 - Server error',
message: 'Something went wrong, please try again or contact an administrator.',
});
});

// Error handler when a controller crashes
Expand Down
18 changes: 18 additions & 0 deletions Framework/Backend/test/mocha-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ describe('REST API', () => {
}, done);
});

it('Error route should respond with message and stack when NODE_ENV = development', async () => {
const prevNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
try {
httpServer.get('/get-error', (req, res, next) => next(new Error('Some unexpected error')));
const { body } = await request(httpServer)
.get(`/api/get-error?token=${token}`)
.expect('Content-Type', /json/)
.expect(500);

assert.strictEqual(body.error.name, 'Error');
assert.strictEqual(body.error.message, 'Some unexpected error');
assert.ok(body.error.stack.includes('Some unexpected error'));
} finally {
process.env.NODE_ENV = prevNodeEnv;
}
});

it('GET with token should respond 200/JSON', (done) => {
request(httpServer)
.get(`/api/get-request?token=${token}`)
Expand Down
Loading