cogo_http/multipart/
error.rs1use std::borrow::Cow;
9use std::error::Error as StdError;
10use std::fmt::{self, Debug, Display};
11use std::io;
12use std::string::FromUtf8Error;
13use httparse;
14
15pub enum Error {
17 NoRequestContentType,
19 NotMultipart,
21 BoundaryNotSpecified,
23 PartialHeaders,
25 EofInMainHeaders,
26 EofBeforeFirstBoundary,
27 NoCrLfAfterBoundary,
28 EofInPartHeaders,
29 EofInFile,
30 EofInPart,
31 Httparse(httparse::Error),
33 Io(io::Error),
35 Hyper(crate::Error),
37 Utf8(FromUtf8Error),
39 Decoding(Cow<'static, str>),
41
42 MissingDisposition,
43 NoName
44}
45
46impl From<io::Error> for Error {
47 fn from(err: io::Error) -> Error {
48 Error::Io(err)
49 }
50}
51
52impl From<httparse::Error> for Error {
53 fn from(err: httparse::Error) -> Error {
54 Error::Httparse(err)
55 }
56}
57
58impl From<crate::Error> for Error {
59 fn from(err: crate::Error) -> Error {
60 Error::Hyper(err)
61 }
62}
63
64impl From<FromUtf8Error> for Error {
65 fn from(err: FromUtf8Error) -> Error {
66 Error::Utf8(err)
67 }
68}
69
70impl Display for Error {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 match self {
73 Error::Httparse(ref e) =>
74 std::fmt::Display::fmt(&e, f),
75 Error::Io(ref e) =>
76 std::fmt::Display::fmt(&e, f),
77 Error::Hyper(ref e) =>
78 std::fmt::Display::fmt(&e, f),
79 Error::Utf8(ref e) =>
80 std::fmt::Display::fmt(&e, f),
81 Error::Decoding(ref e) =>
82 std::fmt::Display::fmt(&e, f),
83
84 Error::NoRequestContentType => {
85 f.write_str("NoRequestContentType")
86 }
87 Error::NotMultipart => {
88 f.write_str("NotMultipart")
89 }
90 Error::BoundaryNotSpecified => {
91 f.write_str("BoundaryNotSpecified")
92 }
93 Error::PartialHeaders => {
94 f.write_str("PartialHeaders")
95 }
96 Error::EofInMainHeaders => {
97 f.write_str("EofInMainHeaders")
98 }
99 Error::EofBeforeFirstBoundary => {
100 f.write_str("EofBeforeFirstBoundary")
101 }
102 Error::NoCrLfAfterBoundary => {
103 f.write_str("NoCrLfAfterBoundary")
104 }
105 Error::EofInPartHeaders => {
106 f.write_str("EofInPartHeaders")
107 }
108 Error::EofInFile => {
109 f.write_str("EofInFile")
110 }
111 Error::EofInPart => {
112 f.write_str("EofInPart")
113 }
114 Error::MissingDisposition => {
115 f.write_str("MissingDisposition")
116 }
117 Error::NoName => {
118 f.write_str("NoName")
119 }
120 }
121 }
122}
123
124impl fmt::Debug for Error {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "{}", self)?;
127 if self.source().is_some() {
128 write!(f, ": {:?}", self.source().unwrap())?; }
130 Ok(())
131 }
132}
133
134impl StdError for Error {
135 fn description(&self) -> &str{
136 match *self {
137 Error::NoRequestContentType => "The cogo-http request did not have a Content-Type header.",
138 Error::NotMultipart =>
139 "The cogo-http request Content-Type top-level Mime was not multipart.",
140 Error::BoundaryNotSpecified =>
141 "The Content-Type header failed to specify a boundary token.",
142 Error::PartialHeaders =>
143 "A multipart section contained only partial headers.",
144 Error::EofInMainHeaders =>
145 "The request headers ended pre-maturely.",
146 Error::EofBeforeFirstBoundary =>
147 "The request body ended prior to reaching the expected starting boundary.",
148 Error::NoCrLfAfterBoundary =>
149 "Missing CRLF after boundary.",
150 Error::EofInPartHeaders =>
151 "The request body ended prematurely while parsing headers of a multipart part.",
152 Error::EofInFile =>
153 "The request body ended prematurely while streaming a file part.",
154 Error::EofInPart =>
155 "The request body ended prematurely while reading a multipart part.",
156 Error::Httparse(_) =>
157 "A parse error occurred while parsing the headers of a multipart section.",
158 Error::Io(_) => "An I/O error occurred.",
159 Error::Hyper(_) => "A cogo-http error occurred.",
160 Error::Utf8(_) => "A UTF-8 error occurred.",
161 Error::Decoding(_) => "A decoding error occurred.",
162 Error::MissingDisposition => "MissingDisposition",
163 Error::NoName => "no name",
164 }
165 }
166}