use crate::Usage;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("wasmtime error: {0}")]
Wasmtime(#[from] wasmtime::Error),
#[error("corrupt or missing module state: {0}")]
Corrupt(String),
#[error("not found: {0}")]
NotFound(String),
#[error("invalid configuration: {0}")]
Config(&'static str),
#[error("internal error: {0}")]
Internal(String),
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PublishError {
#[error("invalid tenant or function name")]
InvalidName,
#[error("not a valid wasm component")]
Compile(#[source] wasmtime::Error),
#[error("component does not match the warpline handler world")]
ImportMismatch(#[source] wasmtime::Error),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("registry error")]
Registry(#[source] Error),
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum InvokeError {
#[error("invalid tenant or function name")]
InvalidName,
#[error("no module published for this function")]
NotFound,
#[error("invalid limits: {0}")]
InvalidLimits(#[from] crate::ConfigError),
#[error("host is at capacity")]
Overloaded,
#[error("tenant has too many invocations in flight")]
TenantBusy,
#[error("cpu budget exceeded ({budget_ms} ms)")]
CpuBudgetExceeded { usage: Usage, budget_ms: u64 },
#[error("memory cap exceeded (peak {} bytes, cap {cap_bytes} bytes)", usage.mem_peak_bytes)]
MemoryCapExceeded { usage: Usage, cap_bytes: usize },
#[error("wall-clock timeout")]
WallClockTimeout { usage: Usage },
#[error("output exceeds the {limit} byte limit")]
OutputTooLarge { usage: Usage, limit: usize },
#[error("guest trapped: {source}")]
GuestTrap {
usage: Usage,
#[source]
source: wasmtime::Error,
},
#[error("failed to load module: {0}")]
Load(#[source] Error),
}
impl InvokeError {
pub fn usage(&self) -> Option<Usage> {
match self {
Self::CpuBudgetExceeded { usage, .. }
| Self::MemoryCapExceeded { usage, .. }
| Self::WallClockTimeout { usage }
| Self::OutputTooLarge { usage, .. }
| Self::GuestTrap { usage, .. } => Some(*usage),
_ => None,
}
}
pub fn http_status(&self) -> u16 {
match self {
Self::InvalidName | Self::InvalidLimits(_) => 400,
Self::NotFound => 404,
Self::Overloaded => 503,
Self::TenantBusy => 429,
Self::CpuBudgetExceeded { .. } | Self::WallClockTimeout { .. } => 408,
Self::MemoryCapExceeded { .. } => 507,
Self::OutputTooLarge { .. } => 502,
Self::GuestTrap { .. } | Self::Load(_) => 500,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn http_status_table() {
let u = Usage::new(0, 0, 0);
let trap = || wasmtime::Error::msg("trap");
let cases: Vec<(InvokeError, u16)> = vec![
(InvokeError::InvalidName, 400),
(InvokeError::NotFound, 404),
(
InvokeError::InvalidLimits(crate::ConfigError::CpuBudgetRange),
400,
),
(InvokeError::Overloaded, 503),
(InvokeError::TenantBusy, 429),
(
InvokeError::CpuBudgetExceeded {
usage: u,
budget_ms: 1,
},
408,
),
(InvokeError::WallClockTimeout { usage: u }, 408),
(
InvokeError::MemoryCapExceeded {
usage: u,
cap_bytes: 1,
},
507,
),
(InvokeError::OutputTooLarge { usage: u, limit: 1 }, 502),
(
InvokeError::GuestTrap {
usage: u,
source: trap(),
},
500,
),
(InvokeError::Load(Error::Corrupt("x".into())), 500),
];
for (err, status) in cases {
assert_eq!(err.http_status(), status, "{err}");
}
}
}