-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttperror.rs
More file actions
57 lines (46 loc) · 1.49 KB
/
Copy pathhttperror.rs
File metadata and controls
57 lines (46 loc) · 1.49 KB
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
use std::{error::Error as StdError, fmt::Display};
use anyhow::Error;
#[derive(Debug)]
pub struct HttpError {
pub code: u32,
pub message: String,
pub source: Option<Box<dyn StdError + Send + Sync + 'static>>,
}
impl HttpError {
pub fn new(message: String) -> Error {
Error::new(Self { code: 500, message, source: None })
}
pub fn new_with_code(code: u32, message: String, ) -> Error {
Error::new(Self { code, message, source: None })
}
pub fn new_with_source<E>(message: String, source: E) -> Error
where
E: StdError + Sync + Send + 'static,
{
Error::new(Self { code: 500, message, source: Some(Box::new(source)) })
}
pub fn new_with_full<E>(code: u32, message: String, source: E) -> Error
where
E: StdError + Sync + Send + 'static,
{
Error::new(Self { code, message, source: Some(Box::new(source)) })
}
}
impl StdError for HttpError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match &self.source {
Some(s) => Some(s.as_ref()),
None => None,
}
}
}
impl Display for HttpError {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "code = {}, message = {}",
self.code, self.message)?;
if let Some(source) = &self.source {
write!(formatter, ", source = {source:?}")?;
}
Ok(())
}
}