diff --git a/package.json b/package.json index b9866c5..e8830d5 100644 --- a/package.json +++ b/package.json @@ -19,12 +19,12 @@ "main": "dist/index.js", "scripts": { "test": "npm run lint && npm run spec", - "prepublish": "./node_modules/.bin/babel -s -d dist src", + "prepublish": "./node_modules/.bin/babel -s -d dist src", "spec": "NODE_ENV=test ./node_modules/.bin/mocha --compilers js:babel-core/register -r adana-dump -R spec test/spec", "lint": "eslint --ignore-path .gitignore ." }, "dependencies": { - "babel-core": "^6.4.5" + "es6-error": "^4.0.0" }, "devDependencies": { "adana-cli": "^0.1.1", 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..a77a2ce --- /dev/null +++ b/src/invalid-header-error.js @@ -0,0 +1,3 @@ +import ExtendableError from 'es6-error'; + +export default class extends ExtendableError {} 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..db82fc9 --- /dev/null +++ b/test/spec/invalid-header-error.spec.js @@ -0,0 +1,12 @@ +import {expect} from 'chai'; +import InvalidHeaderError from '../../src/invalid-header-error'; + +describe('InvalidHeaderError', () => { + it('should inherit from Error', () => { + expect(new InvalidHeaderError()).to.be.an.instanceof(Error); + }); + + it('should set given message', () => { + expect(new InvalidHeaderError('foobar').message).to.equal('foobar'); + }); +}); 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', () => {