refactor: ollama errors centralized

This commit is contained in:
2026-05-11 11:16:16 +02:00
parent 7bc489939f
commit b77715268d
9 changed files with 78 additions and 111 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
use super::errors::OllamaError;
use crate::dto::{api, ollama};
use crate::errors::OllamaError;
use axum::response::sse::Event;
use futures::StreamExt;
use reqwest::Client;
+58
View File
@@ -0,0 +1,58 @@
// errors.rs
use thiserror::Error;
#[derive(Debug, Error)]
pub enum OllamaError {
#[error("prompt is required and cannot be empty")]
MissingPrompt,
#[error("model is required and cannot be empty")]
MissingModel,
#[error("messages must be a non-empty array containing at least one user message")]
MissingMessages,
#[error("model '{0}' is not available — run `ollama pull {0}` first")]
ModelNotFound(String),
#[error("keep_alive is required and cannot be empty")]
MissingKeepAlive,
#[error(
"invalid keep_alive format '{0}' — expected <number><unit> (e.g. 30s, 10m, 2h), a plain integer (seconds), or -1"
)]
InvalidKeepAlive(String),
#[error(transparent)]
Http(#[from] reqwest::Error),
}
pub fn into_http_response(e: OllamaError) -> (axum::http::StatusCode, String) {
match e {
OllamaError::MissingPrompt => (
axum::http::StatusCode::BAD_REQUEST,
"prompt is required and cannot be empty".to_string(),
),
OllamaError::MissingModel => (
axum::http::StatusCode::BAD_REQUEST,
"model is required and cannot be empty".to_string(),
),
OllamaError::ModelNotFound(m) => (
axum::http::StatusCode::UNPROCESSABLE_ENTITY,
format!("model '{m}' is not available — run `ollama pull {m}` first"),
),
OllamaError::MissingKeepAlive => (
axum::http::StatusCode::BAD_REQUEST,
"keep alive is required and cannot be empty".to_string(),
),
OllamaError::InvalidKeepAlive(v) => (
axum::http::StatusCode::BAD_REQUEST,
format!("invalid keep_alive '{v}'"),
),
OllamaError::MissingMessages => (
axum::http::StatusCode::BAD_REQUEST,
"messages array with at least one user message is required".to_string(),
),
OllamaError::Http(e) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
}
}
+1
View File
@@ -1,2 +1,3 @@
pub mod client;
pub mod errors;
pub mod mapper;