Skip to main content

rustapi_toon/
error.rs

1//! TOON Error types and conversions
2
3use rustapi_core::ApiError;
4use thiserror::Error;
5
6/// Error type for TOON operations
7#[derive(Error, Debug)]
8pub 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
26impl 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
35impl From<ToonError> for ApiError {
36    fn from(err: ToonError) -> Self {
37        match err {
38            ToonError::Encode(msg) => ApiError::internal(format!("Failed to encode TOON: {}", msg)),
39            ToonError::Decode(msg) => ApiError::bad_request(format!("Invalid TOON: {}", msg)),
40            ToonError::InvalidContentType => ApiError::bad_request(
41                "Invalid content type: expected application/toon or text/toon",
42            ),
43            ToonError::EmptyBody => ApiError::bad_request("Empty request body"),
44        }
45    }
46}