Skip to content

Commit dda2445

Browse files
authored
polish: enable useUnknownInCatchVariables (#4619)
1 parent 60742f0 commit dda2445

13 files changed

Lines changed: 94 additions & 23 deletions

File tree

resources/benchmark.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ function runBenchmark(
286286
results.push(computeStats(revision, samples));
287287
process.stdout.write(' ' + cyan(i + 1) + ' tests completed.\u000D');
288288
} catch (error) {
289-
console.log(' ' + revision + ': ' + red(error.message));
289+
const errorMessage =
290+
error instanceof Error ? error.message : String(error);
291+
console.log(' ' + revision + ': ' + red(errorMessage));
290292
}
291293
}
292294
console.log('\n');

src/__testUtils__/__tests__/expectPromise-test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe('expectPromise', () => {
2222
'foo',
2323
); /* c8 ignore start */
2424
} /* c8 ignore stop */ catch (err) {
25-
expect(err.message).to.equal(
25+
const errorMessage = err instanceof Error ? err.message : String(err);
26+
expect(errorMessage).to.equal(
2627
"Promise should have rejected with message 'foo', but resolved as '{}'",
2728
);
2829
}
@@ -34,7 +35,8 @@ describe('expectPromise', () => {
3435
'bar',
3536
); /* c8 ignore start */
3637
} /* c8 ignore stop */ catch (err) {
37-
expect(err.message).to.equal(
38+
const errorMessage = err instanceof Error ? err.message : String(err);
39+
expect(errorMessage).to.equal(
3840
"expected Error: foo to have property 'message' of 'bar', but got 'foo'",
3941
);
4042
}

src/__testUtils__/expectPromise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function expectPromise(maybePromise: unknown) {
1414
return maybePromise;
1515
},
1616
async toRejectWith(message: string) {
17-
let caughtError: Error | undefined;
17+
let caughtError: unknown;
1818
let resolved;
1919
let rejected = false;
2020
try {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
4+
import { ensureGraphQLError } from '../ensureGraphQLError.js';
5+
import { GraphQLError } from '../GraphQLError.js';
6+
7+
describe('ensureGraphQLError', () => {
8+
it('passes GraphQLError through', () => {
9+
const error = new GraphQLError('boom');
10+
expect(ensureGraphQLError(error)).to.equal(error);
11+
});
12+
13+
it('wraps Error values as GraphQLError', () => {
14+
const originalError = new Error('boom');
15+
const error = ensureGraphQLError(originalError);
16+
17+
expect(error).to.be.instanceOf(GraphQLError);
18+
expect(error.message).to.equal('boom');
19+
expect(error.originalError).to.equal(originalError);
20+
});
21+
22+
it('wraps non-error thrown values', () => {
23+
const error = ensureGraphQLError('boom');
24+
25+
expect(error).to.be.instanceOf(GraphQLError);
26+
expect(error.message).to.equal('Unexpected error value: "boom"');
27+
expect(error.originalError).to.include({
28+
name: 'NonErrorThrown',
29+
thrownValue: 'boom',
30+
});
31+
});
32+
});

src/error/ensureGraphQLError.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { toError } from '../jsutils/toError.js';
2+
3+
import { GraphQLError } from './GraphQLError.js';
4+
5+
/**
6+
* Ensure an unknown thrown value is represented as a GraphQLError.
7+
*/
8+
export function ensureGraphQLError(rawError: unknown): GraphQLError {
9+
if (rawError instanceof GraphQLError) {
10+
return rawError;
11+
}
12+
13+
const originalError = toError(rawError);
14+
return new GraphQLError(originalError.message, { originalError });
15+
}

src/execution/Executor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { promiseForObject } from '../jsutils/promiseForObject.js';
1212
import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
1313
import { promiseReduce } from '../jsutils/promiseReduce.js';
1414

15+
import { ensureGraphQLError } from '../error/ensureGraphQLError.js';
1516
import type { GraphQLFormattedError } from '../error/GraphQLError.js';
1617
import { GraphQLError } from '../error/GraphQLError.js';
1718
import { locatedError } from '../error/locatedError.js';
@@ -302,7 +303,7 @@ export class Executor<
302303
},
303304
(error: unknown) => {
304305
onFinish();
305-
this.collectedErrors.add(error as GraphQLError, undefined);
306+
this.collectedErrors.add(ensureGraphQLError(error), undefined);
306307
return this.buildResponse(null);
307308
},
308309
);
@@ -312,7 +313,7 @@ export class Executor<
312313
return this.buildResponse(result);
313314
} catch (error) {
314315
onFinish();
315-
this.collectedErrors.add(error as GraphQLError, undefined);
316+
this.collectedErrors.add(ensureGraphQLError(error), undefined);
316317
return this.buildResponse(null);
317318
}
318319
}

src/execution/execute.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Path } from '../jsutils/Path.js';
88
import { addPath, pathToArray } from '../jsutils/Path.js';
99
import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
1010

11+
import { ensureGraphQLError } from '../error/ensureGraphQLError.js';
1112
import { GraphQLError } from '../error/GraphQLError.js';
1213
import { locatedError } from '../error/locatedError.js';
1314

@@ -529,13 +530,13 @@ function createSourceEventStreamImpl(
529530
const eventStream = executeSubscription(validatedExecutionArgs);
530531
if (isPromise(eventStream)) {
531532
return eventStream.then(undefined, (error: unknown) => ({
532-
errors: [error as GraphQLError],
533+
errors: [ensureGraphQLError(error)],
533534
}));
534535
}
535536

536537
return eventStream;
537538
} catch (error) {
538-
return { errors: [error] };
539+
return { errors: [ensureGraphQLError(error)] };
539540
}
540541
}
541542

src/execution/incremental/IncrementalPublisher.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ObjMap } from '../../jsutils/ObjMap.js';
22
import { pathToArray } from '../../jsutils/Path.js';
33

4+
import { ensureGraphQLError } from '../../error/ensureGraphQLError.js';
45
import type { GraphQLError } from '../../error/GraphQLError.js';
56

67
import { mapAsyncIterable } from '../mapAsyncIterable.js';
@@ -211,7 +212,7 @@ export class IncrementalPublisher {
211212
const id = this._ensureId(group);
212213
context.completed.push({
213214
id,
214-
errors: [error as GraphQLError],
215+
errors: [ensureGraphQLError(error)],
215216
});
216217
this._ids.delete(group);
217218
break;
@@ -250,7 +251,7 @@ export class IncrementalPublisher {
250251
const stream = event.stream;
251252
context.completed.push({
252253
id: this._ensureId(stream),
253-
errors: [event.error as GraphQLError],
254+
errors: [ensureGraphQLError(event.error)],
254255
});
255256
this._ids.delete(stream);
256257
break;

src/execution/legacyIncremental/BranchingIncrementalPublisher.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ObjMap } from '../../jsutils/ObjMap.js';
22
import { addPath, pathToArray } from '../../jsutils/Path.js';
33

4+
import { ensureGraphQLError } from '../../error/ensureGraphQLError.js';
45
import type { GraphQLError } from '../../error/GraphQLError.js';
56

67
import type {
@@ -157,7 +158,7 @@ export class BranchingIncrementalPublisher {
157158
path: pathToArray(group.path),
158159
},
159160
group.label,
160-
[event.error as GraphQLError],
161+
[ensureGraphQLError(event.error)],
161162
),
162163
);
163164
break;
@@ -205,7 +206,7 @@ export class BranchingIncrementalPublisher {
205206
path: pathToArray(stream.path),
206207
},
207208
stream.label,
208-
[event.error as GraphQLError],
209+
[ensureGraphQLError(event.error)],
209210
),
210211
);
211212
break;

src/execution/values.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Maybe } from '../jsutils/Maybe.js';
33
import type { ObjMap, ReadOnlyObjMap } from '../jsutils/ObjMap.js';
44
import { printPathArray } from '../jsutils/printPathArray.js';
55

6+
import { ensureGraphQLError } from '../error/ensureGraphQLError.js';
67
import { GraphQLError } from '../error/GraphQLError.js';
78

89
import type {
@@ -89,7 +90,7 @@ export function getVariableValues(
8990
return { variableValues };
9091
}
9192
} catch (error) {
92-
errors.push(error);
93+
errors.push(ensureGraphQLError(error));
9394
}
9495

9596
return { errors };

0 commit comments

Comments
 (0)