1use std::fmt::Display;
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5use axum::Json;
6
7#[derive(Debug)]
10pub struct AppError {
11 pub code: StatusCode,
12 pub message: String,
13}
14
15impl Display for AppError {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 write!(f, "Code: {}; {};", self.code.as_u16(), self.message)
18 }
19}
20
21impl AppError {
22 pub fn new(code: StatusCode, message: impl ToString) -> Self {
23 Self {
24 code,
25 message: message.to_string(),
26 }
27 }
28
29 pub fn not_found() -> Self {
30 Self {
31 code: StatusCode::NOT_FOUND,
32 message: "Not Found".to_string(),
33 }
34 }
35
36 pub fn server_error(message: impl ToString) -> Self {
37 Self {
38 code: StatusCode::INTERNAL_SERVER_ERROR,
39 message: message.to_string(),
40 }
41 }
42
43 pub fn bad_request(message: impl ToString) -> Self {
44 Self {
45 code: StatusCode::BAD_REQUEST,
46 message: message.to_string(),
47 }
48 }
49
50 pub fn from(obj: impl ToString) -> Self {
52 Self {
53 code: StatusCode::INTERNAL_SERVER_ERROR,
54 message: obj.to_string(),
55 }
56 }
57}
58
59impl IntoResponse for AppError {
60 fn into_response(self) -> Response {
61 (self.code, self.message).into_response()
62 }
63}
64
65pub type AppResult<T> = Result<T, AppError>;
67pub type JsonResult<T> = AppResult<Json<T>>;
68
69pub fn json_ok<T>(obj: T) -> JsonResult<T> {
70 Ok(Json(obj))
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_fmt() {
79 let err = AppError {
80 code: StatusCode::OK,
81 message: "ok".to_string(),
82 };
83
84 assert_eq!(err.to_string(), "Code: 200; ok;");
85 }
86
87 #[test]
89 fn test_from() {
90 let err2: AppError = AppError::from("hi");
91
92 assert_eq!(err2.message, "hi");
93 assert_eq!(err2.code, StatusCode::INTERNAL_SERVER_ERROR);
94 }
95
96 #[test]
98 fn test_json() {
99 let resp: JsonResult<String> = json_ok("hi".to_string());
100 assert_eq!(resp.unwrap().to_string(), "hi");
101 }
102
103 #[test]
104 fn test_traits() {
105 assert_eq!(AppError::new(StatusCode::FORBIDDEN, "hi").message, "hi");
106 assert_eq!(
107 AppError::new(StatusCode::FORBIDDEN, "hi".to_string()).message,
108 "hi"
109 );
110 }
111}