@@ -384,20 +384,20 @@ export class GenericHandler implements RestateHandler {
384
384
service . options ?. asTerminalError
385
385
) ;
386
386
387
- ( async ( ) => {
388
- try {
389
- // Decode input. If fails, throw TerminalError to fail the invocation
390
- return await journalValueCodec . decode ( input . input ) ;
391
- } catch ( e ) {
392
- const error = ensureError ( e ) ;
393
- throw new TerminalError (
394
- `Failed to decode input using journal value codec: ${ error . message } `,
395
- {
396
- errorCode : 400 ,
397
- }
398
- ) ;
399
- }
400
- } ) ( )
387
+ journalValueCodec
388
+ . decode ( input . input )
389
+ . catch ( ( e ) =>
390
+ Promise . reject (
391
+ new TerminalError (
392
+ `Failed to decode input using journal value codec: ${
393
+ ensureError ( e ) . message
394
+ } `,
395
+ {
396
+ errorCode : 400 ,
397
+ }
398
+ )
399
+ )
400
+ )
401
401
. then ( ( decodedInput ) =>
402
402
// Invoke user handler code
403
403
handler . invoke ( ctx , decodedInput )
@@ -409,16 +409,28 @@ export class GenericHandler implements RestateHandler {
409
409
vmLogger . info ( "Invocation completed successfully." ) ;
410
410
} )
411
411
. catch ( ( e ) => {
412
+ // Convert to Error
412
413
const error = ensureError ( e , service . options ?. asTerminalError ) ;
413
414
logError ( vmLogger , error ) ;
414
415
416
+ // If TerminalError, handle it here.
417
+ // NOTE: this can still fail!
415
418
if ( error instanceof TerminalError ) {
416
419
coreVm . sys_write_output_failure ( {
417
420
code : error . code ,
418
421
message : error . message ,
419
422
} ) ;
420
423
coreVm . sys_end ( ) ;
421
- } else if ( error instanceof RetryableError ) {
424
+ return ;
425
+ }
426
+
427
+ // Not a terminal error, have the below catch handle it
428
+ throw error ;
429
+ } )
430
+ . catch ( ( e ) => {
431
+ // Handle any other error now (retryable errors)
432
+ const error = ensureError ( e ) ;
433
+ if ( error instanceof RetryableError ) {
422
434
coreVm . notify_error_with_delay_override (
423
435
error . message ,
424
436
error . stack ,
0 commit comments