Skip to main content

stackdeck_mcp_writes/
error.rs

1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum ErrorCode {
5    Validation,
6    Forbidden,
7    Conflict,
8    InProgress,
9    Expired,
10    ReplayUnavailable,
11    OutcomeUnknown,
12    ApprovalRequired,
13    StoreError,
14}
15
16impl ErrorCode {
17    pub fn as_str(self) -> &'static str {
18        match self {
19            Self::Validation => "validation",
20            Self::Forbidden => "forbidden",
21            Self::Conflict => "conflict",
22            Self::InProgress => "in_progress",
23            Self::Expired => "expired",
24            Self::ReplayUnavailable => "replay_unavailable",
25            Self::OutcomeUnknown => "outcome_unknown",
26            Self::ApprovalRequired => "approval_required",
27            Self::StoreError => "store_error",
28        }
29    }
30
31    pub fn http_status(self) -> u16 {
32        match self {
33            Self::Validation => 400,
34            Self::Forbidden => 403,
35            Self::Conflict => 409,
36            Self::InProgress => 409,
37            Self::Expired => 409,
38            Self::ReplayUnavailable => 409,
39            Self::OutcomeUnknown => 500,
40            Self::ApprovalRequired => 428,
41            Self::StoreError => 503,
42        }
43    }
44
45    pub fn default_retryable(self) -> bool {
46        matches!(self, Self::InProgress | Self::StoreError)
47    }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct WriteSafetyError {
52    pub code: ErrorCode,
53    pub message: String,
54    pub status: u16,
55    pub retryable: bool,
56    pub reason: Option<&'static str>,
57    pub retry_after_ms: Option<i64>,
58}
59
60#[derive(Debug)]
61pub enum HandlerError<E> {
62    /// The handler stopped before attempting any external side effect.
63    BeforeEffect(E),
64    /// The handler may have attempted an external side effect.
65    MayHaveRun(E),
66}
67
68impl WriteSafetyError {
69    pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
70        Self {
71            code,
72            message: message.into(),
73            status: code.http_status(),
74            retryable: code.default_retryable(),
75            reason: None,
76            retry_after_ms: None,
77        }
78    }
79
80    pub fn with_retryable(mut self, retryable: bool) -> Self {
81        self.retryable = retryable;
82        self
83    }
84
85    pub fn with_reason(mut self, reason: &'static str) -> Self {
86        self.reason = Some(reason);
87        self
88    }
89
90    /// Mark a handler error only when no external side effect was attempted.
91    pub fn before_effect(self) -> HandlerError<Self> {
92        HandlerError::BeforeEffect(self)
93    }
94}
95
96impl fmt::Display for WriteSafetyError {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{}: {}", self.code.as_str(), self.message)
99    }
100}
101
102impl std::error::Error for WriteSafetyError {}