use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CodingErrorCode {
InvalidInput,
NotFound,
ProjectNotTrusted,
Persistence,
Credential,
Provider,
PolicyDenied,
Cancelled,
Runtime,
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("{code:?}: {message}")]
pub struct CodingError {
code: CodingErrorCode,
message: String,
}
impl CodingError {
pub(crate) fn new(code: CodingErrorCode, message: impl Into<String>) -> Self {
let mut message = message.into();
if message.is_empty() {
"coding operation failed".clone_into(&mut message);
}
message.retain(|character| character != '\0');
if message.len() > 512 {
let boundary = message
.char_indices()
.map(|(index, _)| index)
.take_while(|index| *index <= 512)
.last()
.unwrap_or(0);
message.truncate(boundary);
}
Self { code, message }
}
#[must_use]
pub const fn code(&self) -> CodingErrorCode {
self.code
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl From<tea::RuntimeError> for CodingError {
fn from(error: tea::RuntimeError) -> Self {
let code = match error.code() {
tea::RuntimeErrorCode::ProviderFailure
| tea::RuntimeErrorCode::UnknownProvider
| tea::RuntimeErrorCode::UnknownModel => CodingErrorCode::Provider,
tea::RuntimeErrorCode::PolicyFailure => CodingErrorCode::PolicyDenied,
tea::RuntimeErrorCode::Cancelled => CodingErrorCode::Cancelled,
tea::RuntimeErrorCode::InvalidRequest
| tea::RuntimeErrorCode::UnknownProfile
| tea::RuntimeErrorCode::UnknownTool
| tea::RuntimeErrorCode::UnknownPolicyRule => CodingErrorCode::InvalidInput,
tea::RuntimeErrorCode::SessionFailure => CodingErrorCode::Persistence,
_ => CodingErrorCode::Runtime,
};
let message = match code {
CodingErrorCode::Provider if error.is_safe_diagnostic() => error.message(),
CodingErrorCode::Provider => "model provider operation failed",
CodingErrorCode::Persistence => "session persistence operation failed",
CodingErrorCode::Runtime => "coding runtime operation failed",
CodingErrorCode::Cancelled => "coding operation was cancelled",
CodingErrorCode::PolicyDenied
| CodingErrorCode::Credential
| CodingErrorCode::InvalidInput
| CodingErrorCode::NotFound
| CodingErrorCode::ProjectNotTrusted => error.message(),
};
Self::new(code, message)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalized_provider_diagnostic_survives_product_error_conversion() {
let kernel = tea_kernel::KernelError::provider_failure(
tea_kernel::KernelErrorCode::ModelFailure,
"HTTP 403: blocked by gateway WAF",
);
let runtime = tea::RuntimeError::from(kernel);
let coding = CodingError::from(runtime);
assert_eq!(coding.code(), CodingErrorCode::Provider);
assert_eq!(coding.message(), "HTTP 403: blocked by gateway WAF");
}
#[test]
fn untrusted_provider_runtime_messages_stay_generic() {
let runtime = tea::RuntimeError::new(
tea::RuntimeErrorCode::ProviderFailure,
"sk-seeded-cli-credential-must-never-persist",
);
let coding = CodingError::from(runtime);
assert_eq!(coding.message(), "model provider operation failed");
}
}