Skip to content

Commit 4b701d2

Browse files
authored
Merge pull request #19 from mjsalinger/use-error-handler
Use error handler
2 parents b47f246 + c1ffb61 commit 4b701d2

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

index.js

Lines changed: 31 additions & 22 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

@@ -34,22 +37,22 @@ function ExpressOAuthServer(options) {
3437
*/
3538

3639
ExpressOAuthServer.prototype.authenticate = function(options) {
37-
var server = this.server;
40+
var that = this;
3841

3942
return function(req, res, next) {
4043
var request = new Request(req);
4144
var response = new Response(res);
4245

43-
return Promise.bind(this)
46+
return Promise.bind(that)
4447
.then(function() {
45-
return server.authenticate(request, response, options);
48+
return this.server.authenticate(request, response, options);
4649
})
4750
.tap(function(token) {
4851
res.locals.oauth = { token: token };
4952
next();
5053
})
5154
.catch(function(e) {
52-
return handleError(e, req, res);
55+
return handleError.call(this, e, req, res, null, next);
5356
});
5457
};
5558
};
@@ -63,24 +66,24 @@ ExpressOAuthServer.prototype.authenticate = function(options) {
6366
*/
6467

6568
ExpressOAuthServer.prototype.authorize = function(options) {
66-
var server = this.server;
69+
var that = this;
6770

6871
return function(req, res, next) {
6972
var request = new Request(req);
7073
var response = new Response(res);
7174

72-
return Promise.bind(this)
75+
return Promise.bind(that)
7376
.then(function() {
74-
return server.authorize(request, response, options);
77+
return this.server.authorize(request, response, options);
7578
})
7679
.tap(function(code) {
7780
res.locals.oauth = { code: code };
7881
})
7982
.then(function() {
80-
return handleResponse(req, res, response);
83+
return handleResponse.call(this, req, res, response);
8184
})
8285
.catch(function(e) {
83-
return handleError(e, req, res, response);
86+
return handleError.call(this, e, req, res, response, next);
8487
});
8588
};
8689
};
@@ -94,24 +97,24 @@ ExpressOAuthServer.prototype.authorize = function(options) {
9497
*/
9598

9699
ExpressOAuthServer.prototype.token = function(options) {
97-
var server = this.server;
100+
var that = this;
98101

99102
return function(req, res, next) {
100103
var request = new Request(req);
101104
var response = new Response(res);
102105

103-
return Promise.bind(this)
106+
return Promise.bind(that)
104107
.then(function() {
105-
return server.token(request, response, options);
108+
return this.server.token(request, response, options);
106109
})
107110
.tap(function(token) {
108111
res.locals.oauth = { token: token };
109112
})
110113
.then(function() {
111-
return handleResponse(req, res, response);
114+
return handleResponse.call(this, req, res, response);
112115
})
113116
.catch(function(e) {
114-
return handleError(e, req, res, response);
117+
return handleError.call(this, e, req, res, response, next);
115118
});
116119
};
117120
};
@@ -136,17 +139,23 @@ var handleResponse = function(req, res, response) {
136139
* Handle error.
137140
*/
138141

139-
var handleError = function(e, req, res, response) {
142+
var handleError = function(e, req, res, response, next) {
140143

141-
if (response) {
142-
res.set(response.headers);
143-
}
144+
if (this.useErrorHandler === true) {
145+
next(e);
146+
} else {
147+
if (response) {
148+
res.set(response.headers);
149+
}
144150

145-
if (e instanceof UnauthorizedRequestError) {
146-
return res.status(e.code);
147-
}
151+
res.status(e.code);
152+
153+
if (e instanceof UnauthorizedRequestError) {
154+
return res.send();
155+
}
148156

149-
res.status(e.code).send({ error: e.name, error_description: e.message });
157+
res.send({ error: e.name, error_description: e.message });
158+
}
150159
};
151160

152161
/**

0 commit comments

Comments
 (0)