|
| 1 | +//! Structured runtime error types shared by the invocation stream. |
| 2 | +//! |
| 3 | +//! A [`RuntimeError`] carries a stable machine-readable [`RuntimeErrorCode`], |
| 4 | +//! the offending builtin operation name, and optional numeric limit/value |
| 5 | +//! fields. The invocation stream preserves these instead of flattening them |
| 6 | +//! to a string, so an embedding can branch on the code and inspect the |
| 7 | +//! numeric state (payload bytes, depth) without string matching. |
| 8 | +
|
| 9 | +use std::fmt; |
| 10 | + |
| 11 | +/// Result alias used by runtime builtin surfaces. |
| 12 | +pub type RuntimeResult<T> = Result<T, RuntimeError>; |
| 13 | + |
| 14 | +/// Stable machine-readable runtime error codes. |
| 15 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 16 | +pub enum RuntimeErrorCode { |
| 17 | + InvalidConfiguration, |
| 18 | + EventPayloadTooLarge, |
| 19 | + EventDepthExceeded, |
| 20 | + ResourceLimitExceeded, |
| 21 | + InvalidResourceHandle, |
| 22 | + ResourceHandleWrongTable, |
| 23 | + OperationFailed, |
| 24 | + OperationAlreadyTerminal, |
| 25 | + OperationCancelled, |
| 26 | + SyncResourceUnavailable, |
| 27 | + CloseFailed, |
| 28 | +} |
| 29 | + |
| 30 | +impl RuntimeErrorCode { |
| 31 | + /// Stable snake_case string form, used for transport and tests. |
| 32 | + pub const fn as_str(self) -> &'static str { |
| 33 | + match self { |
| 34 | + Self::InvalidConfiguration => "invalid_configuration", |
| 35 | + Self::EventPayloadTooLarge => "event_payload_too_large", |
| 36 | + Self::EventDepthExceeded => "event_depth_exceeded", |
| 37 | + Self::ResourceLimitExceeded => "resource_limit_exceeded", |
| 38 | + Self::InvalidResourceHandle => "invalid_resource_handle", |
| 39 | + Self::ResourceHandleWrongTable => "resource_handle_wrong_table", |
| 40 | + Self::OperationFailed => "operation_failed", |
| 41 | + Self::OperationAlreadyTerminal => "operation_already_terminal", |
| 42 | + Self::OperationCancelled => "operation_cancelled", |
| 43 | + Self::SyncResourceUnavailable => "sync_resource_unavailable", |
| 44 | + Self::CloseFailed => "close_failed", |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// A structured runtime error with a stable code and optional numeric state. |
| 50 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 51 | +pub struct RuntimeError { |
| 52 | + code: RuntimeErrorCode, |
| 53 | + operation: String, |
| 54 | + message: String, |
| 55 | + limit: Option<u64>, |
| 56 | + value: Option<u64>, |
| 57 | +} |
| 58 | + |
| 59 | +impl RuntimeError { |
| 60 | + pub fn new(code: RuntimeErrorCode, operation: &str, message: impl Into<String>) -> Self { |
| 61 | + Self { |
| 62 | + code, |
| 63 | + operation: operation.to_string(), |
| 64 | + message: message.into(), |
| 65 | + limit: None, |
| 66 | + value: None, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Attaches the configured bound that was violated. |
| 71 | + pub fn with_limit(mut self, limit: usize) -> Self { |
| 72 | + self.limit = Some(limit as u64); |
| 73 | + self |
| 74 | + } |
| 75 | + |
| 76 | + /// Attaches the offending value (for example the measured payload size). |
| 77 | + pub fn with_value(mut self, value: usize) -> Self { |
| 78 | + self.value = Some(value as u64); |
| 79 | + self |
| 80 | + } |
| 81 | + |
| 82 | + pub fn code(&self) -> RuntimeErrorCode { |
| 83 | + self.code |
| 84 | + } |
| 85 | + |
| 86 | + pub fn operation(&self) -> &str { |
| 87 | + &self.operation |
| 88 | + } |
| 89 | + |
| 90 | + pub fn limit(&self) -> Option<u64> { |
| 91 | + self.limit |
| 92 | + } |
| 93 | + |
| 94 | + pub fn value(&self) -> Option<u64> { |
| 95 | + self.value |
| 96 | + } |
| 97 | + |
| 98 | + pub fn message(&self) -> &str { |
| 99 | + &self.message |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl fmt::Display for RuntimeError { |
| 104 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 105 | + write!(f, "{}: {}", self.code.as_str(), self.message)?; |
| 106 | + if let Some(limit) = self.limit { |
| 107 | + write!(f, " (limit {limit})")?; |
| 108 | + } |
| 109 | + if let Some(value) = self.value { |
| 110 | + write!(f, " (value {value})")?; |
| 111 | + } |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl std::error::Error for RuntimeError {} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::{RuntimeError, RuntimeErrorCode}; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn structured_error_preserves_code_and_fields() { |
| 124 | + let error = RuntimeError::new( |
| 125 | + RuntimeErrorCode::EventPayloadTooLarge, |
| 126 | + "stream::emit", |
| 127 | + "event payload exceeds the configured bound", |
| 128 | + ) |
| 129 | + .with_limit(32) |
| 130 | + .with_value(64); |
| 131 | + |
| 132 | + assert_eq!(error.code(), RuntimeErrorCode::EventPayloadTooLarge); |
| 133 | + assert_eq!(error.operation(), "stream::emit"); |
| 134 | + assert_eq!(error.limit(), Some(32)); |
| 135 | + assert_eq!(error.value(), Some(64)); |
| 136 | + assert!(error.to_string().contains("event_payload_too_large")); |
| 137 | + } |
| 138 | +} |
0 commit comments