Skip to content

Commit 87e90fc

Browse files
author
scf4
committed
options
1 parent 901d30d commit 87e90fc

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

dist/index.js

Lines changed: 18 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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,34 @@ class ApolloError extends ExtendableError {
1212
message,
1313
time_thrown = (new Date()).toISOString(),
1414
data = {},
15-
showLocations,
16-
showPath,
15+
options = {},
1716
}) {
1817
const t = (arguments[2] && arguments[2].thrown_at) || time_thrown;
1918
const d = Object.assign({}, data, ((arguments[2] && arguments[2].data) || {}));
20-
const l = showLocations && (arguments[2] && arguments[2].locations);
21-
const p = showPath && (arguments[2] && arguments[2].path);
19+
const opts = Object.assign({}, options, ((arguments[2] && arguments[2].options) || {}));
20+
2221
super(serializeName([
2322
name,
2423
t,
2524
Object.assign({}, d, {
2625
toString: () => JSON.stringify(d)
27-
})
26+
}),
2827
]));
2928

3029
this._name = name;
3130
this._humanized_message = message || '';
3231
this._time_thrown = t;
3332
this._data = d;
34-
this._locations = l;
35-
this._path = p;
33+
this._locations = (opts.showLocations && arguments[2] && arguments[2].locations)
34+
this._path = (opts.showPath && arguments[2] && arguments[2].path);
3635
}
3736
serialize () {
3837
const name = this._name;
3938
const message = this._humanized_message;
4039
const time_thrown = this._time_thrown;
4140
const data = this._data;
42-
const locations = this._locations;
43-
const path = this._path;
41+
const locations = this._locations || undefined;
42+
const path = this._path || undefined;
4443
return {
4544
message,
4645
name,
@@ -52,7 +51,7 @@ class ApolloError extends ExtendableError {
5251
}
5352
}
5453

55-
export const createError = (name, data = { message: 'An error has occurred', showLocations, showPath }) => {
54+
export const createError = (name, data = { message: 'An error has occurred', options }) => {
5655
const e = ApolloError.bind(null, name, data);
5756
errorMap.set(name, e);
5857
return e;

test/spec.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,36 @@ 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

22-
const { message, name, time_thrown, data } = e.serialize();
23-
30+
const { message, name, time_thrown, data, locations, path } = e.serialize();
31+
2432
expect(message).to.equal('A foo error has occurred');
2533
expect(name).to.equal('FooError');
2634
expect(time_thrown).to.equal(e._time_thrown);
2735
expect(data).to.eql({
2836
hello: 'world',
2937
foo: 'bar'
3038
});
39+
expect(locations).to.be.undefined;
40+
expect(path).to.be.undefined;
3141
});
3242
});
3343

0 commit comments

Comments
 (0)