Echo Writes Code

errors.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use axum::http::{ StatusCode };
use axum::response::{ IntoResponse, Response };

pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
  AuthenticationFailed,
  FromKdl(kdl::KdlError),
  FromPasswordHash(password_hash::Error),
  FromSerdeJson(serde_json::Error),
  FromStdIo(std::io::Error),
  FromTera(tera::Error),
  FromTowerSessions(tower_sessions::session::Error),
  PermissionDenied,
  PageAlreadyExists(String),
  PageNotFound(String),
}

impl IntoResponse for Error {
  fn into_response(self) -> Response {
    tracing::error!("{}", self);

    let (status, message) = match self {
      Error::AuthenticationFailed => (StatusCode::FORBIDDEN, "Authentication failed."),
      Error::PermissionDenied => (StatusCode::FORBIDDEN, "Permission denied."),
      Error::PageAlreadyExists(_) => (StatusCode::BAD_REQUEST, "Page already exists."),
      Error::PageNotFound(_) => (StatusCode::NOT_FOUND, "Page not found."),
      _ => (StatusCode::INTERNAL_SERVER_ERROR, "The server encountered an unexpected error."),
    };

    (status, message).into_response()
  }
}

impl std::fmt::Display for Error {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    match *self {
      Error::AuthenticationFailed => write!(f, "Authentication failed"),
      Error::FromKdl(ref e) => write!(f, "{}", e),
      Error::FromPasswordHash(ref e) => write!(f, "{}", e),
      Error::FromSerdeJson(ref e) => write!(f, "{}", e),
      Error::FromStdIo(ref e) => write!(f, "{}", e),
      Error::FromTera(ref e) => write!(f, "{}", e),
      Error::FromTowerSessions(ref e) => write!(f, "{}", e),
      Error::PermissionDenied => write!(f, "Permission denied"),
      Error::PageAlreadyExists(ref slug) => write!(f, "Page already exists: {}", slug),
      Error::PageNotFound(ref slug) => write!(f, "Page not found: {}", slug),
    }
  }
}

impl std::error::Error for Error {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    match *self {
      Error::FromKdl(ref e) => Some(e),
      Error::FromPasswordHash(ref e) => Some(e),
      Error::FromSerdeJson(ref e) => Some(e),
      Error::FromStdIo(ref e) => Some(e),
      Error::FromTera(ref e) => Some(e),
      Error::FromTowerSessions(ref e) => Some(e),
      _ => None,
    }
  }
}

impl From<kdl::KdlError> for Error {
  fn from(e: kdl::KdlError) -> Error {
    Error::FromKdl(e)
  }
}

impl From<password_hash::Error> for Error {
  fn from(e: password_hash::Error) -> Error {
    Error::FromPasswordHash(e)
  }
}

impl From<serde_json::Error> for Error {
  fn from(e: serde_json::Error) -> Error {
    Error::FromSerdeJson(e)
  }
}

impl From<std::io::Error> for Error {
  fn from(e: std::io::Error) -> Error {
    Error::FromStdIo(e)
  }
}

impl From<tera::Error> for Error {
  fn from(e: tera::Error) -> Error {
    Error::FromTera(e)
  }
}

impl From<tower_sessions::session::Error> for Error {
  fn from(e: tower_sessions::session::Error) -> Error {
    Error::FromTowerSessions(e)
  }
}