Skip to content

Commit

Permalink
fixes for createError
Browse files Browse the repository at this point in the history
  • Loading branch information
ch-andrewrhyne committed Aug 29, 2017
1 parent bbacf37 commit 5c45afc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ jspm_packages

# Yarn
yarn.lock

# Direnv
.envrc
19 changes: 15 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-errors",
"version": "1.4.0",
"version": "1.5.0",
"description": "Machine-readable custom errors for Apollostack's GraphQL server",
"main": "dist/index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import assert from 'assert';
import ExtendableError from 'es6-error';

const isString = d => Object.prototype.toString.call(d) === '[object String]';
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';

class ApolloError extends ExtendableError {
constructor (name, {
message,
Expand Down Expand Up @@ -41,8 +45,10 @@ class ApolloError extends ExtendableError {

export const isInstance = e => e instanceof ApolloError;

export const createError = (name, data = { message: 'An error has occurred', options }) => {
const e = ApolloError.bind(null, name, data);
export const createError = (name, config) => {
assert(isObject(config), 'createError requires a config object as the second parameter');
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
const e = ApolloError.bind(null, name, config);
return e;
};

Expand Down
84 changes: 55 additions & 29 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,65 @@ import { expect } from 'chai';
import { createError, formatError } from '../dist';

describe('createError', () => {
it('returns an error that serializes properly', () => {
const FooError = createError('FooError', {
message: 'A foo error has occurred',
data: {
hello: 'world'
},
options: {
showLocations: false,
showPath: true,
},
});
context('when properly used', () => {
it('returns an error that serializes properly', () => {
const FooError = createError('FooError', {
message: 'A foo error has occurred',
data: {
hello: 'world'
},
options: {
showLocations: false,
showPath: true,
},
});

const iso = new Date().toISOString();
const iso = new Date().toISOString();

const e = new FooError({
message: 'A foo 2.0 error has occurred',
data: {
foo: 'bar'
},
options: {
showLocations: true,
showPath: false,
},
});
const e = new FooError({
message: 'A foo 2.0 error has occurred',
data: {
foo: 'bar'
},
options: {
showLocations: true,
showPath: false,
},
});

const { message, name, time_thrown, data } = e.serialize();
const { message, name, time_thrown, data } = e.serialize();

expect(message).to.equal('A foo 2.0 error has occurred');
expect(name).to.equal('FooError');
expect(time_thrown).to.equal(e.time_thrown);
expect(data).to.eql({
hello: 'world',
foo: 'bar'
expect(message).to.equal('A foo 2.0 error has occurred');
expect(name).to.equal('FooError');
expect(time_thrown).to.equal(e.time_thrown);
expect(data).to.eql({
hello: 'world',
foo: 'bar'
});
});
});
context('when missing a config as the second parameter', () => {
it('throws an assertion error with a useful message', () => {
try {
createError('FooError');
throw new Error('did not throw as expected');
} catch (err) {
expect(err.name).to.equal('AssertionError [ERR_ASSERTION]');
expect(err.message).to.equal('createError requires a config object as the second parameter');
}
});
});
context('when missing a message from the config object passed as the second parameter', () => {
it('throws an assertion error with a useful message', () => {
try {
createError('FooError', {

});
throw new Error('did not throw as expected');
} catch (err) {
expect(err.name).to.equal('AssertionError [ERR_ASSERTION]');
expect(err.message).to.equal('createError requires a "message" property on the config object passed as the second parameter')
}
});
});
});
Expand Down

0 comments on commit 5c45afc

Please sign in to comment.