Skip to content

Commit 541a058

Browse files
subscribe: drop mapping of AsyncIterable errors (#3027)
1 parent 179e479 commit 541a058

File tree

4 files changed

+4
-142
lines changed

4 files changed

+4
-142
lines changed

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -269,35 +269,6 @@ describe('mapAsyncIterator', () => {
269269
.with.property('message', 'Goodbye');
270270
});
271271

272-
it('maps over thrown errors if second callback provided', async () => {
273-
async function* source() {
274-
yield 'Hello';
275-
throw new Error('Goodbye');
276-
}
277-
278-
const doubles = mapAsyncIterator(
279-
source(),
280-
(x) => x + x,
281-
(error) => error,
282-
);
283-
284-
expect(await doubles.next()).to.deep.equal({
285-
value: 'HelloHello',
286-
done: false,
287-
});
288-
289-
const result = await doubles.next();
290-
expect(result.value)
291-
.to.be.an.instanceOf(Error)
292-
.with.property('message', 'Goodbye');
293-
expect(result.done).to.equal(false);
294-
295-
expect(await doubles.next()).to.deep.equal({
296-
value: undefined,
297-
done: true,
298-
});
299-
});
300-
301272
async function testClosesSourceWithMapper<T>(mapper: (number) => T) {
302273
let didVisitFinally = false;
303274

@@ -353,37 +324,4 @@ describe('mapAsyncIterator', () => {
353324
: Promise.resolve(x),
354325
);
355326
});
356-
357-
it('closes source if mapper throws an error', async () => {
358-
async function* source() {
359-
yield 1;
360-
throw new Error(2);
361-
}
362-
363-
const throwOver1 = mapAsyncIterator(
364-
source(),
365-
(x) => x,
366-
(error) => {
367-
throw new Error('Cannot count to ' + error.message);
368-
},
369-
);
370-
371-
expect(await throwOver1.next()).to.deep.equal({ value: 1, done: false });
372-
373-
let expectedError;
374-
try {
375-
await throwOver1.next();
376-
} catch (error) {
377-
expectedError = error;
378-
}
379-
380-
expect(expectedError)
381-
.to.be.an.instanceOf(Error)
382-
.with.property('message', 'Cannot count to 2');
383-
384-
expect(await throwOver1.next()).to.deep.equal({
385-
value: undefined,
386-
done: true,
387-
});
388-
});
389327
});

src/subscription/__tests__/subscribe-test.js

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { isAsyncIterable } from '../../jsutils/isAsyncIterable';
99
import type { DocumentNode } from '../../language/ast';
1010
import { parse } from '../../language/parser';
1111

12-
import { GraphQLError } from '../../error/GraphQLError';
13-
1412
import { GraphQLSchema } from '../../type/schema';
1513
import { GraphQLList, GraphQLObjectType } from '../../type/definition';
1614
import { GraphQLInt, GraphQLString, GraphQLBoolean } from '../../type/scalars';
@@ -1031,62 +1029,4 @@ describe('Subscription Publish Phase', () => {
10311029
value: undefined,
10321030
});
10331031
});
1034-
1035-
it('should resolve GraphQL error from source event stream', async () => {
1036-
async function* generateEmails() {
1037-
yield { email: { subject: 'Hello' } };
1038-
throw new GraphQLError('test error');
1039-
}
1040-
1041-
const erroringEmailSchema = emailSchemaWithResolvers(
1042-
generateEmails,
1043-
(email) => email,
1044-
);
1045-
1046-
const subscription = await subscribe({
1047-
schema: erroringEmailSchema,
1048-
document: parse(`
1049-
subscription {
1050-
importantEmail {
1051-
email {
1052-
subject
1053-
}
1054-
}
1055-
}
1056-
`),
1057-
});
1058-
invariant(isAsyncIterable(subscription));
1059-
1060-
const payload1 = await subscription.next();
1061-
expect(payload1).to.deep.equal({
1062-
done: false,
1063-
value: {
1064-
data: {
1065-
importantEmail: {
1066-
email: {
1067-
subject: 'Hello',
1068-
},
1069-
},
1070-
},
1071-
},
1072-
});
1073-
1074-
const payload2 = await subscription.next();
1075-
expect(payload2).to.deep.equal({
1076-
done: false,
1077-
value: {
1078-
errors: [
1079-
{
1080-
message: 'test error',
1081-
},
1082-
],
1083-
},
1084-
});
1085-
1086-
const payload3 = await subscription.next();
1087-
expect(payload3).to.deep.equal({
1088-
done: true,
1089-
value: undefined,
1090-
});
1091-
});
10921032
});

src/subscription/mapAsyncIterator.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
77
export function mapAsyncIterator<T, U>(
88
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
99
callback: (T) => PromiseOrValue<U>,
10-
rejectCallback: (any) => U = (error) => {
11-
throw error;
12-
},
1310
): AsyncGenerator<U, void, void> {
1411
// $FlowFixMe[prop-missing]
1512
const iteratorMethod = iterable[Symbol.asyncIterator];
@@ -38,28 +35,20 @@ export function mapAsyncIterator<T, U>(
3835
}
3936
}
4037

41-
function mapReject(error: mixed) {
42-
try {
43-
return { value: rejectCallback(error), done: false };
44-
} catch (callbackError) {
45-
return abruptClose(callbackError);
46-
}
47-
}
48-
4938
/* TODO: Flow doesn't support symbols as keys:
5039
https://github.com/facebook/flow/issues/3258 */
5140
return ({
5241
next(): Promise<IteratorResult<U, void>> {
53-
return iterator.next().then(mapResult, mapReject);
42+
return iterator.next().then(mapResult, abruptClose);
5443
},
5544
return() {
5645
return typeof iterator.return === 'function'
57-
? iterator.return().then(mapResult, mapReject)
46+
? iterator.return().then(mapResult, abruptClose)
5847
: Promise.resolve({ value: undefined, done: true });
5948
},
6049
throw(error?: mixed): Promise<IteratorResult<U, void>> {
6150
if (typeof iterator.throw === 'function') {
62-
return iterator.throw(error).then(mapResult, mapReject);
51+
return iterator.throw(error).then(mapResult, abruptClose);
6352
}
6453
return Promise.reject(error).catch(abruptClose);
6554
},

src/subscription/subscribe.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,7 @@ export async function subscribe(
104104
});
105105

106106
// Map every source value to a ExecutionResult value as described above.
107-
return mapAsyncIterator(resultOrStream, mapSourceToResponse, (error) => {
108-
if (error instanceof GraphQLError) {
109-
return { errors: [error] };
110-
}
111-
throw error;
112-
});
107+
return mapAsyncIterator(resultOrStream, mapSourceToResponse);
113108
}
114109

115110
/**

0 commit comments

Comments
 (0)