From 70eebc466fdb8f992f51be75c4a05e777bc041c6 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Thu, 12 Sep 2019 12:52:44 -0400 Subject: [PATCH] Remove unhelpful and slightly buggy enhanceErrorWithDocument function. Extending the built-in `Error` class in TypeScript results in a subclass whose `.prototype` is simply `Error.prototype`, rather than (in this case) `WriteError.prototype`, so this code never worked as intended, because `new WriteError(message) instanceof WriteError` would never be true. Relevant discussion (content warning: maintainers refusing to fix deviations from the ECMAScript specification for reasons that are not even remotely compelling): https://github.com/microsoft/TypeScript/issues/12123 https://github.com/microsoft/TypeScript/issues/12581 More subjectively (IMHO), the additional messaging did not ease debugging enough to justify its contribution to bundle sizes. --- .../__tests__/__snapshots__/roundtrip.ts.snap | 6 +-- .../__snapshots__/writeToStore.ts.snap | 6 +-- src/cache/inmemory/__tests__/writeToStore.ts | 2 +- src/cache/inmemory/writeToStore.ts | 46 ++++++------------- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/cache/inmemory/__tests__/__snapshots__/roundtrip.ts.snap b/src/cache/inmemory/__tests__/__snapshots__/roundtrip.ts.snap index 404860212e7..9886a50fb32 100644 --- a/src/cache/inmemory/__tests__/__snapshots__/roundtrip.ts.snap +++ b/src/cache/inmemory/__tests__/__snapshots__/roundtrip.ts.snap @@ -1,7 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = ` -"Error writing result to store for query: - {\\"kind\\":\\"Document\\",\\"definitions\\":[{\\"kind\\":\\"OperationDefinition\\",\\"operation\\":\\"query\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Failure\\"},\\"variableDefinitions\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"item\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"stringField\\"},\\"arguments\\":[],\\"directives\\":[]}]}}]}}],\\"loc\\":{\\"start\\":0,\\"end\\":106}} -Store error: the application attempted to write an object with no provided id but the store already contains an id of abcd for this object." -`; +exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = `"Store error: the application attempted to write an object with no provided id but the store already contains an id of abcd for this object."`; diff --git a/src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap b/src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap index 404860212e7..9886a50fb32 100644 --- a/src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap +++ b/src/cache/inmemory/__tests__/__snapshots__/writeToStore.ts.snap @@ -1,7 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = ` -"Error writing result to store for query: - {\\"kind\\":\\"Document\\",\\"definitions\\":[{\\"kind\\":\\"OperationDefinition\\",\\"operation\\":\\"query\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"Failure\\"},\\"variableDefinitions\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"item\\"},\\"arguments\\":[],\\"directives\\":[],\\"selectionSet\\":{\\"kind\\":\\"SelectionSet\\",\\"selections\\":[{\\"kind\\":\\"Field\\",\\"name\\":{\\"kind\\":\\"Name\\",\\"value\\":\\"stringField\\"},\\"arguments\\":[],\\"directives\\":[]}]}}]}}],\\"loc\\":{\\"start\\":0,\\"end\\":106}} -Store error: the application attempted to write an object with no provided id but the store already contains an id of abcd for this object." -`; +exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = `"Store error: the application attempted to write an object with no provided id but the store already contains an id of abcd for this object."`; diff --git a/src/cache/inmemory/__tests__/writeToStore.ts b/src/cache/inmemory/__tests__/writeToStore.ts index 9a480b98bfb..40164cafdf3 100644 --- a/src/cache/inmemory/__tests__/writeToStore.ts +++ b/src/cache/inmemory/__tests__/writeToStore.ts @@ -1660,7 +1660,7 @@ describe('writing to the store', () => { }, dataIdFromObject: getIdField, }); - }).toThrowError(/stringField(.|\n)*abcd/g); + }).toThrowError(/contains an id of abcd/g); }); it('properly handles the connection directive', () => { diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index 20944d11add..1c8bf29c9d4 100644 --- a/src/cache/inmemory/writeToStore.ts +++ b/src/cache/inmemory/writeToStore.ts @@ -27,20 +27,6 @@ import { } from './helpers'; import { defaultDataIdFromObject } from './inMemoryCache'; -export class WriteError extends Error { - public type = 'WriteError'; -} - -export function enhanceErrorWithDocument(error: Error, document: DocumentNode) { - // XXX A bit hacky maybe ... - const enhancedError = new WriteError( - `Error writing result to store for query:\n ${JSON.stringify(document)}`, - ); - enhancedError.message += '\n' + error.message; - enhancedError.stack = error.stack; - return enhancedError; -} - export type WriteContext = { readonly store: NormalizedCache; readonly processedData: { [x: string]: Set }; @@ -90,25 +76,21 @@ export class StoreWriter { dataIdFromObject?: IdGetter; }): NormalizedCache { const operationDefinition = getOperationDefinition(query)!; - try { - return this.writeSelectionSetToStore({ - result, - dataId, - selectionSet: operationDefinition.selectionSet, - context: { - store, - processedData: {}, - variables: { - ...getDefaultValues(operationDefinition), - ...variables, - }, - dataIdFromObject, - fragmentMap: createFragmentMap(getFragmentDefinitions(query)), + return this.writeSelectionSetToStore({ + result, + dataId, + selectionSet: operationDefinition.selectionSet, + context: { + store, + processedData: {}, + variables: { + ...getDefaultValues(operationDefinition), + ...variables, }, - }); - } catch (e) { - throw enhanceErrorWithDocument(e, query); - } + dataIdFromObject, + fragmentMap: createFragmentMap(getFragmentDefinitions(query)), + }, + }); } public writeSelectionSetToStore({