Skip to content

Commit 2321ee8

Browse files
committed
more DRY less then
1 parent 02c40fc commit 2321ee8

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/execution/subscribe.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
88
import { GraphQLError } from '../error/GraphQLError';
99
import { locatedError } from '../error/locatedError';
1010

11-
import type { DocumentNode } from '../language/ast';
11+
import type { DocumentNode, FieldNode } from '../language/ast';
1212

1313
import type { GraphQLFieldResolver } from '../type/definition';
1414
import type { GraphQLSchema } from '../type/schema';
@@ -196,26 +196,15 @@ export function createSourceEventStream(
196196
const eventStream = executeSubscription(exeContext);
197197

198198
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+
);
209203
}
210204

211205
return ensureAsyncIterable(eventStream);
212206
} 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);
219208
}
220209
}
221210

@@ -231,6 +220,15 @@ function ensureAsyncIterable(eventStream: unknown): AsyncIterable<unknown> {
231220
return eventStream;
232221
}
233222

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+
234232
function executeSubscription(exeContext: ExecutionContext): unknown {
235233
const { schema, fragments, operation, variableValues, rootValue } =
236234
exeContext;
@@ -290,22 +288,32 @@ function executeSubscription(exeContext: ExecutionContext): unknown {
290288
const eventStream = resolveFn(rootValue, args, contextValue, info);
291289

292290
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) => {
296299
throw locatedError(error, fieldNodes, pathToArray(path));
297-
});
300+
},
301+
);
298302
}
299303

300-
return throwReturnedError(eventStream);
304+
return throwReturnedError(eventStream, fieldNodes, pathToArray(path));
301305
} catch (error) {
302306
throw locatedError(error, fieldNodes, pathToArray(path));
303307
}
304308
}
305309

306-
function throwReturnedError(eventStream: unknown): unknown {
310+
function throwReturnedError(
311+
eventStream: unknown,
312+
fieldNodes: ReadonlyArray<FieldNode>,
313+
path: ReadonlyArray<string | number>,
314+
): unknown {
307315
if (eventStream instanceof Error) {
308-
throw eventStream;
316+
throw locatedError(eventStream, fieldNodes, path);
309317
}
310318
return eventStream;
311319
}

0 commit comments

Comments
 (0)