Skip to content

Commit f913100

Browse files
committed
Handle failure conversion merge
1 parent 10e70b0 commit f913100

19 files changed

Lines changed: 133 additions & 77 deletions

File tree

crates/common-wasm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "temporalio-common-wasm"
3-
version = "0.2.0"
3+
version = "0.4.0"
44
edition = "2024"
55
authors = ["Temporal Technologies Inc. <sdk@temporal.io>"]
66
license-file = { workspace = true }
@@ -58,5 +58,6 @@ workspace = true
5858

5959
[dev-dependencies]
6060
futures-util = { version = "0.3", default-features = false }
61+
rstest = "0.26"
6162
tempfile = "3.21"
6263
tokio = { version = "1.47", features = ["macros", "rt"] }

crates/common-wasm/src/activity_definition.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
44
use crate::{
55
data_converters::{TemporalDeserializable, TemporalSerializable},
6-
protos::temporal::api::common::v1::Payload,
6+
error::{ApplicationFailure, FailurePayloads},
77
};
8-
use std::time::Duration;
98

109
/// Implement on a marker struct to define an activity.
1110
///
@@ -26,22 +25,13 @@ pub trait ActivityDefinition {
2625
/// Returned as errors from activity functions.
2726
#[derive(Debug)]
2827
pub enum ActivityError {
29-
/// This error can be returned from activities to allow the explicit configuration of certain
30-
/// error properties. It's also the default error type that arbitrary errors will be converted
31-
/// into.
32-
Retryable {
33-
/// The underlying error
34-
source: Box<dyn std::error::Error + Send + Sync + 'static>,
35-
/// If specified, the next retry (if there is one) will occur after this delay
36-
explicit_delay: Option<Duration>,
37-
},
28+
/// Return this error to attach application-failure metadata to an activity failure.
29+
Application(Box<ApplicationFailure>),
3830
/// Return this error to indicate your activity is cancelling
3931
Cancelled {
40-
/// Some data to save as the cancellation reason
41-
details: Option<Payload>,
32+
/// Optional cancellation details.
33+
details: Option<FailurePayloads>,
4234
},
43-
/// Return this error to indicate that the activity should not be retried.
44-
NonRetryable(Box<dyn std::error::Error + Send + Sync + 'static>),
4535
/// Return this error to indicate that the activity will be completed outside of this activity
4636
/// definition, by an external client.
4737
WillCompleteAsync,
@@ -52,16 +42,32 @@ impl ActivityError {
5242
pub fn cancelled() -> Self {
5343
Self::Cancelled { details: None }
5444
}
45+
46+
/// Construct a cancelled error with details that will be converted using the active data
47+
/// converter.
48+
pub fn cancelled_with_details<T>(details: T) -> Self
49+
where
50+
T: Into<FailurePayloads>,
51+
{
52+
Self::Cancelled {
53+
details: Some(details.into()),
54+
}
55+
}
56+
57+
/// Construct an application activity error.
58+
pub fn application(err: ApplicationFailure) -> Self {
59+
Self::Application(err.into())
60+
}
5561
}
5662

5763
impl<E> From<E> for ActivityError
5864
where
5965
E: Into<anyhow::Error>,
6066
{
6167
fn from(source: E) -> Self {
62-
Self::Retryable {
63-
source: source.into().into_boxed_dyn_error(),
64-
explicit_delay: None,
68+
match source.into().downcast::<ApplicationFailure>() {
69+
Ok(application_failure) => Self::Application(Box::new(application_failure)),
70+
Err(err) => Self::Application(ApplicationFailure::new(err).into()),
6571
}
6672
}
6773
}

crates/common/src/data_converters/failure_converter.rs renamed to crates/common-wasm/src/data_converters/failure_converter.rs

File renamed without changes.

crates/common-wasm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern crate tracing;
99

1010
mod activity_definition;
1111
pub mod data_converters;
12+
pub mod error;
1213
mod priority;
1314
pub mod protos;
1415
pub mod worker;

crates/common-wasm/src/protos/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ pub mod coresdk {
161161
}
162162
}
163163

164+
pub fn cancel(fail: APIFailure) -> Self {
165+
Self {
166+
status: Some(aer::Status::Cancelled(Cancellation {
167+
failure: Some(fail),
168+
})),
169+
}
170+
}
171+
164172
pub fn cancel_from_details(payload: Option<Payload>) -> Self {
165173
Self {
166174
status: Some(aer::Status::Cancelled(Cancellation::from_details(payload))),

crates/common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pbjson = { workspace = true }
9898

9999
[dependencies.temporalio-common-wasm]
100100
path = "../common-wasm"
101-
version = "0.2"
101+
version = "0.4"
102102
features = ["grpc-clients"]
103103

104104
[build-dependencies]

crates/common/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ extern crate tracing;
99

1010
#[cfg(feature = "envconfig")]
1111
pub mod envconfig;
12-
pub mod error;
1312
#[doc(hidden)]
1413
pub mod fsm_trait;
1514
pub mod payload_visitor;
@@ -19,7 +18,7 @@ pub mod worker;
1918
pub use temporalio_common_wasm::{
2019
ActivityDefinition, ActivityError, HasWorkflowDefinition, Priority, QueryDefinition,
2120
SignalDefinition, UntypedWorkflow, UpdateDefinition, WorkerDeploymentVersion,
22-
WorkflowDefinition, data_converters,
21+
WorkflowDefinition, data_converters, error,
2322
};
2423

2524
macro_rules! dbg_panic {

crates/sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ default-features = false
4747

4848
[dependencies.temporalio-workflow]
4949
path = "../workflow"
50-
version = "0.2"
50+
version = "0.4"
5151

5252
[dependencies.temporalio-common]
5353
path = "../common"

crates/sdk/src/activities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use temporalio_common::{
6161
data_converters::{
6262
DataConverter, GenericPayloadConverter, SerializationContext, SerializationContextData,
6363
},
64-
error::{ApplicationFailure, FailurePayloads},
6564
protos::{
6665
coresdk::{ActivityHeartbeat, activity_task},
6766
temporal::api::common::v1::{Payload, RetryPolicy, WorkflowExecution},
@@ -382,6 +381,7 @@ impl Debug for ActivityDefinitions {
382381
mod test {
383382
use super::*;
384383
use rstest::rstest;
384+
use temporalio_common::error::ApplicationFailure;
385385

386386
#[rstest]
387387
#[case(true)]

0 commit comments

Comments
 (0)