Skip to content

Added option to show original locations and path in error #1

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

Merged
merged 6 commits into from
Dec 1, 2016
Merged
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
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using arguments here rather than opts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a truthy check of the opts. The line was initially below const t = (arguments[2] && arguments[2]... and I was keeping it brief, but I'll make the code a little clearer tomorrow!

The arguments come from the originalError in formatError.

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,
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test formatError to make sure things are serializing properly

});

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