cogo_http/multipart/
error.rs

1// Copyright 2016 mime-multipart Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use 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
15/// An error type for the `mime-multipart` crate.
16pub enum Error {
17    /// The cogo-http request did not have a Content-Type header.
18    NoRequestContentType,
19    /// The cogo-http request Content-Type top-level Mime was not `Multipart`.
20    NotMultipart,
21    /// The Content-Type header failed to specify boundary token.
22    BoundaryNotSpecified,
23    /// A multipart section contained only partial headers.
24    PartialHeaders,
25    EofInMainHeaders,
26    EofBeforeFirstBoundary,
27    NoCrLfAfterBoundary,
28    EofInPartHeaders,
29    EofInFile,
30    EofInPart,
31    /// An HTTP parsing error from a multipart section.
32    Httparse(httparse::Error),
33    /// An I/O error.
34    Io(io::Error),
35    /// An error was returned from Hyper.
36    Hyper(crate::Error),
37    /// An error occurred during UTF-8 processing.
38    Utf8(FromUtf8Error),
39    /// An error occurred during character decoding
40    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())?; // recurse
129        }
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}