error.rs
⎇
Raw
1use std::path::PathBuf;
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5
6use crate::db::Db;
7
8#[derive(Clone)]
9pub struct AppState {
10 pub db: Db,
11 /// Canonicalized server root folder.
12 pub root: PathBuf,
13 /// Whether we sit behind a TLS-terminating reverse proxy.
14 pub https: bool,
15}
16
17#[derive(Debug, thiserror::Error)]
18#[error("{1}")]
19pub struct ApiError(pub StatusCode, pub String);
20
21impl ApiError {
22 pub fn new(status: StatusCode, msg: impl Into<String>) -> Self {
23 Self(status, msg.into())
24 }
25}
26
27impl IntoResponse for ApiError {
28 fn into_response(self) -> Response {
29 (
30 self.0,
31 axum::Json(serde_json::json!({ "error": self.1 })),
32 )
33 .into_response()
34 }
35}
36
37impl From<rusqlite::Error> for ApiError {
38 fn from(e: rusqlite::Error) -> Self {
39 tracing::error!(error = %e, "database error");
40 ApiError::new(StatusCode::INTERNAL_SERVER_ERROR, "internal error")
41 }
42}
43