objectiveai_sdk/error/
response_error.rs1use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6pub trait StatusError {
8 fn status(&self) -> u16;
10
11 fn message(&self) -> Option<serde_json::Value>;
13}
14
15#[derive(
21 Debug,
22 Clone,
23 PartialEq,
24 Serialize,
25 Deserialize,
26 thiserror::Error,
27 JsonSchema,
28 arbitrary::Arbitrary,
29)]
30#[error("{}", &serde_json::to_string(self).unwrap_or_default())]
31#[schemars(rename = "error.ResponseError")]
32pub struct ResponseError {
33 pub code: u16,
35 #[arbitrary(with = crate::arbitrary_util::arbitrary_json_value)]
37 pub message: serde_json::Value,
38}
39
40impl StatusError for ResponseError {
41 fn status(&self) -> u16 {
42 self.code
43 }
44
45 fn message(&self) -> Option<serde_json::Value> {
46 Some(self.message.clone())
47 }
48}
49
50impl<T> From<&T> for ResponseError
51where
52 T: StatusError,
53{
54 fn from(error: &T) -> Self {
55 ResponseError {
56 code: error.status(),
57 message: error.message().unwrap_or_default(),
58 }
59 }
60}