mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
add express-validator, add body parser middleware, support some more errors in error handler middleware
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
"estraverse": "^4.1.1",
|
"estraverse": "^4.1.1",
|
||||||
"express": "~4.13.3",
|
"express": "~4.13.3",
|
||||||
"express-csv": "~0.6.0",
|
"express-csv": "~0.6.0",
|
||||||
|
"express-validator": "^2.18.0",
|
||||||
"firebase": "^2.2.9",
|
"firebase": "^2.2.9",
|
||||||
"firebase-token-generator": "^2.0.0",
|
"firebase-token-generator": "^2.0.0",
|
||||||
"glob": "^4.3.5",
|
"glob": "^4.3.5",
|
||||||
@@ -82,7 +83,7 @@
|
|||||||
"superagent": "~1.4.0",
|
"superagent": "~1.4.0",
|
||||||
"swagger-node-express": "lefnire/swagger-node-express#habitrpg",
|
"swagger-node-express": "lefnire/swagger-node-express#habitrpg",
|
||||||
"universal-analytics": "~0.3.2",
|
"universal-analytics": "~0.3.2",
|
||||||
"validator": "~3.19.0",
|
"validator": "~4.2.1",
|
||||||
"winston": "~2.0.1"
|
"winston": "~2.0.1"
|
||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ describe('errorHandler', () => {
|
|||||||
sandbox.stub(logger, 'error');
|
sandbox.stub(logger, 'error');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends internal server error if error is not a CustomError', () => {
|
it('sends internal server error if error is not a CustomError and is not identified', () => {
|
||||||
let error = new Error();
|
let error = new Error();
|
||||||
|
|
||||||
errorHandler(error, req, res, next);
|
errorHandler(error, req, res, next);
|
||||||
@@ -35,6 +35,38 @@ describe('errorHandler', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('identifies errors with statusCode property and format them correctly', () => {
|
||||||
|
let error = new Error('Error message');
|
||||||
|
error.statusCode = 400;
|
||||||
|
|
||||||
|
errorHandler(error, req, res, next);
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledOnce;
|
||||||
|
expect(res.json).to.be.calledOnce;
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledWith(400);
|
||||||
|
expect(res.json).to.be.calledWith({
|
||||||
|
error: 'Error',
|
||||||
|
message: 'Error message',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('doesn\'t leak info about 500 errors', () => {
|
||||||
|
let error = new Error('Some secret error message');
|
||||||
|
error.statusCode = 500;
|
||||||
|
|
||||||
|
errorHandler(error, req, res, next);
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledOnce;
|
||||||
|
expect(res.json).to.be.calledOnce;
|
||||||
|
|
||||||
|
expect(res.status).to.be.calledWith(500);
|
||||||
|
expect(res.json).to.be.calledWith({
|
||||||
|
error: 'InternalServerError',
|
||||||
|
message: 'Internal server error.',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('sends CustomError', () => {
|
it('sends CustomError', () => {
|
||||||
let error = new BadRequest();
|
let error = new BadRequest();
|
||||||
|
|
||||||
|
|||||||
@@ -23,11 +23,20 @@ export default function errorHandler (err, req, res, next) {
|
|||||||
// If we can't identify it, respond with a generic 500 error
|
// If we can't identify it, respond with a generic 500 error
|
||||||
let responseErr = err instanceof CustomError ? err : null;
|
let responseErr = err instanceof CustomError ? err : null;
|
||||||
|
|
||||||
if (!responseErr) {
|
// Handle errors created with 'http-errors' or similar that have a status/statusCode property
|
||||||
|
if (err.statusCode && typeof err.statusCode === 'number') {
|
||||||
|
responseErr = new CustomError();
|
||||||
|
responseErr.httpCode = err.statusCode;
|
||||||
|
responseErr.error = err.name;
|
||||||
|
responseErr.message = err.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!responseErr || responseErr.httpCode >= 500) {
|
||||||
// Try to identify the error...
|
// Try to identify the error...
|
||||||
// ...
|
// ...
|
||||||
// Otherwise create an InternalServerError and use it
|
// Otherwise create an InternalServerError and use it
|
||||||
// we don't want to leak anything, just a generic error message
|
// we don't want to leak anything, just a generic error message
|
||||||
|
// Use it also in case of identified errors but with httpCode === 500
|
||||||
responseErr = new InternalServerError();
|
responseErr = new InternalServerError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,17 @@
|
|||||||
// This module is only used to attach middlewares to the express app
|
// This module is only used to attach middlewares to the express app
|
||||||
|
|
||||||
import errorHandler from './errorHandler';
|
import errorHandler from './errorHandler';
|
||||||
|
import bodyParser from 'body-parser';
|
||||||
|
|
||||||
export default function attachMiddlewares (app) {
|
export default function attachMiddlewares (app) {
|
||||||
|
|
||||||
|
// Parse query parameters and json bodies
|
||||||
|
// TODO handle errors
|
||||||
|
app.use(bodyParser.urlencoded(
|
||||||
|
extended: true, // Uses 'qs' library as old connect middleware
|
||||||
|
}));
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
// Error handler middleware, define as the last one
|
// Error handler middleware, define as the last one
|
||||||
app.use(errorHandler);
|
app.use(errorHandler);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user