Skip to main content

objectiveai_sdk/error/
response_error.rs

1//! Core API error type.
2
3use serde::{Deserialize, Serialize};
4use schemars::JsonSchema;
5
6/// A trait for errors that have an HTTP status code and optional message.
7pub trait StatusError {
8    /// Returns the HTTP status code associated with this error.
9    fn status(&self) -> u16;
10
11    /// Returns the error message, if any.
12    fn message(&self) -> Option<serde_json::Value>;
13}
14
15/// An error returned by the ObjectiveAI API.
16///
17/// This struct represents an API error response containing an HTTP status
18/// code and a message. The message can be any JSON value, allowing for
19/// both simple string errors and structured error objects.
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, thiserror::Error, JsonSchema, arbitrary::Arbitrary)]
21#[error("{}", &serde_json::to_string(self).unwrap_or_default())]
22#[schemars(rename = "error.ResponseError")]
23pub struct ResponseError {
24    /// The HTTP status code of the error response.
25    pub code: u16,
26    /// The error message or details as a JSON value.
27    #[arbitrary(with = crate::arbitrary_util::arbitrary_json_value)]
28    pub message: serde_json::Value,
29}
30
31impl StatusError for ResponseError {
32    fn status(&self) -> u16 {
33        self.code
34    }
35
36    fn message(&self) -> Option<serde_json::Value> {
37        Some(self.message.clone())
38    }
39}
40
41impl<T> From<&T> for ResponseError
42where
43    T: StatusError,
44{
45    fn from(error: &T) -> Self {
46        ResponseError {
47            code: error.status(),
48            message: error.message().unwrap_or_default(),
49        }
50    }
51}