Skip to content

Commit f11bc16

Browse files
Fix error handling when writing output (#584)
1 parent 8bb37b0 commit f11bc16

File tree

1 file changed

+27
-15
lines changed
  • packages/restate-sdk/src/endpoint/handlers

1 file changed

+27
-15
lines changed

packages/restate-sdk/src/endpoint/handlers/generic.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,20 @@ export class GenericHandler implements RestateHandler {
384384
service.options?.asTerminalError
385385
);
386386

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+
)
401401
.then((decodedInput) =>
402402
// Invoke user handler code
403403
handler.invoke(ctx, decodedInput)
@@ -409,16 +409,28 @@ export class GenericHandler implements RestateHandler {
409409
vmLogger.info("Invocation completed successfully.");
410410
})
411411
.catch((e) => {
412+
// Convert to Error
412413
const error = ensureError(e, service.options?.asTerminalError);
413414
logError(vmLogger, error);
414415

416+
// If TerminalError, handle it here.
417+
// NOTE: this can still fail!
415418
if (error instanceof TerminalError) {
416419
coreVm.sys_write_output_failure({
417420
code: error.code,
418421
message: error.message,
419422
});
420423
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) {
422434
coreVm.notify_error_with_delay_override(
423435
error.message,
424436
error.stack,

0 commit comments

Comments
 (0)