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 21, 2016
1 parent d287f8b commit 81ad6cc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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};
3 changes: 3 additions & 0 deletions src/invalid-header-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ExtendableError from 'es6-error';

export default class extends ExtendableError {}
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
12 changes: 12 additions & 0 deletions test/spec/invalid-header-error.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
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 81ad6cc

Please sign in to comment.