1use crate::{api::errors::ApiErrorResponse, error::OpenRouterError};
2use surf::Response;
3
4#[macro_export]
35macro_rules! setter {
36 ($name:ident, $type:ty) => {
38 #[doc = concat!("Sets the `", stringify!($name), "` field directly.")]
39 pub fn $name(mut self, value: $type) -> Self {
40 self.$name = Some(value);
41 self
42 }
43 };
44
45 ($name:ident, into $type:ty) => {
47 #[doc = concat!("Sets the `", stringify!($name), "` field with automatic conversion using `Into<", stringify!($type), ">`.")]
48 pub fn $name(mut self, value: impl Into<$type>) -> Self {
49 self.$name = Some(value.into());
50 self
51 }
52 };
53}
54
55pub async fn handle_error(mut response: Response) -> Result<(), OpenRouterError> {
56 let status = response.status();
57 let text = response
58 .body_string()
59 .await
60 .unwrap_or_else(|_| "Failed to read response text".to_string());
61 let api_error_response: Result<ApiErrorResponse, _> = serde_json::from_str(&text);
62
63 if let Ok(api_error_response) = api_error_response {
64 Err(OpenRouterError::from(api_error_response))
65 } else {
66 Err(OpenRouterError::ApiError {
67 code: status,
68 message: text,
69 })
70 }
71}