-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
97 lines (83 loc) · 3.01 KB
/
Copy patherror.rs
File metadata and controls
97 lines (83 loc) · 3.01 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
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
use std::{error::Error, fmt::write};
use std::fmt;
use serde_derive::Serialize;
use actix_web::{dev::HttpResponseBuilder, error, http::header, http::StatusCode, HttpResponse};
#[derive(Debug, Serialize)]
pub enum ErrorKind {
OddLength(usize/*odd length*/),
SliceLengthError(usize/*expected_length*/, usize/*actual_length*/),
UnknowPageType(u8/*the page type found*/),
NotImplemented,
InvalidVarInt,
UnreachableCode,
}
#[derive(Debug, Serialize)]
pub struct MyError {
error_kind: ErrorKind,
}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let result = match &self.error_kind {
ErrorKind::OddLength(odd_length) => write!(f, "The slice length should not be odd {}", odd_length),
ErrorKind::SliceLengthError(expected_length, actual_length) => write!(f, "The length of slice is not correct! The expected length is {}, but the actual length is {}", expected_length, actual_length),
ErrorKind::UnknowPageType(actual_page_type) => write!(f, "The page type {} doesn't exist.", actual_page_type),
ErrorKind::NotImplemented => write!(f, "Function not implemented."),
ErrorKind::InvalidVarInt => write!(f, "Invalid Variable-Length Integer."),
ErrorKind::UnreachableCode => write!(f, "Unreachable code."),
};
result
}
}
impl Error for MyError {}
impl MyError {
pub fn new(error_kind: ErrorKind) -> Self {
MyError {
error_kind,
}
}
}
#[derive(Debug, Serialize)]
pub enum HttpErrorKind{
InternalError(String),
BadClientData(String),
Timeout(String),
PageIndexError(usize),
}
#[derive(Debug, Serialize)]
pub struct HttpError {
error_kind: HttpErrorKind,
}
impl fmt::Display for HttpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let result = match &self.error_kind {
HttpErrorKind::InternalError(msg) => write!(f, "{}", msg),
HttpErrorKind::BadClientData(msg) => write!(f, "{}", msg),
HttpErrorKind::Timeout(msg) => write!(f, "{}", msg),
HttpErrorKind::PageIndexError(page_idx) => write!(f, "Page index error: {}", page_idx),
};
result
}
}
impl Error for HttpError {}
impl HttpError {
pub fn new(error_kind: HttpErrorKind) -> Self {
HttpError {
error_kind,
}
}
}
impl error::ResponseError for HttpError {
fn error_response(&self) -> HttpResponse {
HttpResponseBuilder::new(self.status_code())
.set_header(header::CONTENT_TYPE, "application/json; charset=utf-8")
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
match self.error_kind {
HttpErrorKind::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
HttpErrorKind::BadClientData(_) => StatusCode::BAD_REQUEST,
HttpErrorKind::Timeout(_) => StatusCode::BAD_GATEWAY,
HttpErrorKind::PageIndexError(_) => StatusCode::BAD_REQUEST,
}
}
}