Skip to content

Commit

Permalink
Merge pull request #1 from scf4/master
Browse files Browse the repository at this point in the history
  • Loading branch information
thebigredgeek authored Dec 1, 2016
2 parents b10f55f + 8f437c0 commit d5ed772
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ to an error class created and returned by `createError` documented below. Error
initialized with a custom `time_thrown` ISODate (default is current ISODate) and `data` object (which will be merged with data specified through `createError`, if it exists).


### createError(name, {message: String, [data: Object]}): ApolloError
### createError(name, {message: String, [data: Object, options: Object]}): ApolloError

Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data`. `data` passed to `createError` will later be merged with any data passed to the constructor.
Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data` and `options`. `data` passed to `createError` will later be merged with any data passed to the constructor.

#### Options (default):

- `showPath` *(false)*: Preserve the GraphQLError `path` data.
- `showLocations` *(false)*: Preserve the GraphQLError `locations` data.

### formatError (error, strict = false): ApolloError|Error|null
If the error is a known ApolloError, returns the serialized form of said error.
Expand Down
23 changes: 19 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.

24 changes: 18 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,65 @@ class ApolloError extends ExtendableError {
constructor (name, {
message,
time_thrown = (new Date()).toISOString(),
data = {}
data = {},
options = {},
}) {
const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));

super(serializeName([
name,
t,
Object.assign({}, d, {
toString: () => JSON.stringify(d)
})
}),
]));

this._name = name;
this._humanized_message = message || '';
this._time_thrown = t;
this._data = d;
this._locations = (opts.showLocations && arguments[2] && arguments[2].locations)
this._path = (opts.showPath && arguments[2] && arguments[2].path);
}
serialize () {
const name = this._name;
const message = this._humanized_message;
const time_thrown = this._time_thrown;
const data = this._data;
return {
const locations = this._locations;
const path = this._path;
let error = {
message,
name,
time_thrown,
data
data,
};
if (locations) error.locations = locations;
if (path) error.path = path;
return error;
}
}

export const createError = (name, data = { message: 'An error has occurred' }) => {
export const createError = (name, data = { message: 'An error has occurred', options }) => {
const e = ApolloError.bind(null, name, data);
errorMap.set(name, e);
return e;
};

export const formatError = (originalError, returnNull = false) => {
const [ name, thrown_at, d ] = deserializeName(originalError.message);
const { locations, path } = originalError;
const data = d !== undefined ? JSON.parse(d) : {};
if (!name) return returnNull ? null : originalError;
const CustomError = errorMap.get(name);
if (!CustomError) return returnNull ? null : originalError;
const error = new CustomError({
thrown_at,
data
data,
locations,
path,
});
return error.serialize();
};
12 changes: 10 additions & 2 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ describe('createError', () => {
message: 'A foo error has occurred',
data: {
hello: 'world'
}
},
options: {
showLocations: false,
showPath: true,
},
});

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

const e = new FooError({
data: {
foo: 'bar'
}
},
options: {
showLocations: true,
showPath: false,
},
});

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

0 comments on commit d5ed772

Please sign in to comment.