Skip to content

Commit b24cbcd

Browse files
committed
Make error handling consistent in createSourceEventStream
1 parent 7cfd686 commit b24cbcd

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/subscription/subscribe.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ import { getOperationRootType } from '../utilities/getOperationRootType';
3535
* Implements the "Subscribe" algorithm described in the GraphQL specification.
3636
*
3737
* Returns a Promise which resolves to either an AsyncIterator (if successful)
38-
* or an ExecutionResult (client error). The promise will be rejected if a
39-
* server error occurs.
38+
* or an ExecutionResult (error). The promise will be rejected if the schema or
39+
* other arguments to this function are invalid, or if the resolved event stream
40+
* is not an async iterable.
4041
*
4142
* If the client-provided arguments to this function do not result in a
4243
* compliant subscription, a GraphQL Response (ExecutionResult) with
@@ -171,19 +172,28 @@ function subscribeImpl(
171172
reportGraphQLError,
172173
)
173174
: ((resultOrStream: any): ExecutionResult),
174-
reportGraphQLError,
175175
);
176176
}
177177

178178
/**
179179
* Implements the "CreateSourceEventStream" algorithm described in the
180180
* GraphQL specification, resolving the subscription source event stream.
181181
*
182-
* Returns a Promise<AsyncIterable>.
182+
* Returns a Promise which resolves to either an AsyncIterable (if successful)
183+
* or an ExecutionResult (error). The promise will be rejected if the schema or
184+
* other arguments to this function are invalid, or if the resolved event stream
185+
* is not an async iterable.
183186
*
184-
* If the client-provided invalid arguments, the source stream could not be
185-
* created, or the resolver did not return an AsyncIterable, this function will
186-
* will throw an error, which should be caught and handled by the caller.
187+
* If the client-provided arguments to this function do not result in a
188+
* compliant subscription, a GraphQL Response (ExecutionResult) with
189+
* descriptive errors and no data will be returned.
190+
*
191+
* If the the source stream could not be created due to faulty subscription
192+
* resolver logic or underlying systems, the promise will resolve to a single
193+
* ExecutionResult containing `errors` and no `data`.
194+
*
195+
* If the operation succeeded, the promise resolves to the AsyncIterable for the
196+
* event stream returned by the resolver.
187197
*
188198
* A Source Event Stream represents a sequence of events, each of which triggers
189199
* a GraphQL execution for that event.
@@ -270,7 +280,11 @@ export function createSourceEventStream(
270280
return Promise.resolve(result).then(eventStream => {
271281
// If eventStream is an Error, rethrow a located error.
272282
if (eventStream instanceof Error) {
273-
throw locatedError(eventStream, fieldNodes, responsePathAsArray(path));
283+
return {
284+
errors: [
285+
locatedError(eventStream, fieldNodes, responsePathAsArray(path)),
286+
],
287+
};
274288
}
275289

276290
// Assert field returned an event stream, otherwise yield an error.
@@ -284,6 +298,8 @@ export function createSourceEventStream(
284298
);
285299
});
286300
} catch (error) {
287-
return Promise.reject(error);
301+
return error instanceof GraphQLError
302+
? Promise.resolve({ errors: [error] })
303+
: Promise.reject(error);
288304
}
289305
}

0 commit comments

Comments
 (0)