1use rustapi_core::ApiError;
4use thiserror::Error;
5
6#[derive(Error, Debug)]
8pub enum ToonError {
9 #[error("TOON encoding error: {0}")]
11 Encode(String),
12
13 #[error("TOON decoding error: {0}")]
15 Decode(String),
16
17 #[error("Invalid content type: expected application/toon or text/toon")]
19 InvalidContentType,
20
21 #[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}