Skip to content

Commit 8a42418

Browse files
benjamnStephen Barlow
authored and
Stephen Barlow
committed
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): microsoft/TypeScript#12123 microsoft/TypeScript#12581 More subjectively (IMHO), the additional messaging did not ease debugging enough to justify its contribution to bundle sizes.
1 parent f9bfc7f commit 8a42418

File tree

4 files changed

+17
-43
lines changed

4 files changed

+17
-43
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = `
4-
"Error writing result to store for query:
5-
{\\"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}}
6-
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."
7-
`;
3+
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."`;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`writing to the store throws when trying to write an object without id that was previously queried with id 1`] = `
4-
"Error writing result to store for query:
5-
{\\"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}}
6-
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."
7-
`;
3+
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."`;

src/cache/inmemory/__tests__/writeToStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ describe('writing to the store', () => {
16601660
},
16611661
dataIdFromObject: getIdField,
16621662
});
1663-
}).toThrowError(/stringField(.|\n)*abcd/g);
1663+
}).toThrowError(/contains an id of abcd/g);
16641664
});
16651665

16661666
it('properly handles the connection directive', () => {

src/cache/inmemory/writeToStore.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ import {
2727
} from './helpers';
2828
import { defaultDataIdFromObject } from './inMemoryCache';
2929

30-
export class WriteError extends Error {
31-
public type = 'WriteError';
32-
}
33-
34-
export function enhanceErrorWithDocument(error: Error, document: DocumentNode) {
35-
// XXX A bit hacky maybe ...
36-
const enhancedError = new WriteError(
37-
`Error writing result to store for query:\n ${JSON.stringify(document)}`,
38-
);
39-
enhancedError.message += '\n' + error.message;
40-
enhancedError.stack = error.stack;
41-
return enhancedError;
42-
}
43-
4430
export type WriteContext = {
4531
readonly store: NormalizedCache;
4632
readonly processedData: { [x: string]: Set<FieldNode> };
@@ -90,25 +76,21 @@ export class StoreWriter {
9076
dataIdFromObject?: IdGetter;
9177
}): NormalizedCache {
9278
const operationDefinition = getOperationDefinition(query)!;
93-
try {
94-
return this.writeSelectionSetToStore({
95-
result,
96-
dataId,
97-
selectionSet: operationDefinition.selectionSet,
98-
context: {
99-
store,
100-
processedData: {},
101-
variables: {
102-
...getDefaultValues(operationDefinition),
103-
...variables,
104-
},
105-
dataIdFromObject,
106-
fragmentMap: createFragmentMap(getFragmentDefinitions(query)),
79+
return this.writeSelectionSetToStore({
80+
result,
81+
dataId,
82+
selectionSet: operationDefinition.selectionSet,
83+
context: {
84+
store,
85+
processedData: {},
86+
variables: {
87+
...getDefaultValues(operationDefinition),
88+
...variables,
10789
},
108-
});
109-
} catch (e) {
110-
throw enhanceErrorWithDocument(e, query);
111-
}
90+
dataIdFromObject,
91+
fragmentMap: createFragmentMap(getFragmentDefinitions(query)),
92+
},
93+
});
11294
}
11395

11496
public writeSelectionSetToStore({

0 commit comments

Comments
 (0)