Skip to content

Commit d5ed772

Browse files
Merge pull request #1 from scf4/master
2 parents b10f55f + 8f437c0 commit d5ed772

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ to an error class created and returned by `createError` documented below. Error
8888
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).
8989

9090

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

93-
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.
93+
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.
94+
95+
#### Options (default):
96+
97+
- `showPath` *(false)*: Preserve the GraphQLError `path` data.
98+
- `showLocations` *(false)*: Preserve the GraphQLError `locations` data.
9499

95100
### formatError (error, strict = false): ApolloError|Error|null
96101
If the error is a known ApolloError, returns the serialized form of said error.

dist/index.js

Lines changed: 19 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.

src/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,65 @@ class ApolloError extends ExtendableError {
1111
constructor (name, {
1212
message,
1313
time_thrown = (new Date()).toISOString(),
14-
data = {}
14+
data = {},
15+
options = {},
1516
}) {
1617
const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;
1718
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
19+
const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));
1820

1921
super(serializeName([
2022
name,
2123
t,
2224
Object.assign({}, d, {
2325
toString: () => JSON.stringify(d)
24-
})
26+
}),
2527
]));
2628

2729
this._name = name;
2830
this._humanized_message = message || '';
2931
this._time_thrown = t;
3032
this._data = d;
33+
this._locations = (opts.showLocations && arguments[2] && arguments[2].locations)
34+
this._path = (opts.showPath && arguments[2] && arguments[2].path);
3135
}
3236
serialize () {
3337
const name = this._name;
3438
const message = this._humanized_message;
3539
const time_thrown = this._time_thrown;
3640
const data = this._data;
37-
return {
41+
const locations = this._locations;
42+
const path = this._path;
43+
let error = {
3844
message,
3945
name,
4046
time_thrown,
41-
data
47+
data,
4248
};
49+
if (locations) error.locations = locations;
50+
if (path) error.path = path;
51+
return error;
4352
}
4453
}
4554

46-
export const createError = (name, data = { message: 'An error has occurred' }) => {
55+
export const createError = (name, data = { message: 'An error has occurred', options }) => {
4756
const e = ApolloError.bind(null, name, data);
4857
errorMap.set(name, e);
4958
return e;
5059
};
5160

5261
export const formatError = (originalError, returnNull = false) => {
5362
const [ name, thrown_at, d ] = deserializeName(originalError.message);
63+
const { locations, path } = originalError;
5464
const data = d !== undefined ? JSON.parse(d) : {};
5565
if (!name) return returnNull ? null : originalError;
5666
const CustomError = errorMap.get(name);
5767
if (!CustomError) return returnNull ? null : originalError;
5868
const error = new CustomError({
5969
thrown_at,
60-
data
70+
data,
71+
locations,
72+
path,
6173
});
6274
return error.serialize();
6375
};

test/spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ describe('createError', () => {
88
message: 'A foo error has occurred',
99
data: {
1010
hello: 'world'
11-
}
11+
},
12+
options: {
13+
showLocations: false,
14+
showPath: true,
15+
},
1216
});
1317

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

1620
const e = new FooError({
1721
data: {
1822
foo: 'bar'
19-
}
23+
},
24+
options: {
25+
showLocations: true,
26+
showPath: false,
27+
},
2028
});
2129

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

0 commit comments

Comments
 (0)