Skip to main content

systemprompt_api/error/
mod.rs

1//! Entry-local HTTP error type for non-OAuth API routes.
2//!
3//! Handlers return `Result<_, ApiHttpError>` and propagate domain, repository,
4//! and service errors with `?`. The variant-to-HTTP-status mapping lives once,
5//! in the `conversions` submodule's `From` impls, so `domain/*` never
6//! references the HTTP envelope and the boundary decides the status code from
7//! the error variant rather than at each call site.
8//!
9//! The wire shape is the shared [`ApiError`] JSON envelope; [`ApiHttpError`] is
10//! a thin entry-local newtype whose only reason to exist is the orphan rule —
11//! `impl From<DomainError> for ApiError` is forbidden in this crate (both types
12//! are foreign), so a local target type is required to obtain bare `?`.
13//! `into_response` delegates to `ApiError`, which logs exactly once by status
14//! class.
15//!
16//! Copyright (c) systemprompt.io — Business Source License 1.1.
17//! See <https://systemprompt.io> for licensing details.
18
19mod conversions;
20
21use axum::response::{IntoResponse, Response};
22use systemprompt_models::api::ApiError;
23
24#[derive(Debug)]
25pub struct ApiHttpError(ApiError);
26
27impl ApiHttpError {
28    pub fn not_found(message: impl Into<String>) -> Self {
29        Self(ApiError::not_found(message))
30    }
31
32    pub fn bad_request(message: impl Into<String>) -> Self {
33        Self(ApiError::bad_request(message))
34    }
35
36    pub fn unauthorized(message: impl Into<String>) -> Self {
37        Self(ApiError::unauthorized(message))
38    }
39
40    pub fn forbidden(message: impl Into<String>) -> Self {
41        Self(ApiError::forbidden(message))
42    }
43
44    pub fn internal_error(message: impl Into<String>) -> Self {
45        Self(ApiError::internal_error(message))
46    }
47
48    pub fn into_inner(self) -> ApiError {
49        self.0
50    }
51}
52
53impl From<ApiError> for ApiHttpError {
54    fn from(error: ApiError) -> Self {
55        Self(error)
56    }
57}
58
59impl IntoResponse for ApiHttpError {
60    fn into_response(self) -> Response {
61        self.0.into_response()
62    }
63}