lightws/error/
handshake.rs1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum HandshakeError {
5 HttpVersion,
7
8 HttpMethod,
9
10 HttpSatusCode,
11
12 HttpHost,
13
14 Upgrade,
16
17 Connection,
18
19 SecWebSocketKey,
20
21 SecWebSocketAccept,
22
23 SecWebSocketVersion,
24
25 NotEnoughData,
29
30 NotEnoughCapacity,
32
33 Httparse(httparse::Error),
34
35 Manual(&'static str),
36}
37
38impl Display for HandshakeError {
39 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40 use HandshakeError::*;
41 match self {
42 HttpVersion => write!(f, "Illegal http version"),
44
45 HttpMethod => write!(f, "Illegal http method"),
46
47 HttpSatusCode => write!(f, "Illegal http status code"),
48
49 HttpHost => write!(f, "Missing http host header"),
50
51 Upgrade => write!(f, "Missing or illegal upgrade header"),
53
54 Connection => write!(f, "Missing or illegal connection header"),
55
56 SecWebSocketKey => {
57 write!(f, "Missing sec-websocket-key header")
58 }
59
60 SecWebSocketAccept => {
61 write!(f, "Missing or illegal sec-websocket-accept header")
62 }
63
64 SecWebSocketVersion => {
65 write!(f, "Missing or illegal sec-websocket-version")
66 }
67
68 NotEnoughData => write!(f, "Not enough data to parse"),
70
71 NotEnoughCapacity => write!(f, "Not enough space to write to"),
72
73 Httparse(e) => write!(f, "Http parse error: {}", e),
74
75 Manual(s) => write!(f, "Manual error: {}", s),
76 }
77 }
78}
79
80impl From<httparse::Error> for HandshakeError {
81 fn from(e: httparse::Error) -> Self { HandshakeError::Httparse(e) }
82}
83
84impl std::error::Error for HandshakeError {
85 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
86 if let HandshakeError::Httparse(e) = self {
87 Some(e)
88 } else {
89 None
90 }
91 }
92}