Skip to main content

objectiveai_api/functions/executions/
error.rs

1//! Error types for Function execution.
2
3use crate::vector;
4
5/// Errors that can occur during Function execution.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// Failed to fetch a Function definition.
9    #[error("fetch function error: {0}")]
10    FetchFunction(objectiveai::error::ResponseError),
11    /// The requested Function was not found.
12    #[error("function not found")]
13    FunctionNotFound,
14    /// Failed to fetch a Profile definition.
15    #[error("fetch profile error: {0}")]
16    FetchProfile(objectiveai::error::ResponseError),
17    /// The requested Profile was not found.
18    #[error("profile not found")]
19    ProfileNotFound,
20    /// The Profile is invalid for the Function.
21    #[error("invalid profile")]
22    InvalidProfile,
23    /// Failed to fetch an Ensemble definition.
24    #[error("fetch ensemble error: {0}")]
25    FetchEnsemble(objectiveai::error::ResponseError),
26    /// The requested Ensemble was not found.
27    #[error("ensemble not found")]
28    EnsembleNotFound,
29    /// The Ensemble definition is invalid.
30    #[error("invalid ensemble: {0}")]
31    InvalidEnsemble(String),
32    /// Failed to fetch retry data.
33    #[error("fetch retry error: {0}")]
34    FetchRetry(objectiveai::error::ResponseError),
35    /// The retry data was not found.
36    #[error("retry not found")]
37    RetryNotFound,
38    /// The retry token is malformed.
39    #[error("invalid retry token")]
40    InvalidRetryToken,
41    /// A JMESPath expression in the Function is invalid.
42    #[error("invalid function expression: {0}")]
43    InvalidAppExpression(
44        #[from] objectiveai::functions::expression::ExpressionError,
45    ),
46    /// A Vector Completion task failed.
47    #[error("vector completion error: {0}")]
48    Vector(#[from] vector::completions::Error),
49    /// The input does not match the Function's input schema.
50    #[error("Input does not match function input schema")]
51    InputSchemaMismatch,
52    /// Scalar output is not in [0, 1] range.
53    #[error("invalid scalar output, expected number between 0 and 1")]
54    InvalidScalarOutput,
55    /// Vector output does not sum to 1 or has wrong length.
56    #[error(
57        "invalid vector output, expected vector of numbers summing to 1 of length {0}"
58    )]
59    InvalidVectorOutput(usize),
60    /// Invalid Function for Strategy
61    #[error("invalid function for strategy: {0}")]
62    InvalidFunctionForStrategy(String),
63    /// Invalid Strategy
64    #[error("invalid strategy: {0}")]
65    InvalidStrategy(String),
66}
67
68impl objectiveai::error::StatusError for Error {
69    fn status(&self) -> u16 {
70        match self {
71            Error::FetchFunction(e) => e.status(),
72            Error::FunctionNotFound => 404,
73            Error::FetchProfile(e) => e.status(),
74            Error::ProfileNotFound => 404,
75            Error::InvalidProfile => 400,
76            Error::FetchEnsemble(e) => e.status(),
77            Error::EnsembleNotFound => 404,
78            Error::InvalidEnsemble(_) => 400,
79            Error::FetchRetry(e) => e.status(),
80            Error::RetryNotFound => 404,
81            Error::InvalidRetryToken => 400,
82            Error::InvalidAppExpression(_) => 400,
83            Error::Vector(e) => e.status(),
84            Error::InputSchemaMismatch => 400,
85            Error::InvalidScalarOutput => 400,
86            Error::InvalidVectorOutput(_) => 400,
87            Error::InvalidFunctionForStrategy(_) => 400,
88            Error::InvalidStrategy(_) => 400,
89        }
90    }
91
92    fn message(&self) -> Option<serde_json::Value> {
93        Some(serde_json::json!({
94            "kind": "vector",
95            "error": match self {
96                Error::FetchFunction(e) => serde_json::json!({
97                    "kind": "fetch_function",
98                    "error": e.message(),
99                }),
100                Error::FunctionNotFound => serde_json::json!({
101                    "kind": "function_not_found",
102                    "error": "function not found",
103                }),
104                Error::FetchProfile(e) => serde_json::json!({
105                    "kind": "fetch_profile",
106                    "error": e.message(),
107                }),
108                Error::ProfileNotFound => serde_json::json!({
109                    "kind": "profile_not_found",
110                    "error": "profile not found",
111                }),
112                Error::InvalidProfile => serde_json::json!({
113                    "kind": "invalid_profile",
114                    "error": "invalid profile"
115                }),
116                Error::FetchEnsemble(e) => serde_json::json!({
117                    "kind": "fetch_ensemble",
118                    "error": e.message(),
119                }),
120                Error::EnsembleNotFound => serde_json::json!({
121                    "kind": "ensemble_not_found",
122                    "error": "ensemble not found",
123                }),
124                Error::InvalidEnsemble(msg) => serde_json::json!({
125                    "kind": "invalid_ensemble",
126                    "error": msg,
127                }),
128                Error::FetchRetry(e) => serde_json::json!({
129                    "kind": "fetch_retry",
130                    "error": e.message(),
131                }),
132                Error::RetryNotFound => serde_json::json!({
133                    "kind": "retry_not_found",
134                    "error": "retry not found",
135                }),
136                Error::InvalidRetryToken => serde_json::json!({
137                    "kind": "invalid_retry_token",
138                    "error": "invalid retry token",
139                }),
140                Error::InvalidAppExpression(e) => serde_json::json!({
141                    "kind": "invalid_expression",
142                    "error": e.to_string(),
143                }),
144                Error::Vector(e) => serde_json::json!({
145                    "kind": "vector_completion",
146                    "error": e.message(),
147                }),
148                Error::InputSchemaMismatch => serde_json::json!({
149                    "kind": "input_schema_mismatch",
150                    "error": "Input does not match function input schema",
151                }),
152                Error::InvalidScalarOutput => serde_json::json!({
153                    "kind": "invalid_scalar_output",
154                    "error": "invalid scalar output, expected number between 0 and 1",
155                }),
156                Error::InvalidVectorOutput(len) => serde_json::json!({
157                    "kind": "invalid_vector_output",
158                    "error": format!("invalid vector output, expected vector of numbers summing to 1 of length {}", len),
159                }),
160                Error::InvalidFunctionForStrategy(msg) => serde_json::json!({
161                    "kind": "invalid_function_for_strategy",
162                    "error": msg,
163                }),
164                Error::InvalidStrategy(msg) => serde_json::json!({
165                    "kind": "invalid_strategy",
166                    "error": msg,
167                }),
168            }
169        }))
170    }
171}