|
| 1 | +//! TOON Error types and conversions |
| 2 | +
|
| 3 | +use rustapi_core::ApiError; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +/// Error type for TOON operations |
| 7 | +#[derive(Error, Debug)] |
| 8 | +pub enum ToonError { |
| 9 | + /// Error during TOON encoding (serialization) |
| 10 | + #[error("TOON encoding error: {0}")] |
| 11 | + Encode(String), |
| 12 | + |
| 13 | + /// Error during TOON decoding (parsing/deserialization) |
| 14 | + #[error("TOON decoding error: {0}")] |
| 15 | + Decode(String), |
| 16 | + |
| 17 | + /// Invalid content type for TOON request |
| 18 | + #[error("Invalid content type: expected application/toon or text/toon")] |
| 19 | + InvalidContentType, |
| 20 | + |
| 21 | + /// Empty body provided |
| 22 | + #[error("Empty request body")] |
| 23 | + EmptyBody, |
| 24 | +} |
| 25 | + |
| 26 | +impl From<toon_format::ToonError> for ToonError { |
| 27 | + fn from(err: toon_format::ToonError) -> Self { |
| 28 | + match &err { |
| 29 | + toon_format::ToonError::SerializationError(_) => ToonError::Encode(err.to_string()), |
| 30 | + _ => ToonError::Decode(err.to_string()), |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl From<ToonError> for ApiError { |
| 36 | + fn from(err: ToonError) -> Self { |
| 37 | + match err { |
| 38 | + ToonError::Encode(msg) => { |
| 39 | + ApiError::internal(format!("Failed to encode TOON: {}", msg)) |
| 40 | + } |
| 41 | + ToonError::Decode(msg) => ApiError::bad_request(format!("Invalid TOON: {}", msg)), |
| 42 | + ToonError::InvalidContentType => ApiError::bad_request( |
| 43 | + "Invalid content type: expected application/toon or text/toon", |
| 44 | + ), |
| 45 | + ToonError::EmptyBody => ApiError::bad_request("Empty request body"), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments