From 2990601138e3d81d532383e44d061371111ccfed Mon Sep 17 00:00:00 2001 From: Ricardo Gama Date: Mon, 5 Dec 2016 11:25:23 +0000 Subject: [PATCH] Add and export InvalidHeaderError --- src/index.js | 3 ++- src/invalid-header-error.js | 10 ++++++++++ src/parse.js | 5 +++-- test/spec/invalid-header-error.spec.js | 16 ++++++++++++++++ test/spec/parse.spec.js | 11 ++++++----- 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/invalid-header-error.js create mode 100644 test/spec/invalid-header-error.spec.js diff --git a/src/index.js b/src/index.js index ca13086..9658dbd 100644 --- a/src/index.js +++ b/src/index.js @@ -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}; diff --git a/src/invalid-header-error.js b/src/invalid-header-error.js new file mode 100644 index 0000000..1188ff1 --- /dev/null +++ b/src/invalid-header-error.js @@ -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; diff --git a/src/parse.js b/src/parse.js index 54b7d91..3b2f92f 100644 --- a/src/parse.js +++ b/src/parse.js @@ -1,3 +1,4 @@ +import InvalidHeaderError from './invalid-header-error'; import {isScheme, unquote} from './util'; // lol dis @@ -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)); diff --git a/test/spec/invalid-header-error.spec.js b/test/spec/invalid-header-error.spec.js new file mode 100644 index 0000000..8233973 --- /dev/null +++ b/test/spec/invalid-header-error.spec.js @@ -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; + }); +}); diff --git a/test/spec/parse.spec.js b/test/spec/parse.spec.js index 488de15..cc9ab6d 100644 --- a/test/spec/parse.spec.js +++ b/test/spec/parse.spec.js @@ -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', () => {