Skip to content

Commit

Permalink
Fix leakage to global.code, update and fix tests, fix whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
joepie91 committed Mar 24, 2015
1 parent 00c8f58 commit efab21b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/errors.js
Expand Up @@ -928,3 +928,4 @@ var mapper = exports.mapper = function(errName, fn) {
var mapError = exports.mapError = function(err) {
return mapper(err.name) ? mapper(err.name)(err) : err;
};

4 changes: 2 additions & 2 deletions test/express.js
Expand Up @@ -65,7 +65,7 @@ function testError(code) {
};
};

for (code in http.STATUS_CODES) {
for (var code in http.STATUS_CODES) {
if (http.STATUS_CODES.hasOwnProperty(code) && code >= 400) {
describe('GET /errors/' + code, testError(code));
}
Expand Down Expand Up @@ -197,4 +197,4 @@ describe('Username and password error mappings', function() {
done();
});
});
});
});
89 changes: 47 additions & 42 deletions test/test.js
@@ -1,6 +1,7 @@

var errors = require('..')
, http = require('http');
, http = require('http')
, should = require('should');

describe('errors export structure', function() {
it('should contain find() method', function() {
Expand All @@ -20,7 +21,11 @@ describe('errors export structure', function() {
httpError.code.should.be.above(599);
});

for (code in http.STATUS_CODES) {
it('should not leak to global.code', function() {
should.not.exist(global.code);
});

for (var code in http.STATUS_CODES) {
it('should contain ' + http.STATUS_CODES[code] + ' Error', function() {
var errorName = 'Http' + code + 'Error'
, errorInstance = new errors[errorName]();
Expand Down Expand Up @@ -195,58 +200,58 @@ describe('errors.title()', function() {
});

describe('options style constructor', function() {
var IdentifiableError = errors.create('IdentifiableError'),
err = new IdentifiableError({message: 'Error with ref ID',
status: 501, refID: 'a1b2c3'});
var IdentifiableError = errors.create('IdentifiableError'),
err = new IdentifiableError({message: 'Error with ref ID',
status: 501, refID: 'a1b2c3'});

it('should contain refID property', function() {
err.refID.should.equal('a1b2c3');
});
it('should contain refID property', function() {
err.refID.should.equal('a1b2c3');
});

it('should have overridden status', function() {
err.status.should.equal(501);
});
it('should have overridden status', function() {
err.status.should.equal(501);
});

it('toString() should output refID', function() {
err.toString().should.include('refID: a1b2c3');
});
it('toString() should output refID', function() {
err.toString().should.include('refID: a1b2c3');
});

it('toJSON() should include refID', function() {
err.toJSON().should.include({refID: 'a1b2c3'});
});
it('toJSON() should include refID', function() {
err.toJSON().should.include({refID: 'a1b2c3'});
});

it('should have overriden message', function() {
err.toString().should.include(': Error with ref ID');
});
it('should have overriden message', function() {
err.toString().should.include(': Error with ref ID');
});

it('should not allow overriding of stack', function() {
(function() {new IdentifiableError({stack: 'fail'});}).should.throw();
});
it('should not allow overriding of stack', function() {
(function() {new IdentifiableError({stack: 'fail'});}).should.throw();
});

it('should not allow overriding of name', function() {
(function() {new IdentifiableError({name: 'fail'});}).should.throw();
});
it('should not allow overriding of name', function() {
(function() {new IdentifiableError({name: 'fail'});}).should.throw();
});

it('should not allow overriding of code', function() {
(function() {new IdentifiableError({code: 601});}).should.throw();
});
it('should not allow overriding of code', function() {
(function() {new IdentifiableError({code: 601});}).should.throw();
});
});

describe('status code override', function() {
var CustomHttpError = errors.create({name: 'CustomHttpError', status: 409}),
err = new CustomHttpError();
var CustomHttpError = errors.create({name: 'CustomHttpError', status: 409}),
err = new CustomHttpError();

it('should have status of 409', function() {
err.status.should.equal(409);
});
it('should have status of 409', function() {
err.status.should.equal(409);
});

it('should include 409 status in toJSON()', function() {
err.toJSON().should.include({status: 409});
});
it('should include 409 status in toJSON()', function() {
err.toJSON().should.include({status: 409});
});

it('should allow overriding in constructor', function() {
new CustomHttpError({status:411}).status.should.equal(411);
});
it('should allow overriding in constructor', function() {
new CustomHttpError({status:411}).status.should.equal(411);
});
});

describe('errors.isError()', function(){
Expand All @@ -255,8 +260,8 @@ describe('errors.isError()', function(){
it('should return true for legit errors', function() {
errors.isError(legitError).should.be.ok;
});

it('should return false for other objects', function() {
errors.isError({}).should.not.be.ok;
});
});
});

0 comments on commit efab21b

Please sign in to comment.