Skip to content

Commit fbac46b

Browse files
committed
Adds interface for config
1 parent 7e3af95 commit fbac46b

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

dist/index.js

Lines changed: 14 additions & 7 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.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"homepage": "https://github.com/thebigredgeek/apollo-errors#readme",
2828
"dependencies": {
2929
"assert": "^1.4.1",
30-
"extendable-error": "^0.1.5"
30+
"extendable-error": "^0.1.5",
31+
"install": "^0.10.1"
3132
},
3233
"devDependencies": {
3334
"babel-cli": "^6.18.0",

src/index.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as assert from 'assert';
22
import ExtendableError from 'extendable-error';
3-
43
const isString = d => Object.prototype.toString.call(d) === '[object String]';
54
const isObject = d => Object.prototype.toString.call(d) === '[object Object]';
65
interface ErrorConfig {
@@ -9,7 +8,6 @@ interface ErrorConfig {
98
data: any,
109
options: any,
1110
}
12-
1311
class ApolloError extends ExtendableError {
1412
name: string;
1513
message: string;
@@ -19,12 +17,12 @@ class ApolloError extends ExtendableError {
1917
locations: any;
2018
_showLocations: any;
2119
constructor (name, config: ErrorConfig) {
22-
const t = (config && config.time_thrown) || (new Date()).toISOString();
23-
const d = (config && config.data || {})
24-
const m = (config && config.message) || '';
25-
const opts = ((config && config.options) || {})
26-
27-
super(m);
20+
super((config && config.message) || '');
21+
const t = (arguments[2] && arguments[2].time_thrown) || (new Date()).toISOString();
22+
const m = (arguments[2] && arguments[2].message) || '';
23+
const configData = (arguments[2] && arguments[2].data) || {};
24+
const d = {...this.data, ...configData}
25+
const opts = ((arguments[2] && arguments[2].options) || {})
2826

2927
this.name = name;
3028
this.message = m;
@@ -43,20 +41,17 @@ class ApolloError extends ExtendableError {
4341
path,
4442
locations
4543
};
46-
4744
if (_showLocations) {
4845
error.locations = locations;
4946
error.path = path;
5047
}
51-
5248
return error;
5349
}
5450
}
5551

5652
export const isInstance = e => e instanceof ApolloError;
5753

5854
export const createError = (name, config: ErrorConfig) => {
59-
console.log('config', config);
6055
assert(isObject(config), 'createError requires a config object as the second parameter');
6156
assert(isString(config.message), 'createError requires a "message" property on the config object passed as the second parameter');
6257
const e = ApolloError.bind(null, name, config);

test/spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@ describe('createError', () => {
1515
},
1616
options: {
1717
showLocations: false,
18-
showPath: true,
18+
showPath: false,
1919
},
2020
});
2121
const iso = new Date().toISOString();
22-
2322
const e = new FooError({
2423
message: 'A foo 2.0 error has occurred',
2524
data: {
25+
hello: 'world',
2626
foo: 'bar'
2727
},
2828
options: {
2929
showLocations: true,
30-
showPath: false,
30+
showPath: true,
3131
},
3232
});
33-
const { message, name, time_thrown, data } = e.serialize();
3433

34+
const { message, name, time_thrown, data } = e.serialize();
3535
expect(message).to.equal('A foo 2.0 error has occurred');
3636
expect(name).to.equal('FooError');
3737
expect(time_thrown).to.equal(e.time_thrown);
3838
expect(data).to.eql({
39+
hello: 'world',
3940
foo: 'bar'
4041
});
4142
});
@@ -86,7 +87,6 @@ describe('formatError', () => {
8687
oh: 'shit'
8788
}
8889
});
89-
9090
const s = formatError({
9191
originalError: e
9292
}, false);

0 commit comments

Comments
 (0)