From 94b3502842f9fba0b8469c04f3ce9a11fb66bb8f Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Wed, 11 Apr 2018 15:48:16 +0200 Subject: [PATCH 1/3] Implement internalData property --- src/index.ts | 1 + test/spec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/index.ts b/src/index.ts index 180ffc7..71bf3a6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ export interface ErrorConfig { message: string; time_thrown?: string; data?: object; + internalData?: object; options?: { showPath?: boolean; showLocations?: boolean; diff --git a/test/spec.js b/test/spec.js index 74c07ae..ac2feb6 100644 --- a/test/spec.js +++ b/test/spec.js @@ -137,4 +137,18 @@ 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(); + const s = formatError(e); + expect(s.internalData).to.eq(undefined) + }) + }) }); From a710b537f3d7233e85ada580d56dbdd7e4efb0be Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Wed, 11 Apr 2018 16:04:50 +0200 Subject: [PATCH 2/3] Ensure internalData property is actually present on the error instance --- src/index.ts | 5 +++++ test/spec.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/index.ts b/src/index.ts index 71bf3a6..683c776 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,7 @@ export class ApolloError extends ExtendableError { message: string; time_thrown: string; data: object; + internalData: object; path: any; locations: any; _showLocations: boolean = false; @@ -43,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 }; @@ -54,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; } diff --git a/test/spec.js b/test/spec.js index ac2feb6..4b2df72 100644 --- a/test/spec.js +++ b/test/spec.js @@ -147,6 +147,9 @@ describe('formatError', () => { }); const e = new FooError(); + expect(e.internalData).to.to.eql({ + secret: 'SQL ERROR' + }) const s = formatError(e); expect(s.internalData).to.eq(undefined) }) From 7145ffa3c58e811c2ae4392858cd672d48a28222 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Fri, 13 Apr 2018 09:33:42 +0200 Subject: [PATCH 3/3] Add documentation for internalData property --- README.md | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b06322..67744e9 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ const resolverThatThrowsError = (root, params, context) => { throw new FooError({ data: { something: 'important' + }, + internalData: { + error: `The SQL server died.` } }); } @@ -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):