harddrive_party/
errors.rs

1use crate::{
2    connections::discovery::hole_punch::HolePunchError,
3    shares::{EntryParseError, ScanDirError},
4    ui_messages::UiServerError,
5    wishlist::DbError,
6    RequestError,
7};
8use axum::{
9    http::StatusCode,
10    response::{IntoResponse, Response},
11    Json,
12};
13use serde::{Deserialize, Serialize};
14use thiserror::Error;
15
16/// An error in response to a UI command
17#[derive(Serialize, Deserialize, PartialEq, Debug, Error, Clone)]
18pub struct UiServerErrorWrapper(pub UiServerError);
19
20impl std::fmt::Display for UiServerErrorWrapper {
21    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
22        write!(f, "{}", self.0)
23    }
24}
25
26impl From<UiServerError> for UiServerErrorWrapper {
27    fn from(error: UiServerError) -> UiServerErrorWrapper {
28        UiServerErrorWrapper(error)
29    }
30}
31
32impl<T> From<std::sync::PoisonError<T>> for UiServerErrorWrapper {
33    fn from(_error: std::sync::PoisonError<T>) -> UiServerErrorWrapper {
34        UiServerErrorWrapper(UiServerError::Poison)
35    }
36}
37
38impl From<HolePunchError> for UiServerErrorWrapper {
39    fn from(error: HolePunchError) -> UiServerErrorWrapper {
40        UiServerErrorWrapper(UiServerError::PeerDiscovery(error.to_string()))
41    }
42}
43
44impl From<tokio::sync::oneshot::error::RecvError> for UiServerErrorWrapper {
45    fn from(error: tokio::sync::oneshot::error::RecvError) -> UiServerErrorWrapper {
46        UiServerErrorWrapper(UiServerError::PeerDiscovery(error.to_string()))
47    }
48}
49
50impl From<DbError> for UiServerErrorWrapper {
51    fn from(error: DbError) -> UiServerErrorWrapper {
52        UiServerErrorWrapper(UiServerError::Db(error.to_string()))
53    }
54}
55
56impl From<EntryParseError> for UiServerErrorWrapper {
57    fn from(error: EntryParseError) -> UiServerErrorWrapper {
58        UiServerErrorWrapper(UiServerError::Db(error.to_string()))
59    }
60}
61
62impl From<RequestError> for UiServerErrorWrapper {
63    fn from(error: RequestError) -> UiServerErrorWrapper {
64        UiServerErrorWrapper(UiServerError::RequestError(error.to_string()))
65    }
66}
67
68impl From<ScanDirError> for UiServerErrorWrapper {
69    fn from(error: ScanDirError) -> UiServerErrorWrapper {
70        UiServerErrorWrapper(UiServerError::AddShare(error.to_string()))
71    }
72}
73
74impl From<quinn::ConnectionError> for UiServerErrorWrapper {
75    fn from(error: quinn::ConnectionError) -> UiServerErrorWrapper {
76        UiServerErrorWrapper(UiServerError::ConnectionError(error.to_string()))
77    }
78}
79
80impl From<sled::Error> for UiServerErrorWrapper {
81    fn from(error: sled::Error) -> UiServerErrorWrapper {
82        UiServerErrorWrapper(UiServerError::Db(error.to_string()))
83    }
84}
85
86impl From<Box<bincode::ErrorKind>> for UiServerErrorWrapper {
87    fn from(error: Box<bincode::ErrorKind>) -> UiServerErrorWrapper {
88        UiServerErrorWrapper(UiServerError::Db(error.to_string()))
89    }
90}
91
92impl IntoResponse for UiServerErrorWrapper {
93    fn into_response(self) -> Response {
94        log::error!("{self:?}");
95        (StatusCode::INTERNAL_SERVER_ERROR, Json(self.0)).into_response()
96    }
97}