Skip to content

Commit cd56b58

Browse files
committed
feat(error): generic error handler for API requests
1 parent 3e2644f commit cd56b58

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

server/lib/helpers/ApiError.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const ApiErrorCodes = {
4+
serverError: 'SERVER_ERROR'
5+
}
6+
7+
class ApiError extends Error {
8+
constructor (message, {status, code} = {}) {
9+
super(message)
10+
11+
// Set Generic error message
12+
this.message = message
13+
14+
// Set HTTP status code
15+
this.status = status || 500
16+
17+
// Set API error code
18+
this.code = code || ApiErrorCodes.serverError
19+
20+
// Ensures that stack trace uses our subclass name
21+
this.name = this.constructor.name
22+
23+
// Ensures the ApiError subclass is sliced out of the
24+
// stack trace dump for clarity
25+
Error.captureStackTrace(this, this.constructor)
26+
}
27+
}
28+
29+
module.exports = ApiError

server/lib/services/express.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ module.exports.initErrorRoutes = function (app) {
166166
// Log it
167167
console.error(err.stack);
168168

169-
// Redirect to error page
170-
res.status(500).send();
169+
// Build an error response object and send it with the
170+
// status in the error
171+
res.status(err.status).send({
172+
message: err.message,
173+
code: err.code
174+
});
171175
});
172176
};
173177

server/modules/users/server/controllers/users/users.authentication.server.controller.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
const path = require('path')
77
const config = require(path.resolve('./lib/config'))
8+
const ApiError = require(path.resolve('./lib/helpers/ApiError'))
89
const errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'))
910
const mongoose = require('mongoose')
1011
const passport = require('passport')
@@ -22,12 +23,12 @@ var noReturnUrls = [
2223
/**
2324
* Signup
2425
*/
25-
exports.signup = async function (req, res) {
26+
exports.signup = async function (req, res, next) {
2627
try {
2728
const user = await UserService.signUp(req.body)
2829
return res.json(user)
2930
} catch (err) {
30-
return res.status(500).send(err.message)
31+
return next(new ApiError(err.message))
3132
}
3233
}
3334

@@ -49,7 +50,7 @@ exports.signout = function (req, res) {
4950
/**
5051
* Jwt Token Auth
5152
*/
52-
exports.token = async function (req, res) {
53+
exports.token = async function (req, res, next) {
5354
try {
5455
// Authenticate the user based on credentials
5556
// @TODO be consistent with whether the login field for user identification
@@ -69,7 +70,7 @@ exports.token = async function (req, res) {
6970

7071
res.status(200).json({token: token})
7172
} catch (err) {
72-
return res.status(500).send(err.message)
73+
return next(new ApiError(err.message))
7374
}
7475
}
7576

0 commit comments

Comments
 (0)