@@ -191,6 +191,7 @@ export class PothosErrorsPlugin<Types extends SchemaTypes> extends BasePlugin<Ty
191
191
const pothosItemErrors = fieldConfig . extensions ?. pothosItemErrors as
192
192
| ( typeof Error ) [ ]
193
193
| undefined ;
194
+ const onResolvedError = this . builder . options . errors ?. onResolvedError ;
194
195
195
196
if ( ! pothosErrors && ! pothosItemErrors ) {
196
197
return resolver ;
@@ -205,7 +206,7 @@ export class PothosErrorsPlugin<Types extends SchemaTypes> extends BasePlugin<Ty
205
206
const result = ( await resolver ( source , args , context , info ) ) as never ;
206
207
207
208
if ( pothosItemErrors && result && typeof result === 'object' && Symbol . iterator in result ) {
208
- return yieldErrors ( result , pothosItemErrors ) ;
209
+ return yieldErrors ( result , pothosItemErrors , onResolvedError ) ;
209
210
}
210
211
211
212
if (
@@ -214,13 +215,12 @@ export class PothosErrorsPlugin<Types extends SchemaTypes> extends BasePlugin<Ty
214
215
typeof result === 'object' &&
215
216
Symbol . asyncIterator in result
216
217
) {
217
- console . log ( result , yieldAsyncErrors ) ;
218
- return yieldAsyncErrors ( result , pothosItemErrors ) ;
218
+ return yieldAsyncErrors ( result , pothosItemErrors , onResolvedError ) ;
219
219
}
220
220
221
221
return result ;
222
222
} catch ( error : unknown ) {
223
- return wrapOrThrow ( error , pothosErrors ?? [ ] ) ;
223
+ return wrapOrThrow ( error , pothosErrors ?? [ ] , onResolvedError ) ;
224
224
}
225
225
} ;
226
226
}
@@ -230,6 +230,7 @@ export class PothosErrorsPlugin<Types extends SchemaTypes> extends BasePlugin<Ty
230
230
fieldConfig : PothosOutputFieldConfig < Types > ,
231
231
) : GraphQLFieldResolver < unknown , Types [ 'Context' ] , object > | undefined {
232
232
const pothosErrors = fieldConfig . extensions ?. pothosErrors as ( typeof Error ) [ ] | undefined ;
233
+ const onResolvedError = this . builder . options . errors ?. onResolvedError ;
233
234
234
235
if ( ! pothosErrors ) {
235
236
return subscribe ;
@@ -248,7 +249,7 @@ export class PothosErrorsPlugin<Types extends SchemaTypes> extends BasePlugin<Ty
248
249
yield value ;
249
250
}
250
251
} catch ( error : unknown ) {
251
- yield wrapOrThrow ( error , pothosErrors ?? [ ] ) ;
252
+ yield wrapOrThrow ( error , pothosErrors ?? [ ] , onResolvedError ) ;
252
253
}
253
254
}
254
255
@@ -371,9 +372,14 @@ SchemaBuilder.registerPlugin(pluginName, PothosErrorsPlugin, {
371
372
} ) ,
372
373
} ) ;
373
374
374
- function wrapOrThrow ( error : unknown , pothosErrors : ErrorConstructor [ ] ) {
375
+ function wrapOrThrow (
376
+ error : unknown ,
377
+ pothosErrors : ErrorConstructor [ ] ,
378
+ onResolvedError ?: ( error : Error ) => void ,
379
+ ) {
375
380
for ( const errorType of pothosErrors ) {
376
381
if ( error instanceof errorType ) {
382
+ onResolvedError ?.( error ) ;
377
383
const result = createErrorProxy ( error , errorType , { wrapped : true } ) ;
378
384
379
385
errorTypeMap . set ( result , errorType ) ;
@@ -385,30 +391,38 @@ function wrapOrThrow(error: unknown, pothosErrors: ErrorConstructor[]) {
385
391
throw error ;
386
392
}
387
393
388
- function * yieldErrors ( result : Iterable < unknown > , pothosErrors : ErrorConstructor [ ] ) {
394
+ function * yieldErrors (
395
+ result : Iterable < unknown > ,
396
+ pothosErrors : ErrorConstructor [ ] ,
397
+ onResolvedError ?: ( error : Error ) => void ,
398
+ ) {
389
399
try {
390
400
for ( const item of result ) {
391
401
if ( item instanceof Error ) {
392
- yield wrapOrThrow ( item , pothosErrors ) ;
402
+ yield wrapOrThrow ( item , pothosErrors , onResolvedError ) ;
393
403
} else {
394
404
yield item ;
395
405
}
396
406
}
397
407
} catch ( error : unknown ) {
398
- yield wrapOrThrow ( error , pothosErrors ) ;
408
+ yield wrapOrThrow ( error , pothosErrors , onResolvedError ) ;
399
409
}
400
410
}
401
411
402
- async function * yieldAsyncErrors ( result : AsyncIterable < unknown > , pothosErrors : ErrorConstructor [ ] ) {
412
+ async function * yieldAsyncErrors (
413
+ result : AsyncIterable < unknown > ,
414
+ pothosErrors : ErrorConstructor [ ] ,
415
+ onResolvedError ?: ( error : Error ) => void ,
416
+ ) {
403
417
try {
404
418
for await ( const item of result ) {
405
419
if ( item instanceof Error ) {
406
- yield wrapOrThrow ( item , pothosErrors ) ;
420
+ yield wrapOrThrow ( item , pothosErrors , onResolvedError ) ;
407
421
} else {
408
422
yield item ;
409
423
}
410
424
}
411
425
} catch ( error : unknown ) {
412
- yield wrapOrThrow ( error , pothosErrors ) ;
426
+ yield wrapOrThrow ( error , pothosErrors , onResolvedError ) ;
413
427
}
414
428
}
0 commit comments