33
44use 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 ) ]
2827pub 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
5763impl < E > From < E > for ActivityError
5864where
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}
0 commit comments