trillium_http/
error.rs

1use std::borrow::Cow;
2use std::num::TryFromIntError;
3use std::str::Utf8Error;
4
5use thiserror::Error;
6
7/// Concrete errors that occur within trillium's http implementation
8#[derive(Error, Debug)]
9#[non_exhaustive]
10pub enum Error {
11    /// [`std::io::Error`]
12    #[error(transparent)]
13    Io(#[from] std::io::Error),
14
15    /// this error describes a malformed request with a path that does
16    /// not start with / or http:// or https://
17    #[error("unexpected uri format")]
18    UnexpectedUriFormat,
19
20    /// the relevant http protocol expected this header, but it was
21    /// not provided
22    #[error("mandatory {0} header missing")]
23    HeaderMissing(&'static str),
24
25    /// this error describes a request that does not specify a path
26    #[error("request path missing")]
27    RequestPathMissing,
28
29    /// connection was closed
30    #[error("connection closed by client")]
31    Closed,
32
33    /// [`httparse::Error`]
34    #[error(transparent)]
35    Httparse(#[from] httparse::Error),
36
37    /// [`TryFromIntError`]
38    #[error(transparent)]
39    TryFromIntError(#[from] TryFromIntError),
40
41    /// an incomplete http head
42    #[error("partial http head")]
43    PartialHead,
44
45    /// we were unable to parse a header
46    #[error("malformed http header {0}")]
47    MalformedHeader(Cow<'static, str>),
48
49    /// async-h1 doesn't speak this http version
50    /// this error is deprecated
51    #[error("unsupported http version 1.{0}")]
52    UnsupportedVersion(u8),
53
54    /// we were unable to parse this http method
55    #[error("unsupported http method {0}")]
56    UnrecognizedMethod(String),
57
58    /// this request did not have a method
59    #[error("missing method")]
60    MissingMethod,
61
62    /// this request did not have a status code
63    #[error("missing status code")]
64    MissingStatusCode,
65
66    /// we were unable to parse this http method
67    #[error("unrecognized http status code {0}")]
68    UnrecognizedStatusCode(u16),
69
70    /// this request did not have a version, but we expect one
71    /// this error is deprecated
72    #[error("missing version")]
73    MissingVersion,
74
75    /// we expected utf8, but there was an encoding error
76    #[error(transparent)]
77    EncodingError(#[from] Utf8Error),
78
79    /// we received a header that does not make sense in context
80    #[error("unexpected header: {0}")]
81    UnexpectedHeader(&'static str),
82
83    /// to mitigate against malicious http clients, we do not allow request headers beyond this
84    /// length.
85    #[error("Headers were malformed or longer than allowed")]
86    HeadersTooLong,
87
88    /// to mitigate against malicious http clients, we do not read received bodies beyond this
89    /// length to memory. If you need to receive longer bodies, use the Stream or `AsyncRead`
90    /// implementation on `ReceivedBody`
91    #[error("Received body too long. Maximum {0} bytes")]
92    ReceivedBodyTooLong(u64),
93}
94
95/// this crate's result type
96pub type Result<T> = std::result::Result<T, Error>;