Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and export InvalidHeaderError #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"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