Skip to content

Commit 2d7cb4c

Browse files
committed
Add error handler
1 parent 4853f15 commit 2d7cb4c

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

config/express.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import bodyParser from 'body-parser';
55
import compress from 'compression';
66
import methodOverride from 'method-override';
77
import cors from 'cors';
8+
import httpStatus from 'http-status';
89
import routes from '../server/routes';
10+
import * as utilityService from '../server/services/utility';
911

1012
const app = express();
1113

@@ -23,35 +25,40 @@ app.use(cors());
2325

2426
app.use('/api', routes);
2527

28+
// catch different types of error
29+
app.use(function (err, req, res, next) {
30+
//if (err instanceof expressValidation.ValidationError) {
31+
// const completeErrorMessage = _(err.errors).map((error) => {
32+
// return error.messages.join('. ');
33+
// }).join(' and ');
34+
// const error = utilityService.createEnhancedError(completeErrorMessage, err.status);
35+
// error.category = 'ParamValidationError';
36+
// return next(error);
37+
//} else if (err instanceof mongoose.Error.ValidationError) {
38+
// const completeErrorMessage = _(err.errors).map((value, key) => {
39+
// return `${key}: ${value.message}`;
40+
// }).join(' and ');
41+
// const error = utilityService.createEnhancedError(completeErrorMessage, httpStatus.BAD_REQUEST);
42+
// error.category = 'MongooseValidationError';
43+
// return next(error);
44+
//} else {
45+
const error = utilityService.createError(err.message, err.status, err.isPublic);
46+
return next(error);
47+
//}
48+
});
49+
2650
// catch 404 and forward to error handler
2751
app.use(function(req, res, next) {
28-
var err = new Error('Not Found');
29-
err.status = 404;
30-
next(err);
52+
const err = utilityService.createError('API not found', httpStatus.NOT_FOUND);
53+
return next(err);
3154
});
3255

33-
// error handlers
34-
35-
// development error handler
36-
// will print stacktrace
37-
if (app.get('env') === 'development') {
38-
app.use(function(err, req, res, next) {
39-
res.status(err.status || 500);
40-
res.render('error', {
41-
message: err.message,
42-
error: err
43-
});
56+
// error handler
57+
app.use(function(err, req, res, next) {
58+
return res.status(err.status).json({
59+
message: err.isPublic ? err.message : httpStatus[err.status],
60+
error: process.env.NODE_ENV === 'development' ? err : {}
4461
});
45-
}
46-
47-
// production error handler
48-
// no stacktraces leaked to user
49-
//app.use(function(err, req, res, next) {
50-
// res.status(err.status || 500);
51-
// res.render('error', {
52-
// message: err.message,
53-
// error: {}
54-
// });
55-
//});
62+
});
5663

5764
export default app;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"cors": "^2.7.1",
1313
"debug": "~2.2.0",
1414
"express": "~4.13.1",
15+
"http-status": "^0.2.0",
1516
"jade": "~1.11.0",
1617
"method-override": "^2.3.5",
1718
"morgan": "~1.6.1",

server/services/utility.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import httpStatus from 'http-status';
2+
3+
export function createError(message, status = httpStatus.INTERNAL_SERVER_ERROR, isPublic = false) {
4+
const error = new Error(message);
5+
error.status = status;
6+
error.isPublic = isPublic;
7+
return error;
8+
}

0 commit comments

Comments
 (0)