• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! This module defines the Proxy error types.
16 
17 use std::fmt;
18 use std::io;
19 use std::net::SocketAddr;
20 
21 /// An enumeration of possible errors.
22 #[derive(Debug)]
23 pub enum Error {
24     /// An I/O error occurred.
25     IoError(io::Error),
26     /// An error occurred during connection establishment.
27     ConnectionError(SocketAddr, String),
28     /// The configuration string was malformed.
29     MalformedConfigString,
30     /// The provided port number was invalid.
31     InvalidPortNumber,
32     /// The provided host was invalid.
33     InvalidHost,
34 }
35 
36 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result37     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38         write!(f, "ProxyError: {self:?}")
39     }
40 }
41 
42 impl std::error::Error for Error {}
43 
44 impl From<io::Error> for Error {
from(err: io::Error) -> Self45     fn from(err: io::Error) -> Self {
46         Error::IoError(err)
47     }
48 }
49 
50 #[cfg(test)]
51 mod tests {
52     use super::*;
53 
54     #[test]
test_io_error_chaining()55     fn test_io_error_chaining() {
56         let inner_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
57         let outer_error = Error::IoError(inner_error);
58 
59         assert!(outer_error.to_string().contains("file not found"));
60     }
61 }
62