Skip to content

Commit 901d30d

Browse files
author
scf4
committed
Added option to show original locations and paths in error
1 parent b10f55f commit 901d30d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ class ApolloError extends ExtendableError {
1111
constructor (name, {
1212
message,
1313
time_thrown = (new Date()).toISOString(),
14-
data = {}
14+
data = {},
15+
showLocations,
16+
showPath,
1517
}) {
1618
const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;
1719
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
18-
20+
const l = showLocations && (arguments[2] && arguments[2].locations);
21+
const p = showPath && (arguments[2] && arguments[2].path);
1922
super(serializeName([
2023
name,
2124
t,
@@ -28,36 +31,45 @@ class ApolloError extends ExtendableError {
2831
this._humanized_message = message || '';
2932
this._time_thrown = t;
3033
this._data = d;
34+
this._locations = l;
35+
this._path = p;
3136
}
3237
serialize () {
3338
const name = this._name;
3439
const message = this._humanized_message;
3540
const time_thrown = this._time_thrown;
3641
const data = this._data;
42+
const locations = this._locations;
43+
const path = this._path;
3744
return {
3845
message,
3946
name,
4047
time_thrown,
41-
data
48+
data,
49+
locations,
50+
path,
4251
};
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', showLocations, showPath }) => {
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
};

0 commit comments

Comments
 (0)