Skip to content

Commit

Permalink
Make error handling consistent in createSourceEventStream
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Aug 13, 2018
1 parent 7cfd686 commit b24cbcd
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/subscription/subscribe.js
Expand Up @@ -35,8 +35,9 @@ import { getOperationRootType } from '../utilities/getOperationRootType';
* Implements the "Subscribe" algorithm described in the GraphQL specification.
*
* Returns a Promise which resolves to either an AsyncIterator (if successful)
* or an ExecutionResult (client error). The promise will be rejected if a
* server error occurs.
* or an ExecutionResult (error). The promise will be rejected if the schema or
* other arguments to this function are invalid, or if the resolved event stream
* is not an async iterable.
*
* If the client-provided arguments to this function do not result in a
* compliant subscription, a GraphQL Response (ExecutionResult) with
Expand Down Expand Up @@ -171,19 +172,28 @@ function subscribeImpl(
reportGraphQLError,
)
: ((resultOrStream: any): ExecutionResult),
reportGraphQLError,
);
}

/**
* Implements the "CreateSourceEventStream" algorithm described in the
* GraphQL specification, resolving the subscription source event stream.
*
* Returns a Promise<AsyncIterable>.
* Returns a Promise which resolves to either an AsyncIterable (if successful)
* or an ExecutionResult (error). The promise will be rejected if the schema or
* other arguments to this function are invalid, or if the resolved event stream
* is not an async iterable.
*
* If the client-provided invalid arguments, the source stream could not be
* created, or the resolver did not return an AsyncIterable, this function will
* will throw an error, which should be caught and handled by the caller.
* If the client-provided arguments to this function do not result in a
* compliant subscription, a GraphQL Response (ExecutionResult) with
* descriptive errors and no data will be returned.
*
* If the the source stream could not be created due to faulty subscription
* resolver logic or underlying systems, the promise will resolve to a single
* ExecutionResult containing `errors` and no `data`.
*
* If the operation succeeded, the promise resolves to the AsyncIterable for the
* event stream returned by the resolver.
*
* A Source Event Stream represents a sequence of events, each of which triggers
* a GraphQL execution for that event.
Expand Down Expand Up @@ -270,7 +280,11 @@ export function createSourceEventStream(
return Promise.resolve(result).then(eventStream => {
// If eventStream is an Error, rethrow a located error.
if (eventStream instanceof Error) {
throw locatedError(eventStream, fieldNodes, responsePathAsArray(path));
return {
errors: [
locatedError(eventStream, fieldNodes, responsePathAsArray(path)),
],
};
}

// Assert field returned an event stream, otherwise yield an error.
Expand All @@ -284,6 +298,8 @@ export function createSourceEventStream(
);
});
} catch (error) {
return Promise.reject(error);
return error instanceof GraphQLError
? Promise.resolve({ errors: [error] })
: Promise.reject(error);
}
}

0 comments on commit b24cbcd

Please sign in to comment.