Skip to content

Commit 5c45afc

Browse files
fixes for createError
1 parent bbacf37 commit 5c45afc

File tree

6 files changed

+83
-37
lines changed

6 files changed

+83
-37
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ jspm_packages
3838

3939
# Yarn
4040
yarn.lock
41+
42+
# Direnv
43+
.envrc

dist/index.js

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apollo-errors",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
55
"main": "dist/index.js",
66
"scripts": {

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import assert from 'assert';
12
import ExtendableError from 'es6-error';
23

4+
const isString = d => Object.prototype.toString.call(d) === '[object String]';
5+
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';
6+
37
class ApolloError extends ExtendableError {
48
constructor (name, {
59
message,
@@ -41,8 +45,10 @@ class ApolloError extends ExtendableError {
4145

4246
export const isInstance = e => e instanceof ApolloError;
4347

44-
export const createError = (name, data = { message: 'An error has occurred', options }) => {
45-
const e = ApolloError.bind(null, name, data);
48+
export const createError = (name, config) => {
49+
assert(isObject(config), 'createError requires a config object as the second parameter');
50+
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
51+
const e = ApolloError.bind(null, name, config);
4652
return e;
4753
};
4854

test/spec.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,65 @@ import { expect } from 'chai';
33
import { createError, formatError } from '../dist';
44

55
describe('createError', () => {
6-
it('returns an error that serializes properly', () => {
7-
const FooError = createError('FooError', {
8-
message: 'A foo error has occurred',
9-
data: {
10-
hello: 'world'
11-
},
12-
options: {
13-
showLocations: false,
14-
showPath: true,
15-
},
16-
});
6+
context('when properly used', () => {
7+
it('returns an error that serializes properly', () => {
8+
const FooError = createError('FooError', {
9+
message: 'A foo error has occurred',
10+
data: {
11+
hello: 'world'
12+
},
13+
options: {
14+
showLocations: false,
15+
showPath: true,
16+
},
17+
});
1718

18-
const iso = new Date().toISOString();
19+
const iso = new Date().toISOString();
1920

20-
const e = new FooError({
21-
message: 'A foo 2.0 error has occurred',
22-
data: {
23-
foo: 'bar'
24-
},
25-
options: {
26-
showLocations: true,
27-
showPath: false,
28-
},
29-
});
21+
const e = new FooError({
22+
message: 'A foo 2.0 error has occurred',
23+
data: {
24+
foo: 'bar'
25+
},
26+
options: {
27+
showLocations: true,
28+
showPath: false,
29+
},
30+
});
3031

31-
const { message, name, time_thrown, data } = e.serialize();
32+
const { message, name, time_thrown, data } = e.serialize();
3233

33-
expect(message).to.equal('A foo 2.0 error has occurred');
34-
expect(name).to.equal('FooError');
35-
expect(time_thrown).to.equal(e.time_thrown);
36-
expect(data).to.eql({
37-
hello: 'world',
38-
foo: 'bar'
34+
expect(message).to.equal('A foo 2.0 error has occurred');
35+
expect(name).to.equal('FooError');
36+
expect(time_thrown).to.equal(e.time_thrown);
37+
expect(data).to.eql({
38+
hello: 'world',
39+
foo: 'bar'
40+
});
41+
});
42+
});
43+
context('when missing a config as the second parameter', () => {
44+
it('throws an assertion error with a useful message', () => {
45+
try {
46+
createError('FooError');
47+
throw new Error('did not throw as expected');
48+
} catch (err) {
49+
expect(err.name).to.equal('AssertionError [ERR_ASSERTION]');
50+
expect(err.message).to.equal('createError requires a config object as the second parameter');
51+
}
52+
});
53+
});
54+
context('when missing a message from the config object passed as the second parameter', () => {
55+
it('throws an assertion error with a useful message', () => {
56+
try {
57+
createError('FooError', {
58+
59+
});
60+
throw new Error('did not throw as expected');
61+
} catch (err) {
62+
expect(err.name).to.equal('AssertionError [ERR_ASSERTION]');
63+
expect(err.message).to.equal('createError requires a "message" property on the config object passed as the second parameter')
64+
}
3965
});
4066
});
4167
});

0 commit comments

Comments
 (0)