trillium_http/error.rs
1use crate::{HeaderName, Version};
2use std::{num::TryFromIntError, str::Utf8Error, time::Duration};
3use thiserror::Error;
4
5/// Concrete errors that occur within trillium's HTTP implementation
6#[derive(Error, Debug)]
7#[non_exhaustive]
8pub enum Error {
9 /// [`std::io::Error`]
10 #[error(transparent)]
11 Io(#[from] std::io::Error),
12
13 /// this error describes a malformed request with a path that does
14 /// not start with / or http:// or https://
15 #[error("Unexpected uri format")]
16 UnexpectedUriFormat,
17
18 /// the relevant HTTP protocol expected this header, but it was
19 /// not provided
20 #[error("Mandatory {0} header missing")]
21 HeaderMissing(HeaderName<'static>),
22
23 /// this error describes a request that does not specify a path
24 #[error("Request path missing")]
25 RequestPathMissing,
26
27 /// connection was closed
28 #[error("Connection closed by client")]
29 Closed,
30
31 /// [`TryFromIntError`]
32 #[error(transparent)]
33 TryFromIntError(#[from] TryFromIntError),
34
35 /// An incomplete or invalid HTTP head
36 #[error("Partial or invalid HTTP head")]
37 InvalidHead,
38
39 /// We were unable to parse a [`HeaderName`][crate::HeaderName]
40 #[error("Invalid or unparseable header name")]
41 InvalidHeaderName,
42
43 /// We were unable to parse a [`HeaderValue`][crate::HeaderValue]
44 #[error("Invalid or unparseable header value, header name: {0}")]
45 InvalidHeaderValue(HeaderName<'static>),
46
47 /// we were able to parse this [`Version`], but we do not support it
48 #[error("Unsupported version {0}")]
49 UnsupportedVersion(Version),
50
51 /// We were unable to parse a [`Version`]
52 #[error("Invalid or missing version")]
53 InvalidVersion,
54
55 /// we were unable to parse this method
56 #[error("Unsupported method {0}")]
57 UnrecognizedMethod(String),
58
59 /// this request did not have a method
60 #[error("Missing method")]
61 MissingMethod,
62
63 /// this request did not have a status code
64 #[error("Missing status code")]
65 MissingStatus,
66
67 /// we were unable to parse a [`Status`](crate::Status)
68 #[error("Invalid status code")]
69 InvalidStatus,
70
71 /// we expected utf8, but there was an encoding error
72 #[error(transparent)]
73 EncodingError(#[from] Utf8Error),
74
75 /// we either received a header that does not make sense in context
76 #[error("Unexpected header: {0}")]
77 UnexpectedHeader(HeaderName<'static>),
78
79 /// to mitigate against malicious HTTP clients, we do not allow request headers beyond this
80 /// length.
81 #[error("Headers were malformed or longer than allowed")]
82 HeadersTooLong,
83
84 /// to mitigate against malicious HTTP clients, we do not read received bodies beyond this
85 /// length to memory. If you need to receive longer bodies, use the Stream or `AsyncRead`
86 /// implementation on `ReceivedBody`
87 #[error("Received body too long. Maximum {0} bytes")]
88 ReceivedBodyTooLong(u64),
89
90 /// something took longer than was allowed
91 #[error("{0} took longer than {1:?}")]
92 TimedOut(&'static str, Duration),
93}
94
95/// this crate's result type
96pub type Result<T> = std::result::Result<T, Error>;