Skip to content

Commit

Permalink
Merge pull request #41 from n1ru4l/feature-internal-data-prop
Browse files Browse the repository at this point in the history
Add internalData property
  • Loading branch information
thebigredgeek authored Apr 17, 2018
2 parents 106ec8f + 7145ffa commit 181c105
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const resolverThatThrowsError = (root, params, context) => {
throw new FooError({
data: {
something: 'important'
},
internalData: {
error: `The SQL server died.`
}
});
}
Expand All @@ -83,18 +86,39 @@ Witness glorious simplicity:
}
```

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.
You can utilize this data for logging purposes.

```js
import { isInstance as isApolloErrorInstance, formatError as formatApolloError } from 'apollo-errors';

function formatError(error) {
const { originialError } = error;
if (isApolloErrorInstance(originalError)) {
// log internalData to stdout but not include it in the formattedError
console.log(JSON.stringify({
type: `error`,
data: originalError.data,
internalData: originalError.internalData
}));
}
return formatApolloError(error)
}

```

## API

### ApolloError ({ [time_thrown: String, data: Object, message: String ]})
### ApolloError ({ [time_thrown: String, data: Object, internalData: object message: String ]})

Creates a new ApolloError object. Note that `ApolloError` in this context refers
to an error class created and returned by `createError` documented below. Error can be
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`).
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`).


### createError(name, {message: String, [data: Object, options: Object]}): ApolloError
### createError(name, {message: String, [data: Object, internalData: object, options: Object]}): ApolloError

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.
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.

#### Options (default):

Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ErrorConfig {
message: string;
time_thrown?: string;
data?: object;
internalData?: object;
options?: {
showPath?: boolean;
showLocations?: boolean;
Expand All @@ -28,6 +29,7 @@ export class ApolloError extends ExtendableError {
message: string;
time_thrown: string;
data: object;
internalData: object;
path: any;
locations: any;
_showLocations: boolean = false;
Expand All @@ -42,8 +44,11 @@ export class ApolloError extends ExtendableError {
const t = (ctorConfig && ctorConfig.time_thrown) || (config && config.time_thrown) || (new Date()).toISOString();
const m = (ctorConfig && ctorConfig.message) || (config && config.message) || '';
const ctorData = (ctorConfig && ctorConfig.data) || {};
const ctorInternalData = (ctorConfig && ctorConfig.internalData) || {}
const configData = (config && config.data) || {};
const configInternalData = (config && config.internalData) || {}
const d = { ...this.data, ...configData, ...ctorData };
const id = { ...this.internalData, ...configInternalData, ...ctorInternalData}
const ctorOptions = (ctorConfig && ctorConfig.options) || {};
const configOptions = (config && config.options) || {};
const opts = { ...configOptions, ...ctorOptions };
Expand All @@ -53,6 +58,7 @@ export class ApolloError extends ExtendableError {
this.message = m;
this.time_thrown = t;
this.data = d;
this.internalData = id;
this._showLocations = !!opts.showLocations;
this._showPath = !!opts.showPath;
}
Expand Down
17 changes: 17 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,21 @@ describe('formatError', () => {
});
});
});
context('error has internalData', () => {
it('does not include the internalData property', () => {
const FooError = createError('FooError', {
message: 'A foo error has occurred',
internalData: {
secret: 'SQL ERROR'
}
});

const e = new FooError();
expect(e.internalData).to.to.eql({
secret: 'SQL ERROR'
})
const s = formatError(e);
expect(s.internalData).to.eq(undefined)
})
})
});

0 comments on commit 181c105

Please sign in to comment.