forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiBdebc8fbTahinli1 day ago352 commits

error.rs80 lines, 2.0 KiBHistoryRawDownload
1use tracing::error;
2
3#[derive(Debug, thiserror::Error)]
4pub enum ServerError {
5 #[error(transparent)]
6 Wire(#[from] common::error::Error),
7 #[error("{0}")]
8 Internal(String),
9}
10
11impl From<ServerError> for common::error::Error {
12 fn from(value: ServerError) -> Self {
13 match value {
14 ServerError::Wire(error) => error,
15 ServerError::Internal(message) => {
16 error!("{message}");
17
18 common::error::Error::Server
19 }
20 }
21 }
22}
23
24impl From<turso::Error> for ServerError {
25 fn from(error: turso::Error) -> Self {
26 match &error {
27 turso::Error::Constraint(message)
28 if message.starts_with("UNIQUE constraint failed") =>
29 {
30 common::error::Error::Taken.into()
31 }
32 turso::Error::Constraint(message)
33 if message.starts_with("FOREIGN KEY constraint failed") =>
34 {
35 common::error::Error::NotFound.into()
36 }
37 _ => ServerError::Internal(error.to_string()),
38 }
39 }
40}
41
42pub fn ensure(condition: bool) -> Result<(), ServerError> {
43 ensure_with(condition, common::error::Error::Forbidden)
44}
45
46pub fn ensure_with(condition: bool, error: common::error::Error) -> Result<(), ServerError> {
47 if !condition {
48 return Err(error.into());
49 }
50
51 Ok(())
52}
53
54macro_rules! internal_from {
55 ($($source:ty),+ $(,)?) => {
56 $(
57 impl From<$source> for ServerError {
58 fn from(value: $source) -> Self {
59 ServerError::Internal(value.to_string())
60 }
61 }
62 )+
63 };
64}
65
66internal_from! {
67 std::net::AddrParseError,
68 std::io::Error,
69 tokio::task::JoinError,
70 quinn::ConnectionError,
71 quinn::ReadError,
72 quinn::ReadExactError,
73 quinn::WriteError,
74 quinn::ClosedStream,
75 bitcode::Error,
76 common::config::ConfigError,
77 common::frame::FrameError,
78 common::update::UpdateError,
79 tokio::time::error::Elapsed,
80}