ctb_io_webui/error.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later AND MIT
2// SPDX-License-Identifier for parts derived from tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs: MIT
3/*
4This file is part of Collective Toolbox, a database and document workspace and utilities.
5Copyright (C) 2026 Collective Toolbox Developers
6Contact: info@collectivetoolbox.com
7
8This program is free software: you can redistribute it and/or modify it under
9the terms of the GNU Affero General Public License as published by the Free
10Software Foundation, either version 3 of the License, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
16
17You should have received a copy of the GNU Affero General Public License along
18with this program. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21// Based on:
22// <https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs>
23/* MIT License
24
25Copyright (c) 2019–2025 axum Contributors
26
27Permission is hereby granted, free of charge, to any
28person obtaining a copy of this software and associated
29documentation files (the "Software"), to deal in the
30Software without restriction, including without
31limitation the rights to use, copy, modify, merge,
32publish, distribute, sublicense, and/or sell copies of
33the Software, and to permit persons to whom the Software
34is furnished to do so, subject to the following
35conditions:
36
37The above copyright notice and this permission notice
38shall be included in all copies or substantial portions
39of the Software.
40
41THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
42ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
43TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
44PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
45SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
47OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
48IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49DEALINGS IN THE SOFTWARE.*/
50
51//! Anyhow error handling support for Axum.
52
53#[allow(
54 unused_imports,
55 clippy::wildcard_imports,
56 reason = "Standard workspace module prelude"
57)]
58use crate::utilities::*;
59
60use crate::AppState;
61use crate::RequestState;
62use crate::error_500;
63use axum::response::{IntoResponse, Response};
64
65/// A custom result type for web handlers.
66pub type WebResult<T> = std::result::Result<T, WebError>;
67
68// Make our own error that wraps `anyhow::Error` and includes state.
69pub struct WebError {
70 error: anyhow::Error,
71 state: AppState,
72 req: RequestState,
73}
74
75impl WebError {
76 /// Create a new `WebError` with the given error, state, and request.
77 pub fn new<E>(error: E, state: AppState, req: RequestState) -> Self
78 where
79 E: Into<anyhow::Error>,
80 {
81 Self {
82 error: error.into(),
83 state,
84 req,
85 }
86 }
87}
88
89// Tell axum how to convert `WebError` into a response.
90impl IntoResponse for WebError {
91 fn into_response(self) -> Response {
92 error_500(&self.state, &self.req, self.error)
93 }
94}
95
96/// A trait to extend `Result` and `Option` with error handling that injects `AppState` and `RequestState`.
97pub trait WebErr<T> {
98 fn web_err(self, state: &AppState, req: &RequestState) -> WebResult<T>;
99}
100
101impl<T, E> WebErr<T> for Result<T, E>
102where
103 E: Into<anyhow::Error>,
104{
105 fn web_err(self, state: &AppState, req: &RequestState) -> WebResult<T> {
106 self.map_err(|e| WebError::new(e, state.clone(), req.clone()))
107 }
108}
109
110impl<T> WebErr<T> for Option<T> {
111 fn web_err(self, state: &AppState, req: &RequestState) -> WebResult<T> {
112 self.ok_or_else(|| {
113 WebError::new(
114 anyhow::anyhow!("Unexpected None"),
115 state.clone(),
116 req.clone(),
117 )
118 })
119 }
120}