durable_workflow/
activity_error.rs1use crate::Error;
2
3#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
9#[error("{message}")]
10pub struct ActivityError {
11 pub exception_type: String,
12 pub message: String,
13 pub non_retryable: bool,
14}
15
16impl ActivityError {
17 pub fn new(exception_type: impl Into<String>, message: impl Into<String>) -> Self {
19 Self {
20 exception_type: exception_type.into(),
21 message: message.into(),
22 non_retryable: false,
23 }
24 }
25
26 pub fn non_retryable(mut self, non_retryable: bool) -> Self {
28 self.non_retryable = non_retryable;
29 self
30 }
31
32 pub(crate) fn from_error(error: &Error, fallback_type: &str) -> Self {
33 match error {
34 Error::ActivityError(failure) => failure.clone(),
35 Error::ActivityFailed(failure) => Self::new(
36 failure.exception_type.as_deref().unwrap_or(fallback_type),
37 &failure.message,
38 )
39 .non_retryable(failure.non_retryable),
40 Error::ActivityNotRegistered(_) => {
41 Self::new("ActivityNotRegistered", error.to_string()).non_retryable(true)
42 }
43 Error::Codec(_) | Error::HandlerType { .. } => {
44 Self::new("ActivityCodecError", error.to_string()).non_retryable(true)
45 }
46 _ => Self::new(fallback_type, error.to_string()),
47 }
48 .validated()
49 }
50
51 pub(crate) fn validated(&self) -> Self {
54 if self.exception_type.trim().is_empty() || self.exception_type.len() > 255 {
55 Self::new(
56 "ActivityFailureMetadataError",
57 "activity exception type must contain 1 to 255 bytes and not be blank",
58 )
59 .non_retryable(true)
60 } else {
61 self.clone()
62 }
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn typed_activity_errors_preserve_application_metadata_in_both_execution_modes() {
72 for non_retryable in [false, true] {
73 let error: Error = ActivityError::new("PaymentDeclined", "card declined")
74 .non_retryable(non_retryable)
75 .into();
76 for fallback in ["RustActivityFailure", "RustActivityError"] {
77 let failure = ActivityError::from_error(&error, fallback);
78 assert_eq!(failure.exception_type, "PaymentDeclined");
79 assert_eq!(failure.message, "card declined");
80 assert_eq!(failure.non_retryable, non_retryable);
81 }
82 }
83 }
84
85 #[test]
86 fn invalid_activity_error_metadata_becomes_a_terminal_failure() {
87 for exception_type in [String::new(), " \t".into(), "x".repeat(256)] {
88 let failure = ActivityError::new(exception_type, "original message").validated();
89 assert_eq!(failure.exception_type, "ActivityFailureMetadataError");
90 assert!(failure.non_retryable);
91 }
92 assert_eq!(
93 ActivityError::new("x".repeat(255), "message")
94 .validated()
95 .exception_type
96 .len(),
97 255
98 );
99 }
100}