mime_multipart_hyper1/
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::error::Error as StdError;
9use std::fmt::{self, Display};
10use std::io;
11use std::string::FromUtf8Error;
12
13use http;
14use http::header::ToStrError;
15use httparse;
16
17/// An error type for the `mime-multipart` crate.
18pub enum Error {
19    /// The Hyper request did not have a Content-Type header.
20    NoRequestContentType,
21    /// The Hyper request Content-Type top-level Mime was not `Multipart`.
22    NotMultipart,
23    /// The Content-Type header failed to specify boundary token.
24    BoundaryNotSpecified,
25    /// A multipart section contained only partial headers.
26    PartialHeaders,
27    EofInMainHeaders,
28    EofBeforeFirstBoundary,
29    NoCrLfAfterBoundary,
30    EofInPartHeaders,
31    EofInFile,
32    EofInPart,
33    HeaderMissing,
34    InvalidHeaderNameOrValue,
35    HeaderValueNotMime,
36    FilenameWithNonAsciiEncodingNotSupported,
37    ToStr(ToStrError),
38    /// An HTTP parsing error from a multipart section.
39    Httparse(httparse::Error),
40    /// An I/O error.
41    Io(io::Error),
42    /// An error was returned from Hyper.
43    Http(http::Error),
44    /// An error occurred during UTF-8 processing.
45    Utf8(FromUtf8Error),
46}
47
48impl From<io::Error> for Error {
49    fn from(err: io::Error) -> Error {
50        Error::Io(err)
51    }
52}
53
54impl From<httparse::Error> for Error {
55    fn from(err: httparse::Error) -> Error {
56        Error::Httparse(err)
57    }
58}
59
60impl From<http::Error> for Error {
61    fn from(err: http::Error) -> Error {
62        Error::Http(err)
63    }
64}
65
66impl From<FromUtf8Error> for Error {
67    fn from(err: FromUtf8Error) -> Error {
68        Error::Utf8(err)
69    }
70}
71
72impl Display for Error {
73    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74        match *self {
75            Error::Httparse(ref e) => format!("Httparse: {:?}", e).fmt(f),
76            Error::Io(ref e) => format!("Io: {}", e).fmt(f),
77            Error::Http(ref e) => format!("Http: {}", e).fmt(f),
78            Error::Utf8(ref e) => format!("Utf8: {}", e).fmt(f),
79            Error::ToStr(ref e) => format!("ToStr: {}", e).fmt(f),
80            Error::NoRequestContentType => "NoRequestContentType".to_string().fmt(f),
81            Error::NotMultipart => "NotMultipart".to_string().fmt(f),
82            Error::BoundaryNotSpecified => "BoundaryNotSpecified".to_string().fmt(f),
83            Error::PartialHeaders => "PartialHeaders".to_string().fmt(f),
84            Error::EofBeforeFirstBoundary => "EofBeforeFirstBoundary".to_string().fmt(f),
85            Error::NoCrLfAfterBoundary => "NoCrLfAfterBoundary".to_string().fmt(f),
86            Error::EofInPartHeaders => "EofInPartHeaders".to_string().fmt(f),
87            Error::EofInFile => "EofInFile".to_string().fmt(f),
88            Error::EofInPart => "EofInPart".to_string().fmt(f),
89            Error::EofInMainHeaders => "EofInMainHeaders".to_string().fmt(f),
90            Error::HeaderMissing => "HeaderMissing".to_string().fmt(f),
91            Error::InvalidHeaderNameOrValue => "InvalidHeaderNameOrValue".to_string().fmt(f),
92            Error::HeaderValueNotMime => "HeaderValueNotMime".to_string().fmt(f),
93            Error::FilenameWithNonAsciiEncodingNotSupported => {
94                "NonAsciiFilenameNotSupported".to_string().fmt(f)
95            }
96        }
97    }
98}
99
100impl fmt::Debug for Error {
101    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102        write!(f, "{}", self)?;
103        if self.source().is_some() {
104            write!(f, ": {:?}", self.source().unwrap())?; // recurse
105        }
106        Ok(())
107    }
108}
109
110impl StdError for Error {
111    fn description(&self) -> &str {
112        match *self {
113            Error::NoRequestContentType => "The Hyper request did not have a Content-Type header.",
114            Error::NotMultipart => {
115                "The Hyper request Content-Type top-level Mime was not multipart."
116            }
117            Error::BoundaryNotSpecified => {
118                "The Content-Type header failed to specify a boundary token."
119            }
120            Error::PartialHeaders => "A multipart section contained only partial headers.",
121            Error::EofInMainHeaders => "The request headers ended pre-maturely.",
122            Error::EofBeforeFirstBoundary => {
123                "The request body ended prior to reaching the expected starting boundary."
124            }
125            Error::NoCrLfAfterBoundary => "Missing CRLF after boundary.",
126            Error::EofInPartHeaders => {
127                "The request body ended prematurely while parsing headers of a multipart part."
128            }
129            Error::EofInFile => "The request body ended prematurely while streaming a file part.",
130            Error::EofInPart => {
131                "The request body ended prematurely while reading a multipart part."
132            }
133            Error::Httparse(_) => {
134                "A parse error occurred while parsing the headers of a multipart section."
135            }
136            Error::Io(_) => "An I/O error occurred.",
137            Error::Http(_) => "A Http error occurred.",
138            Error::Utf8(_) => "A UTF-8 error occurred.",
139            Error::HeaderMissing => "The requested header could not be found in the HeaderMap",
140            Error::InvalidHeaderNameOrValue => "Parsing to HeaderName or HeaderValue failed",
141            Error::HeaderValueNotMime => "HeaderValue could not be parsed to Mime",
142            Error::ToStr(_) => "A ToStr error occurred.",
143            Error::FilenameWithNonAsciiEncodingNotSupported => {
144                "Non-ASCII filename parsing not supported"
145            }
146        }
147    }
148}