Skip to main content

sova_ai/
error.rs

1//! Errors for the AI plugin.
2
3use sova_core::{IntoResponse, Response};
4use thiserror::Error;
5
6/// Plugin / call errors (maps AISDK failures and missing install config).
7#[derive(Debug, Error)]
8pub enum AiError {
9    /// No default model was configured on [`crate::Ai`] / [`crate::AiClient`].
10    #[error("ai: no model configured (use Ai::model(...) or Ai::fake(...))")]
11    NoModel,
12    /// Missing prompt/messages on a bound call.
13    #[error("ai: prompt or messages required")]
14    EmptyPrompt,
15    /// Upstream AISDK error.
16    #[error(transparent)]
17    Sdk(#[from] aisdk::Error),
18}
19
20impl IntoResponse for AiError {
21    fn into_response(self) -> Response {
22        let status = match &self {
23            AiError::NoModel | AiError::EmptyPrompt => 500,
24            AiError::Sdk(_) => 502,
25        };
26        Response::text(self.to_string()).status(status)
27    }
28}
29
30impl From<AiError> for sova_core::Error {
31    fn from(value: AiError) -> Self {
32        sova_core::Error::Internal(value.to_string())
33    }
34}