Skip to content

Commit dbb3fc3

Browse files
Add better closed error
1 parent 8d59f80 commit dbb3fc3

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ doc = false
1414
[dependencies]
1515
pyo3 = { version = "0.25.1", features = ["extension-module"] }
1616
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
17-
restate-sdk-shared-core = { version = "=0.6.0", features = ["request_identity", "sha2_random_seed"] }
17+
restate-sdk-shared-core = { git = "https://github.com/restatedev/sdk-shared-core.git", rev = "eafbe4729a4ae7fa74fd0fb32e29121b240e1e5e", features = ["request_identity", "sha2_random_seed"] }

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use pyo3::create_exception;
23
use pyo3::prelude::*;
34
use pyo3::types::{PyBytes, PyNone, PyString};
@@ -7,6 +8,7 @@ use restate_sdk_shared_core::{
78
TerminalFailure, VMOptions, Value, CANCEL_NOTIFICATION_HANDLE, VM,
89
};
910
use std::time::{Duration, SystemTime};
11+
use restate_sdk_shared_core::fmt::{set_error_formatter, ErrorFormatter};
1012

1113
// Current crate version
1214
const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -158,6 +160,7 @@ impl From<PyFailure> for TerminalFailure {
158160
TerminalFailure {
159161
code: value.code,
160162
message: value.message,
163+
metadata: vec![],
161164
}
162165
}
163166
}
@@ -751,6 +754,31 @@ impl PyIdentityVerifier {
751754
}
752755
}
753756

757+
#[derive(Debug)]
758+
struct PythonErrorFormatter;
759+
760+
impl ErrorFormatter for PythonErrorFormatter {
761+
fn display_closed_error(&self, f: &mut fmt::Formatter<'_>, event: &str) -> fmt::Result {
762+
write!(f, "Execution is suspended, but the handler is still attempting to make progress (calling '{event}'). This can happen:
763+
764+
* If the SuspendedException is caught. Make sure you NEVER catch the SuspendedException, e.g. avoid:
765+
try:
766+
# Code
767+
except:
768+
# This catches all exceptions, including the SuspendedException!
769+
770+
And use instead:
771+
try:
772+
# Code
773+
except TerminalException:
774+
# In Restate handlers you typically want to catch TerminalException
775+
776+
Check https://docs.restate.dev/develop/python/durable-steps#run for more details on run error handling.
777+
778+
* If you use the context after the handler completed, e.g. moving the context to another thread. Check https://docs.restate.dev/develop/python/concurrent-tasks for more details on how to create durable concurrent tasks in Python.")
779+
}
780+
}
781+
754782
#[pymodule]
755783
fn _internal(m: &Bound<'_, PyModule>) -> PyResult<()> {
756784
use tracing_subscriber::EnvFilter;
@@ -790,5 +818,9 @@ fn _internal(m: &Bound<'_, PyModule>) -> PyResult<()> {
790818
"CANCEL_NOTIFICATION_HANDLE",
791819
PyNotificationHandle::from(CANCEL_NOTIFICATION_HANDLE),
792820
)?;
821+
822+
// Set customized error formatter
823+
set_error_formatter(PythonErrorFormatter);
824+
793825
Ok(())
794826
}

0 commit comments

Comments
 (0)