Skip to content

Commit

Permalink
Add passthroughErrors functionality.
Browse files Browse the repository at this point in the history
Closes oauthjs#7
  • Loading branch information
Tiago Ribeiro authored and Thom Seddon committed Jan 31, 2014
1 parent 8287717 commit 25accae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/oauthServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ OAuthServer.prototype.authorise = function () {
try {
yield auth(this.request, this.response);
} catch (err) {
if (!this.passthroughErrors)
return handleError(err, self.server, this);
return handleError(err, self.server, this);
}

yield next;
Expand All @@ -76,11 +75,13 @@ OAuthServer.prototype.authorise = function () {
*/
var handleError = function (err, server, ctx) {
if (server.debug) console.log(err.stack || err);

delete err.stack;

ctx.type = 'json';
ctx.status = err.code;
ctx.body = err;
if (!server.passthroughErrors) {
ctx.type = 'json';
ctx.status = err.code;
ctx.body = err;
}

return ctx.app.emit('error', err, ctx);
};
70 changes: 70 additions & 0 deletions test/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright 2014-present Thom Seddon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var koa = require('koa'),
request = require('supertest'),
should = require('should');

var oauthServer = require('../');

var bootstrap = function (oauthConfig) {
var app = koa();
app.oauth = oauthServer(oauthConfig || { model: {} });

app.use(app.oauth.authorise());

return app;
};

describe('Error Handler', function () {

it('should return an oauth conformat response', function (done) {
var app = bootstrap();

request(app.listen())
.get('/')
.expect(400)
.end(function (err, res) {
if (err) return done(err);

res.body.should.have.keys('code', 'error', 'error_description');

res.body.code.should.be.a.Number;
res.body.code.should.equal(res.statusCode);

res.body.error.should.be.a.String;

res.body.error_description.should.be.a.String;

done();
});
});

it('should passthrough authorise errors', function (done) {
var app = bootstrap({
passthroughErrors: true,
model: {}
});

app.on('error', function (err, ctx) {
ctx.body = 'passthrough';
});

request(app.listen())
.get('/')
.expect(/^passthrough$/, 200, done);
});
});

0 comments on commit 25accae

Please sign in to comment.