Skip to content

Commit 181c105

Browse files
Merge pull request #41 from n1ru4l/feature-internal-data-prop
Add internalData property
2 parents 106ec8f + 7145ffa commit 181c105

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

README.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const resolverThatThrowsError = (root, params, context) => {
5858
throw new FooError({
5959
data: {
6060
something: 'important'
61+
},
62+
internalData: {
63+
error: `The SQL server died.`
6164
}
6265
});
6366
}
@@ -83,18 +86,39 @@ Witness glorious simplicity:
8386
}
8487
```
8588

89+
The `internalData` property is meant for data you want to store on the error object (e.g. for logging), but not send out to your end users.
90+
You can utilize this data for logging purposes.
91+
92+
```js
93+
import { isInstance as isApolloErrorInstance, formatError as formatApolloError } from 'apollo-errors';
94+
95+
function formatError(error) {
96+
const { originialError } = error;
97+
if (isApolloErrorInstance(originalError)) {
98+
// log internalData to stdout but not include it in the formattedError
99+
console.log(JSON.stringify({
100+
type: `error`,
101+
data: originalError.data,
102+
internalData: originalError.internalData
103+
}));
104+
}
105+
return formatApolloError(error)
106+
}
107+
108+
```
109+
86110
## API
87111

88-
### ApolloError ({ [time_thrown: String, data: Object, message: String ]})
112+
### ApolloError ({ [time_thrown: String, data: Object, internalData: object message: String ]})
89113

90114
Creates a new ApolloError object. Note that `ApolloError` in this context refers
91115
to an error class created and returned by `createError` documented below. Error can be
92-
initialized with a custom `time_thrown` ISODate (default is current ISODate), `data` object (which will be merged with data specified through `createError`, if it exists), and `message` (which will override the message specified through `createError`).
116+
initialized with a custom `time_thrown` ISODate (default is current ISODate), `data` object (which will be merged with data specified through `createError`, if it exists), `internalData` object (which will be merged with internalData specified trough `createError`) and `message` (which will override the message specified through `createError`).
93117

94118

95-
### createError(name, {message: String, [data: Object, options: Object]}): ApolloError
119+
### createError(name, {message: String, [data: Object, internalData: object, options: Object]}): ApolloError
96120

97-
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.
121+
Creates and returns an error class with the given `name` and `message`, optionally initialized with the given `data`, `internalData` and `options`. `data` and `internalData` passed to `createError` will later be merged with any data passed to the constructor.
98122

99123
#### Options (default):
100124

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ErrorConfig {
88
message: string;
99
time_thrown?: string;
1010
data?: object;
11+
internalData?: object;
1112
options?: {
1213
showPath?: boolean;
1314
showLocations?: boolean;
@@ -28,6 +29,7 @@ export class ApolloError extends ExtendableError {
2829
message: string;
2930
time_thrown: string;
3031
data: object;
32+
internalData: object;
3133
path: any;
3234
locations: any;
3335
_showLocations: boolean = false;
@@ -42,8 +44,11 @@ export class ApolloError extends ExtendableError {
4244
const t = (ctorConfig && ctorConfig.time_thrown) || (config && config.time_thrown) || (new Date()).toISOString();
4345
const m = (ctorConfig && ctorConfig.message) || (config && config.message) || '';
4446
const ctorData = (ctorConfig && ctorConfig.data) || {};
47+
const ctorInternalData = (ctorConfig && ctorConfig.internalData) || {}
4548
const configData = (config && config.data) || {};
49+
const configInternalData = (config && config.internalData) || {}
4650
const d = { ...this.data, ...configData, ...ctorData };
51+
const id = { ...this.internalData, ...configInternalData, ...ctorInternalData}
4752
const ctorOptions = (ctorConfig && ctorConfig.options) || {};
4853
const configOptions = (config && config.options) || {};
4954
const opts = { ...configOptions, ...ctorOptions };
@@ -53,6 +58,7 @@ export class ApolloError extends ExtendableError {
5358
this.message = m;
5459
this.time_thrown = t;
5560
this.data = d;
61+
this.internalData = id;
5662
this._showLocations = !!opts.showLocations;
5763
this._showPath = !!opts.showPath;
5864
}

test/spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,21 @@ describe('formatError', () => {
137137
});
138138
});
139139
});
140+
context('error has internalData', () => {
141+
it('does not include the internalData property', () => {
142+
const FooError = createError('FooError', {
143+
message: 'A foo error has occurred',
144+
internalData: {
145+
secret: 'SQL ERROR'
146+
}
147+
});
148+
149+
const e = new FooError();
150+
expect(e.internalData).to.to.eql({
151+
secret: 'SQL ERROR'
152+
})
153+
const s = formatError(e);
154+
expect(s.internalData).to.eq(undefined)
155+
})
156+
})
140157
});

0 commit comments

Comments
 (0)