Skip to main content

journio_core/
error.rs

1//! Error model — ported from `journio/errors.go`.
2//!
3//! Mirrors Go's `JOURNIOError` (struct with a code + message + optional context
4//! fields + wrapped error). The set of codes is identical so cross-language
5//! behaviour and error mapping in bindings stays 1:1.
6
7use std::fmt;
8
9/// Mirrors `JOURNIOErrorCode` in `journio/errors.go`. Integers kept off the public
10/// surface — match by name.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum JournioErrorCode {
13    ConflictingIDError,
14    InitializationError,
15    NonExistentWorkflowError,
16    ConflictingWorkflowError,
17    WorkflowCancelled,
18    UnexpectedStep,
19    AwaitedWorkflowCancelled,
20    ConflictingRegistrationError,
21    WorkflowUnexpectedTypeError,
22    WorkflowExecutionError,
23    StepExecutionError,
24    DeadLetterQueueError,
25    MaxStepRetriesExceeded,
26    QueueDeduplicated,
27    PatchingNotEnabled,
28    TimeoutError,
29    NoApplicationVersions,
30}
31
32/// Unified error type — ported from `JOURNIOError` (`journio/errors.go`).
33#[derive(Debug)]
34pub struct JournioError {
35    pub code: JournioErrorCode,
36    pub message: String,
37    pub source: Option<Box<dyn std::error::Error + Send + Sync>>,
38    // Optional context fields (only set when relevant — mirrors Go struct).
39    pub workflow_id: Option<String>,
40    pub step_name: Option<String>,
41    pub step_id: Option<i32>,
42    pub queue_name: Option<String>,
43    pub deduplication_id: Option<String>,
44}
45
46impl JournioError {
47    pub fn new(code: JournioErrorCode, message: impl Into<String>) -> Self {
48        Self {
49            code,
50            message: message.into(),
51            source: None,
52            workflow_id: None,
53            step_name: None,
54            step_id: None,
55            queue_name: None,
56            deduplication_id: None,
57        }
58    }
59}
60
61impl fmt::Display for JournioError {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(f, "Journio Error ({:?}): {}", self.code, self.message)
64    }
65}
66
67impl std::error::Error for JournioError {
68    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69        self.source.as_deref().map(|e| e as &dyn std::error::Error)
70    }
71}
72
73/// Convenience constructors matching the Go `newXxxError` helpers.
74pub mod constructors {
75    use super::*;
76
77    pub fn initialization(msg: impl Into<String>) -> JournioError {
78        JournioError::new(JournioErrorCode::InitializationError, msg)
79    }
80    pub fn conflicting_registration(name: &str) -> JournioError {
81        JournioError::new(
82            JournioErrorCode::ConflictingRegistrationError,
83            format!("{name} is already registered"),
84        )
85    }
86    pub fn non_existent_workflow(id: &str) -> JournioError {
87        let mut e = JournioError::new(
88            JournioErrorCode::NonExistentWorkflowError,
89            format!("workflow {id} does not exist"),
90        );
91        e.workflow_id = Some(id.to_string());
92        e
93    }
94    pub fn unexpected_step(
95        workflow_id: &str,
96        step_id: i32,
97        expected: &str,
98        recorded: &str,
99    ) -> JournioError {
100        let mut e = JournioError::new(
101            JournioErrorCode::UnexpectedStep,
102            format!(
103                "During execution of workflow {workflow_id} step {step_id}, function {recorded} was recorded when {expected} was expected. Check that your workflow is deterministic."
104            ),
105        );
106        e.workflow_id = Some(workflow_id.to_string());
107        e.step_id = Some(step_id);
108        e
109    }
110}
111
112pub type JournioResult<T> = Result<T, JournioError>;