1use crate::Usage;
4
5#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum Error {
10 #[error("io error: {0}")]
11 Io(#[from] std::io::Error),
12 #[error("wasmtime error: {0}")]
13 Wasmtime(#[from] wasmtime::Error),
14 #[error("corrupt or missing module state: {0}")]
17 Corrupt(String),
18 #[error("not found: {0}")]
20 NotFound(String),
21 #[error("invalid configuration: {0}")]
23 Config(&'static str),
24 #[error("internal error: {0}")]
26 Internal(String),
27}
28
29#[derive(Debug, thiserror::Error)]
32#[non_exhaustive]
33pub enum PublishError {
34 #[error("invalid tenant or function name")]
36 InvalidName,
37 #[error("not a valid wasm component")]
39 Compile(#[source] wasmtime::Error),
40 #[error("component does not match the warpline handler world")]
43 ImportMismatch(#[source] wasmtime::Error),
44 #[error("io error: {0}")]
45 Io(#[from] std::io::Error),
46 #[error("registry error")]
49 Registry(#[source] Error),
50}
51
52#[derive(Debug, thiserror::Error)]
59#[non_exhaustive]
60pub enum InvokeError {
61 #[error("invalid tenant or function name")]
62 InvalidName,
63 #[error("no module published for this function")]
64 NotFound,
65 #[error("invalid limits: {0}")]
68 InvalidLimits(#[from] crate::ConfigError),
69 #[error("host is at capacity")]
72 Overloaded,
73 #[error("tenant has too many invocations in flight")]
75 TenantBusy,
76 #[error("cpu budget exceeded ({budget_ms} ms)")]
77 CpuBudgetExceeded { usage: Usage, budget_ms: u64 },
78 #[error("memory cap exceeded (peak {} bytes, cap {cap_bytes} bytes)", usage.mem_peak_bytes)]
79 MemoryCapExceeded { usage: Usage, cap_bytes: usize },
80 #[error("wall-clock timeout")]
81 WallClockTimeout { usage: Usage },
82 #[error("output exceeds the {limit} byte limit")]
83 OutputTooLarge { usage: Usage, limit: usize },
84 #[error("guest trapped: {source}")]
85 GuestTrap {
86 usage: Usage,
87 #[source]
88 source: wasmtime::Error,
89 },
90 #[error("failed to load module: {0}")]
93 Load(#[source] Error),
94}
95
96impl InvokeError {
97 pub fn usage(&self) -> Option<Usage> {
99 match self {
100 Self::CpuBudgetExceeded { usage, .. }
101 | Self::MemoryCapExceeded { usage, .. }
102 | Self::WallClockTimeout { usage }
103 | Self::OutputTooLarge { usage, .. }
104 | Self::GuestTrap { usage, .. } => Some(*usage),
105 _ => None,
106 }
107 }
108
109 pub fn http_status(&self) -> u16 {
113 match self {
114 Self::InvalidName | Self::InvalidLimits(_) => 400,
115 Self::NotFound => 404,
116 Self::Overloaded => 503,
117 Self::TenantBusy => 429,
118 Self::CpuBudgetExceeded { .. } | Self::WallClockTimeout { .. } => 408,
119 Self::MemoryCapExceeded { .. } => 507,
120 Self::OutputTooLarge { .. } => 502,
121 Self::GuestTrap { .. } | Self::Load(_) => 500,
122 }
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn http_status_table() {
132 let u = Usage::new(0, 0, 0);
133 let trap = || wasmtime::Error::msg("trap");
134 let cases: Vec<(InvokeError, u16)> = vec![
135 (InvokeError::InvalidName, 400),
136 (InvokeError::NotFound, 404),
137 (
138 InvokeError::InvalidLimits(crate::ConfigError::CpuBudgetRange),
139 400,
140 ),
141 (InvokeError::Overloaded, 503),
142 (InvokeError::TenantBusy, 429),
143 (
144 InvokeError::CpuBudgetExceeded {
145 usage: u,
146 budget_ms: 1,
147 },
148 408,
149 ),
150 (InvokeError::WallClockTimeout { usage: u }, 408),
151 (
152 InvokeError::MemoryCapExceeded {
153 usage: u,
154 cap_bytes: 1,
155 },
156 507,
157 ),
158 (InvokeError::OutputTooLarge { usage: u, limit: 1 }, 502),
159 (
160 InvokeError::GuestTrap {
161 usage: u,
162 source: trap(),
163 },
164 500,
165 ),
166 (InvokeError::Load(Error::Corrupt("x".into())), 500),
167 ];
168 for (err, status) in cases {
169 assert_eq!(err.http_status(), status, "{err}");
170 }
171 }
172}