Skip to content

Commit

Permalink
Add and export InvalidHeaderError
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Gama committed Dec 5, 2016
1 parent d287f8b commit 2990601
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import InvalidHeaderError from './invalid-header-error';
import format from './format';
import parse from './parse';

export {format, parse};
export {InvalidHeaderError, format, parse};
10 changes: 10 additions & 0 deletions src/invalid-header-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const InvalidHeaderError = function InvalidHeaderError(message) {
this.name = 'InvalidHeaderError';
this.message = message;
this.stack = (new Error()).stack;
};

InvalidHeaderError.prototype = Object.create(TypeError.prototype);
InvalidHeaderError.prototype.constructor = InvalidHeaderError;

export default InvalidHeaderError;
5 changes: 3 additions & 2 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import InvalidHeaderError from './invalid-header-error';
import {isScheme, unquote} from './util';

// lol dis
Expand Down Expand Up @@ -34,14 +35,14 @@ const parseProperties = (scheme, string) => {

export default (str) => {
if (typeof str !== 'string') {
throw new TypeError('Header value must be a string.');
throw new InvalidHeaderError('Header value must be a string.');
}

const start = str.indexOf(' ');
const scheme = str.substr(0, start);

if (!isScheme(scheme)) {
throw new TypeError(`Invalid scheme ${scheme}`);
throw new InvalidHeaderError(`Invalid scheme ${scheme}`);
}

return parseProperties(scheme, str.substr(start));
Expand Down
16 changes: 16 additions & 0 deletions test/spec/invalid-header-error.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {expect} from 'chai';
import InvalidHeaderError from '../../src/invalid-header-error';

describe('InvalidHeaderError', () => {
it('should inherit from TypeError', () => {
expect(new InvalidHeaderError()).to.be.an.instanceof(TypeError);
});

it('should set given message', () => {
expect(new InvalidHeaderError('foobar').message).to.equal('foobar');
});

it('should set a stack trace', () => {
expect(new InvalidHeaderError().stack).to.not.be.undefined;
});
});
11 changes: 6 additions & 5 deletions test/spec/parse.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import {expect} from 'chai';
import InvalidHeaderError from '../../src/invalid-header-error';
import parse from '../../src/parse';

describe('parse', () => {
it('should fail on invalid schemes', () => {
expect(() => {
parse('@foo');
}).to.throw(TypeError);
parse('@ foo');
}).to.throw(InvalidHeaderError, 'Invalid scheme @');
});

it('should fail if a boolean', () => {
expect(() => {
parse(true);
}).to.throw(TypeError);
}).to.throw(InvalidHeaderError, 'Header value must be a string.');
});

it('should fail if an object', () => {
expect(() => {
parse({ });
}).to.throw(TypeError);
}).to.throw(InvalidHeaderError, 'Header value must be a string.');
});

it('should fail if null', () => {
expect(() => {
parse(null);
}).to.throw(TypeError);
}).to.throw(InvalidHeaderError, 'Header value must be a string.');
});

it('should coalesce many values', () => {
Expand Down

0 comments on commit 2990601

Please sign in to comment.