Skip to content

Commit de9a417

Browse files
author
Michael Salinger
committed
Option to use express error handler
1 parent ade1ca4 commit de9a417

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function ExpressOAuthServer(options) {
2222
throw new InvalidArgumentError('Missing parameter: `model`');
2323
}
2424

25+
this.useErrorHandler = options.useErrorHandler ? true : false;
26+
delete options.useErrorHandler;
27+
2528
this.server = new NodeOAuthServer(options);
2629
}
2730

@@ -49,8 +52,8 @@ ExpressOAuthServer.prototype.authenticate = function(options) {
4952
next();
5053
})
5154
.catch(function(e) {
52-
return handleError(e, req, res);
53-
});
55+
return handleError(e, req, res, null, next);
56+
});
5457
};
5558
};
5659

@@ -80,7 +83,7 @@ ExpressOAuthServer.prototype.authorize = function(options) {
8083
return handleResponse(req, res, response);
8184
})
8285
.catch(function(e) {
83-
return handleError(e, req, res, response);
86+
return handleError(e, req, res, response, next);
8487
});
8588
};
8689
};
@@ -111,7 +114,7 @@ ExpressOAuthServer.prototype.token = function(options) {
111114
return handleResponse(req, res, response);
112115
})
113116
.catch(function(e) {
114-
return handleError(e, req, res, response);
117+
return handleError(e, req, res, response, next);
115118
});
116119
};
117120
};
@@ -129,17 +132,21 @@ var handleResponse = function(req, res, response) {
129132
* Handle error.
130133
*/
131134

132-
var handleError = function(e, req, res, response) {
135+
var handleError = function(e, req, res, response, next) {
133136

134-
if (response) {
135-
res.set(response.headers);
136-
}
137+
if (this.useErrorHandler === true) {
138+
next(e);
139+
} else {
140+
if (response) {
141+
res.set(response.headers);
142+
}
137143

138-
if (e instanceof UnauthorizedRequestError) {
139-
return res.status(e.code);
140-
}
144+
if (e instanceof UnauthorizedRequestError) {
145+
return res.status(e.code);
146+
}
141147

142-
res.status(e.code).send({ error: e.name, error_description: e.message });
148+
res.status(e.code).send({ error: e.name, error_description: e.message });
149+
}
143150
};
144151

145152
/**

0 commit comments

Comments
 (0)