Skip to main content

nmbrs_errorhandler/
detail.rs

1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! Error detail: the immutable value passed through the handler chain.
5
6/// Whether an operation should be retried.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Retry {
9    DoRetry,
10    DoNotRetry,
11    Unset,
12}
13
14/// The error detail passed through and transformed by the handler chain.
15///
16/// Immutable — handlers produce new instances via `with_*` methods.
17#[derive(Debug, Clone)]
18pub struct ErrorDetail {
19    /// Classification name (typically the error type name).
20    pub name: String,
21    /// Whether the operation should be retried.
22    pub retry: Retry,
23    /// Result code for the operation (0 = OK, nonzero = error).
24    pub result_code: i32,
25    /// Whether execution should stop after this error.
26    pub should_stop: bool,
27}
28
29impl ErrorDetail {
30    pub fn ok() -> Self {
31        Self {
32            name: "OK".into(),
33            retry: Retry::Unset,
34            result_code: 0,
35            should_stop: false,
36        }
37    }
38
39    pub fn non_retryable(name: impl Into<String>) -> Self {
40        Self {
41            name: name.into(),
42            retry: Retry::DoNotRetry,
43            result_code: 127,
44            should_stop: false,
45        }
46    }
47
48    pub fn retryable(name: impl Into<String>) -> Self {
49        Self {
50            name: name.into(),
51            retry: Retry::DoRetry,
52            result_code: 127,
53            should_stop: false,
54        }
55    }
56
57    pub fn unknown(name: impl Into<String>) -> Self {
58        Self {
59            name: name.into(),
60            retry: Retry::Unset,
61            result_code: 127,
62            should_stop: false,
63        }
64    }
65
66    pub fn is_retryable(&self) -> bool {
67        self.retry == Retry::DoRetry
68    }
69
70    pub fn with_retryable(mut self) -> Self {
71        self.retry = Retry::DoRetry;
72        self
73    }
74
75    pub fn with_not_retryable(mut self) -> Self {
76        self.retry = Retry::DoNotRetry;
77        self
78    }
79
80    pub fn with_result_code(mut self, code: i32) -> Self {
81        self.result_code = code;
82        self
83    }
84
85    pub fn with_stop(mut self) -> Self {
86        self.should_stop = true;
87        self
88    }
89}