Skip to content

Commit e50b7c5

Browse files
More handling of errors. This now improves a bit more the error handling situation (#555)
1 parent f81e7d9 commit e50b7c5

File tree

9 files changed

+56
-35
lines changed

9 files changed

+56
-35
lines changed
Binary file not shown.

packages/restate-sdk/src/common_api.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ export type {
101101
} from "@restatedev/restate-sdk-core";
102102

103103
export type { RestateEndpoint, RestateEndpointBase } from "./endpoint.js";
104-
export { RestateError, TerminalError, TimeoutError } from "./types/errors.js";
104+
export {
105+
/**
106+
* @deprecated YOU MUST NOT USE THIS TYPE
107+
*/
108+
RestateError,
109+
TerminalError,
110+
TimeoutError,
111+
} from "./types/errors.js";
105112
export type {
106113
LoggerTransport,
107114
LogMetadata,

packages/restate-sdk/src/context_impl.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import {
3636
import {
3737
ensureError,
3838
INTERNAL_ERROR_CODE,
39+
logError,
3940
RestateError,
40-
SUSPENDED_ERROR_CODE,
4141
TerminalError,
4242
UNKNOWN_ERROR_CODE,
4343
} from "./types/errors.js";
@@ -125,7 +125,7 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
125125
),
126126
this.outputPump,
127127
this.runClosuresTracker,
128-
this.handleInvocationEndError.bind(this)
128+
this.promiseExecutorErrorCallback.bind(this)
129129
);
130130
}
131131

@@ -696,21 +696,11 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
696696
);
697697
}
698698

699-
handleInvocationEndError(
700-
e: unknown,
701-
notify_vm_error: (vm: vm.WasmVM, error: Error) => void = (vm, error) => {
702-
vm.notify_error(error.message, error.stack);
703-
}
704-
) {
699+
promiseExecutorErrorCallback(e: unknown) {
705700
if (e instanceof AsyncCompleterError) {
706701
const cause = ensureError(e.cause);
707-
702+
logError(this.vmLogger, e.cause);
708703
// Special handling for this one!
709-
this.vmLogger.warn(
710-
"Error when processing a Restate context operation.\n",
711-
cause
712-
);
713-
714704
this.coreVm.notify_error_for_specific_command(
715705
cause.message,
716706
cause.stack,
@@ -720,21 +710,30 @@ export class ContextImpl implements ObjectContext, WorkflowContext {
720710
);
721711
} else {
722712
const error = ensureError(e);
723-
if (
724-
!(error instanceof RestateError) ||
725-
error.code !== SUSPENDED_ERROR_CODE
726-
) {
727-
this.vmLogger.warn(
728-
"Error when processing a Restate context operation.\n",
729-
error
730-
);
713+
logError(this.vmLogger, error);
714+
if (!(error instanceof RestateError)) {
715+
// Notify error
716+
this.coreVm.notify_error(error.message, error.stack);
731717
}
732-
notify_vm_error(this.coreVm, error);
733718
}
734719

735720
// From now on, no progress will be made.
736721
this.invocationEndPromise.resolve();
737722
}
723+
724+
handleInvocationEndError(
725+
e: unknown,
726+
notify_vm_error: (vm: vm.WasmVM, error: Error) => void = (vm, error) => {
727+
vm.notify_error(error.message, error.stack);
728+
}
729+
) {
730+
const error = ensureError(e);
731+
logError(this.vmLogger, error);
732+
notify_vm_error(this.coreVm, error);
733+
734+
// From now on, no progress will be made.
735+
this.invocationEndPromise.resolve();
736+
}
738737
}
739738

740739
function unpackRunParameters<T>(

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
import {
1313
ensureError,
14+
logError,
1415
RestateError,
15-
SUSPENDED_ERROR_CODE,
1616
TerminalError,
1717
} from "../../types/errors.js";
1818
import type { ProtocolMode } from "../../types/discovery.js";
@@ -377,12 +377,7 @@ export class GenericHandler implements RestateHandler {
377377
})
378378
.catch((e) => {
379379
const error = ensureError(e);
380-
if (
381-
!(error instanceof RestateError) ||
382-
error.code !== SUSPENDED_ERROR_CODE
383-
) {
384-
vmLogger.warn("Invocation completed with an error.\n", error);
385-
}
380+
logError(vmLogger, error);
386381

387382
if (error instanceof TerminalError) {
388383
coreVm.sys_write_output_failure({

packages/restate-sdk/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/restate-sdk/src/promises.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export class PromisesExecutor {
446446
}
447447

448448
// Recursion
449-
await this.doProgress(restatePromise);
449+
await this.doProgressInner(restatePromise);
450450
} catch (e) {
451451
// Not good, this is a retryable error.
452452
this.errorCallback(e);

packages/restate-sdk/src/types/errors.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const CANCEL_ERROR_CODE = 409;
1717
export const UNKNOWN_ERROR_CODE = 500;
1818

1919
// From shared core!
20+
export const CLOSED_ERROR_CODE = 598;
2021
export const SUSPENDED_ERROR_CODE = 599;
2122

2223
export function ensureError(e: unknown): Error {
@@ -40,6 +41,25 @@ export function ensureError(e: unknown): Error {
4041
return new Error("Non-Error value: " + msg);
4142
}
4243

44+
export function logError(log: Console, e: unknown) {
45+
if (e instanceof RestateError) {
46+
if (e.code === SUSPENDED_ERROR_CODE) {
47+
log.info("Invocation suspended");
48+
return;
49+
} else if (e.code === CLOSED_ERROR_CODE) {
50+
log.error(
51+
"DANGER! The invocation is closed, but some related operation is still running. \n" +
52+
"This might indicate that a RestatePromise is being awaited on an asynchronous task, outside the handler, or you're awaiting a RestatePromise inside a ctx.run.\n" +
53+
"This is dangerous, and can lead the service to deadlock. Please fix the issue.\n" +
54+
"Diagnostic: ",
55+
e
56+
);
57+
return;
58+
}
59+
}
60+
log.warn("Error when processing a Restate context operation.\n", e);
61+
}
62+
4363
export class RestateError extends Error {
4464
public readonly code: number;
4565
public name = "RestateError";

sdk-shared-core-wasm-bindings/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk-shared-core-wasm-bindings/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ default = ["console_error_panic_hook"]
1212

1313
[dependencies]
1414
wasm-bindgen = "0.2.100"
15-
restate-sdk-shared-core = { git = "https://github.com/restatedev/sdk-shared-core.git", rev = "168a71b60ee37eb344d38562ff5bbec4ff6f86b9", features = ["request_identity"] }
15+
restate-sdk-shared-core = { git = "https://github.com/restatedev/sdk-shared-core.git", rev = "96b78a6160880cc4b34c56750dd96652e11cd81c", features = ["request_identity"] }
1616
console_error_panic_hook = { version = "0.1.7", optional = true }
1717
serde = { version = "1.0.210", features = ["derive"] }
1818
serde-wasm-bindgen = "0.6.5"

0 commit comments

Comments
 (0)