rust-error
Define, wrap, convert, classify, report or test Rust error types in litellm-rust with thiserror. Use when adding an error type or variant, mapping one crate's error into another, turning an error into a gateway response or Python exception, deciding retry or fault from an error, or replacing call sites that build errors from strings
SKILL.md 原文预览
Rust errors
crates/http/src/error.rs is the reference implementation. Read the reference for the part you are touching before editing, each one carries the reasoning and worked examples behind its rules
- shape
- One top-level
Errorper crate insrc/error.rs. A public enum by default, an opaque struct over a privateReprenum when the variants must stay private. No#[non_exhaustive] - A variant is a failure mode a caller handles differently. Provider names and guidance are data, never variants
- messages and causes
- Every message is written once, in
#[error(...)], and describes its own layer only. Call sites pass typed facts, never a string - The cause is the
source(), never part of the message. Keep it typed, never stringify or drop it reqwest::Errorenters our types only throughlitellm_http::Error, which strips its URL- Anything shown to someone else is rendered with
litellm_error::Report, neverto_string() - conversions
- One
Fromper source and target pair, only when the conversion needs no context. It classifies and sanitizes once - Nest a lower crate's error in one variant. Translate when the meaning changes, never mirror its variants
- classification
- Callers ask predicates named after their decision, they do not match nested variants. Predicates match exhaustively
- Classification states facts. Retry and fallback policy lives with the caller that knows idempotency
- boundaries
- An error becomes text for someone else only in one mapping function per error type, in the gateway crates, the Python bridge and host-python. The inference gateway and the bridge decide by the same predicates
- A failure before the provider was called is a decline. Both ask
declined()first and hand it back to the host unchanged - gateway
- One
ErrorwithIntoResponseper gateway crate that serves a router. Every failure flows through the sameFromchain, is classified once instatus()and rendered by surface and framing - pyo3
- Two
create_exception!classes with the payload inargs,ValueErrorfor the caller's fault andRuntimeErrorotherwise, decoded infailures.py - testing
- Test predicates, chain shape and redaction on errors built the way production builds them
- recipes
- Common tasks walked through end to end
The workspace uses thiserror 2 for every error type, and no library crate uses anyhow or Box<dyn Error> in a public signature. The derive docs are the authority on macro behavior, and the std::error::Error docs on what Display and source() mean
评论
