refactor: ollama errors centralized
This commit is contained in:
+5
-35
@@ -8,7 +8,7 @@ use axum::{
|
||||
};
|
||||
|
||||
use crate::dto::api;
|
||||
use crate::errors::OllamaError;
|
||||
use crate::providers::ollama::errors::into_http_response;
|
||||
use crate::state::app_state::AppState;
|
||||
|
||||
#[utoipa::path(
|
||||
@@ -54,7 +54,7 @@ pub async fn completions(
|
||||
tracing::debug!("Received /completion with body {:?}", body);
|
||||
if body.base.stream {
|
||||
let stream = state.ollama.completions_stream(&body).await.map_err(|e| {
|
||||
let (code, msg) = ollama_err(e);
|
||||
let (code, msg) = into_http_response(e);
|
||||
(code, Json(api::ErrorResponse::new(msg)))
|
||||
})?;
|
||||
|
||||
@@ -63,7 +63,7 @@ pub async fn completions(
|
||||
.into_response())
|
||||
} else {
|
||||
let response = state.ollama.completions(&body).await.map_err(|e| {
|
||||
let (code, msg) = ollama_err(e);
|
||||
let (code, msg) = into_http_response(e);
|
||||
(code, Json(api::ErrorResponse::new(msg)))
|
||||
})?;
|
||||
|
||||
@@ -123,7 +123,7 @@ pub async fn chat_completions(
|
||||
.ollama
|
||||
.chat_completions_stream(&body)
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
.map_err(into_http_response)?;
|
||||
|
||||
Ok(Sse::new(stream)
|
||||
.keep_alive(KeepAlive::default())
|
||||
@@ -133,38 +133,8 @@ pub async fn chat_completions(
|
||||
.ollama
|
||||
.chat_completions(&body)
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
.map_err(into_http_response)?;
|
||||
|
||||
Ok(Json(response).into_response())
|
||||
}
|
||||
}
|
||||
|
||||
fn ollama_err(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()),
|
||||
}
|
||||
}
|
||||
|
||||
+5
-34
@@ -4,7 +4,8 @@ use axum::{
|
||||
};
|
||||
|
||||
use crate::dto::api;
|
||||
use crate::{errors::OllamaError, state::app_state::AppState};
|
||||
use crate::providers::ollama::errors::into_http_response;
|
||||
use crate::state::app_state::AppState;
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
@@ -30,7 +31,7 @@ pub async fn list_models(
|
||||
) -> Result<Json<api::ModelsResponse>, (axum::http::StatusCode, String)> {
|
||||
match state.ollama.list_models().await {
|
||||
Ok(models) => Ok(Json(models)),
|
||||
Err(e) => Err(ollama_err(e)),
|
||||
Err(e) => Err(into_http_response(e)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +87,7 @@ pub async fn load_model(
|
||||
.ollama
|
||||
.load_model(&model, body.keep_alive.as_deref())
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
.map_err(into_http_response)?;
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
@@ -127,37 +128,7 @@ pub async fn unload_model(
|
||||
.ollama
|
||||
.unload_model(&model)
|
||||
.await
|
||||
.map_err(ollama_err)?;
|
||||
.map_err(into_http_response)?;
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
|
||||
fn ollama_err(e: OllamaError) -> (axum::http::StatusCode, String) {
|
||||
match e {
|
||||
OllamaError::ModelNotFound(m) => (
|
||||
axum::http::StatusCode::NOT_FOUND,
|
||||
format!("model '{m}' not found — run `ollama pull {m}`"),
|
||||
),
|
||||
OllamaError::MissingModel => (
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
"model is required and cannot be empty".to_string(),
|
||||
),
|
||||
OllamaError::MissingPrompt => (
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
"prompt is required".to_string(),
|
||||
),
|
||||
OllamaError::MissingMessages => (
|
||||
axum::http::StatusCode::BAD_REQUEST,
|
||||
"messages array with at least one user message is required".to_string(),
|
||||
),
|
||||
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}' — use 30s / 10m / 2h, a plain integer, or -1"),
|
||||
),
|
||||
OllamaError::Http(e) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user