@@ -8,7 +8,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
8
8
import { GraphQLError } from '../error/GraphQLError' ;
9
9
import { locatedError } from '../error/locatedError' ;
10
10
11
- import type { DocumentNode } from '../language/ast' ;
11
+ import type { DocumentNode , FieldNode } from '../language/ast' ;
12
12
13
13
import type { GraphQLFieldResolver } from '../type/definition' ;
14
14
import type { GraphQLSchema } from '../type/schema' ;
@@ -196,26 +196,15 @@ export function createSourceEventStream(
196
196
const eventStream = executeSubscription ( exeContext ) ;
197
197
198
198
if ( isPromise ( eventStream ) ) {
199
- return eventStream
200
- . then ( ( resolvedEventStream ) => ensureAsyncIterable ( resolvedEventStream ) )
201
- . then ( undefined , ( error ) => {
202
- // If it GraphQLError, report it as an ExecutionResult, containing only errors and no data.
203
- // Otherwise treat the error as a system-class error and re-throw it.
204
- if ( error instanceof GraphQLError ) {
205
- return { errors : [ error ] } ;
206
- }
207
- throw error ;
208
- } ) ;
199
+ return eventStream . then (
200
+ ( resolvedEventStream ) => ensureAsyncIterable ( resolvedEventStream ) ,
201
+ ( error ) => handleRawError ( error ) ,
202
+ ) ;
209
203
}
210
204
211
205
return ensureAsyncIterable ( eventStream ) ;
212
206
} catch ( error ) {
213
- // If it GraphQLError, report it as an ExecutionResult, containing only errors and no data.
214
- // Otherwise treat the error as a system-class error and re-throw it.
215
- if ( error instanceof GraphQLError ) {
216
- return { errors : [ error ] } ;
217
- }
218
- throw error ;
207
+ return handleRawError ( error ) ;
219
208
}
220
209
}
221
210
@@ -231,6 +220,15 @@ function ensureAsyncIterable(eventStream: unknown): AsyncIterable<unknown> {
231
220
return eventStream ;
232
221
}
233
222
223
+ function handleRawError ( error : unknown ) : ExecutionResult {
224
+ // If it GraphQLError, report it as an ExecutionResult, containing only errors and no data.
225
+ // Otherwise treat the error as a system-class error and re-throw it.
226
+ if ( error instanceof GraphQLError ) {
227
+ return { errors : [ error ] } ;
228
+ }
229
+ throw error ;
230
+ }
231
+
234
232
function executeSubscription ( exeContext : ExecutionContext ) : unknown {
235
233
const { schema, fragments, operation, variableValues, rootValue } =
236
234
exeContext ;
@@ -290,22 +288,32 @@ function executeSubscription(exeContext: ExecutionContext): unknown {
290
288
const eventStream = resolveFn ( rootValue , args , contextValue , info ) ;
291
289
292
290
if ( isPromise ( eventStream ) ) {
293
- return eventStream
294
- . then ( ( resolvedEventStream ) => throwReturnedError ( resolvedEventStream ) )
295
- . then ( undefined , ( error ) => {
291
+ return eventStream . then (
292
+ ( resolvedEventStream ) =>
293
+ throwReturnedError (
294
+ resolvedEventStream ,
295
+ fieldNodes ,
296
+ pathToArray ( path ) ,
297
+ ) ,
298
+ ( error ) => {
296
299
throw locatedError ( error , fieldNodes , pathToArray ( path ) ) ;
297
- } ) ;
300
+ } ,
301
+ ) ;
298
302
}
299
303
300
- return throwReturnedError ( eventStream ) ;
304
+ return throwReturnedError ( eventStream , fieldNodes , pathToArray ( path ) ) ;
301
305
} catch ( error ) {
302
306
throw locatedError ( error , fieldNodes , pathToArray ( path ) ) ;
303
307
}
304
308
}
305
309
306
- function throwReturnedError ( eventStream : unknown ) : unknown {
310
+ function throwReturnedError (
311
+ eventStream : unknown ,
312
+ fieldNodes : ReadonlyArray < FieldNode > ,
313
+ path : ReadonlyArray < string | number > ,
314
+ ) : unknown {
307
315
if ( eventStream instanceof Error ) {
308
- throw eventStream ;
316
+ throw locatedError ( eventStream , fieldNodes , path ) ;
309
317
}
310
318
return eventStream ;
311
319
}
0 commit comments