objectiveai_api/functions/executions/
error.rs1use crate::vector;
4
5#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error("fetch function error: {0}")]
10 FetchFunction(objectiveai::error::ResponseError),
11 #[error("function not found")]
13 FunctionNotFound,
14 #[error("fetch profile error: {0}")]
16 FetchProfile(objectiveai::error::ResponseError),
17 #[error("profile not found")]
19 ProfileNotFound,
20 #[error("invalid profile")]
22 InvalidProfile,
23 #[error("fetch ensemble error: {0}")]
25 FetchEnsemble(objectiveai::error::ResponseError),
26 #[error("ensemble not found")]
28 EnsembleNotFound,
29 #[error("invalid ensemble: {0}")]
31 InvalidEnsemble(String),
32 #[error("fetch retry error: {0}")]
34 FetchRetry(objectiveai::error::ResponseError),
35 #[error("retry not found")]
37 RetryNotFound,
38 #[error("invalid retry token")]
40 InvalidRetryToken,
41 #[error("invalid function expression: {0}")]
43 InvalidAppExpression(
44 #[from] objectiveai::functions::expression::ExpressionError,
45 ),
46 #[error("vector completion error: {0}")]
48 Vector(#[from] vector::completions::Error),
49 #[error("Input does not match function input schema")]
51 InputSchemaMismatch,
52 #[error("invalid scalar output, expected number between 0 and 1")]
54 InvalidScalarOutput,
55 #[error(
57 "invalid vector output, expected vector of numbers summing to 1 of length {0}"
58 )]
59 InvalidVectorOutput(usize),
60}
61
62impl objectiveai::error::StatusError for Error {
63 fn status(&self) -> u16 {
64 match self {
65 Error::FetchFunction(e) => e.status(),
66 Error::FunctionNotFound => 404,
67 Error::FetchProfile(e) => e.status(),
68 Error::ProfileNotFound => 404,
69 Error::InvalidProfile => 400,
70 Error::FetchEnsemble(e) => e.status(),
71 Error::EnsembleNotFound => 404,
72 Error::InvalidEnsemble(_) => 400,
73 Error::FetchRetry(e) => e.status(),
74 Error::RetryNotFound => 404,
75 Error::InvalidRetryToken => 400,
76 Error::InvalidAppExpression(_) => 400,
77 Error::Vector(e) => e.status(),
78 Error::InputSchemaMismatch => 400,
79 Error::InvalidScalarOutput => 400,
80 Error::InvalidVectorOutput(_) => 400,
81 }
82 }
83
84 fn message(&self) -> Option<serde_json::Value> {
85 Some(serde_json::json!({
86 "kind": "vector",
87 "error": match self {
88 Error::FetchFunction(e) => serde_json::json!({
89 "kind": "fetch_function",
90 "error": e.message(),
91 }),
92 Error::FunctionNotFound => serde_json::json!({
93 "kind": "function_not_found",
94 "error": "function not found",
95 }),
96 Error::FetchProfile(e) => serde_json::json!({
97 "kind": "fetch_profile",
98 "error": e.message(),
99 }),
100 Error::ProfileNotFound => serde_json::json!({
101 "kind": "profile_not_found",
102 "error": "profile not found",
103 }),
104 Error::InvalidProfile => serde_json::json!({
105 "kind": "invalid_profile",
106 "error": "invalid profile"
107 }),
108 Error::FetchEnsemble(e) => serde_json::json!({
109 "kind": "fetch_ensemble",
110 "error": e.message(),
111 }),
112 Error::EnsembleNotFound => serde_json::json!({
113 "kind": "ensemble_not_found",
114 "error": "ensemble not found",
115 }),
116 Error::InvalidEnsemble(msg) => serde_json::json!({
117 "kind": "invalid_ensemble",
118 "error": msg,
119 }),
120 Error::FetchRetry(e) => serde_json::json!({
121 "kind": "fetch_retry",
122 "error": e.message(),
123 }),
124 Error::RetryNotFound => serde_json::json!({
125 "kind": "retry_not_found",
126 "error": "retry not found",
127 }),
128 Error::InvalidRetryToken => serde_json::json!({
129 "kind": "invalid_retry_token",
130 "error": "invalid retry token",
131 }),
132 Error::InvalidAppExpression(e) => serde_json::json!({
133 "kind": "invalid_expression",
134 "error": e.to_string(),
135 }),
136 Error::Vector(e) => serde_json::json!({
137 "kind": "vector_completion",
138 "error": e.message(),
139 }),
140 Error::InputSchemaMismatch => serde_json::json!({
141 "kind": "input_schema_mismatch",
142 "error": "Input does not match function input schema",
143 }),
144 Error::InvalidScalarOutput => serde_json::json!({
145 "kind": "invalid_scalar_output",
146 "error": "invalid scalar output, expected number between 0 and 1",
147 }),
148 Error::InvalidVectorOutput(len) => serde_json::json!({
149 "kind": "invalid_vector_output",
150 "error": format!("invalid vector output, expected vector of numbers summing to 1 of length {}", len),
151 }),
152 }
153 }))
154 }
155}