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
102
103
104
105
106
107
108
109
110
111
112
113
use std::fmt::{self, Debug, Display, Formatter};
type BoxError = Box<dyn std::error::Error + Send + Sync>;
#[non_exhaustive]
pub enum Error {
UnknownField { field_name: Option<String> },
IncompleteFieldData { field_name: Option<String> },
IncompleteHeaders,
ReadHeaderFailed(httparse::Error),
DecodeHeaderName { name: String, cause: BoxError },
DecodeHeaderValue { value: Vec<u8>, cause: BoxError },
IncompleteStream,
FieldSizeExceeded { limit: u64, field_name: Option<String> },
StreamSizeExceeded { limit: u64 },
StreamReadFailed(BoxError),
LockFailure,
NoMultipart,
DecodeContentType(mime::FromStrError),
NoBoundary,
#[cfg(feature = "json")]
#[cfg_attr(nightly, doc(cfg(feature = "json")))]
DecodeJson(serde_json::Error),
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self, f)
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Error::UnknownField { field_name } => {
let name = field_name.as_deref().unwrap_or("<unknown>");
write!(f, "unknown field received: {:?}", name)
}
Error::IncompleteFieldData { field_name } => {
let name = field_name.as_deref().unwrap_or("<unknown>");
write!(f, "field {:?} received with incomplete data", name)
}
Error::DecodeHeaderName { name, cause } => {
write!(f, "failed to decode field's raw header name: {:?} {}", name, cause)
}
Error::DecodeHeaderValue { cause, .. } => {
write!(f, "failed to decode field's raw header value: {}", cause)
}
Error::FieldSizeExceeded { limit, field_name } => {
let name = field_name.as_deref().unwrap_or("<unknown>");
write!(f, "field {:?} exceeded the size limit: {} bytes", name, limit)
}
Error::StreamSizeExceeded { limit } => {
write!(f, "stream size exceeded limit: {} bytes", limit)
}
Error::ReadHeaderFailed(e) => write!(f, "failed to read headers: {}", e),
Error::StreamReadFailed(e) => write!(f, "failed to read stream: {}", e),
Error::DecodeContentType(e) => write!(f, "failed to decode Content-Type: {}", e),
Error::IncompleteHeaders => write!(f, "failed to read field complete headers"),
Error::IncompleteStream => write!(f, "incomplete multipart stream"),
Error::LockFailure => write!(f, "failed to lock multipart state"),
Error::NoMultipart => write!(f, "Content-Type is not multipart/form-data"),
Error::NoBoundary => write!(f, "multipart boundary not found in Content-Type"),
#[cfg(feature = "json")]
Error::DecodeJson(e) => write!(f, "failed to decode field data as JSON: {}", e),
}
}
}
impl std::error::Error for Error {}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.to_string().eq(&other.to_string())
}
}
impl Eq for Error {}