blob: ef5374cd591e30453ba566940f8d0a25f5895334 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// TODO: Slight™ code duplication with `ui/error.rs`
use rocket::{
response::{self, Responder},
Request,
};
use serde_json::{json, Value};
use std::fmt::Display;
use crate::routes::ui::error::MyError;
pub type ApiResult<T> = Result<T, ApiError>;
#[derive(Debug)]
pub struct ApiError(pub anyhow::Error);
impl<'r> Responder<'r, 'static> for ApiError {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
json!({ "error": format!("{}", self.0) }).respond_to(req)
}
}
impl Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl From<anyhow::Error> for ApiError {
fn from(err: anyhow::Error) -> ApiError {
ApiError(err)
}
}
impl From<std::fmt::Error> for ApiError {
fn from(err: std::fmt::Error) -> ApiError {
ApiError(anyhow::anyhow!("{err}"))
}
}
impl From<std::io::Error> for ApiError {
fn from(err: std::io::Error) -> Self {
ApiError(anyhow::anyhow!("{err}"))
}
}
impl From<sled::Error> for ApiError {
fn from(err: sled::Error) -> Self {
ApiError(anyhow::anyhow!("{err}"))
}
}
impl From<serde_json::Error> for ApiError {
fn from(err: serde_json::Error) -> Self {
ApiError(anyhow::anyhow!("{err}"))
}
}
impl From<MyError> for ApiError {
fn from(value: MyError) -> Self {
Self(value.0)
}
}
|