xref: /aosp_15_r20/tools/netsim/rust/http-proxy/src/error.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
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     IoError(io::Error),
25     ConnectionError(SocketAddr, String),
26     MalformedConfigString,
27     InvalidPortNumber,
28     InvalidHost,
29 }
30 
31 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result32     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33         write!(f, "ProxyError: {self:?}")
34     }
35 }
36 
37 impl std::error::Error for Error {}
38 
39 impl From<io::Error> for Error {
from(err: io::Error) -> Self40     fn from(err: io::Error) -> Self {
41         Error::IoError(err)
42     }
43 }
44 
45 #[cfg(test)]
46 mod tests {
47     use super::*;
48 
49     #[test]
test_io_error_chaining()50     fn test_io_error_chaining() {
51         let inner_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
52         let outer_error = Error::IoError(inner_error);
53 
54         assert!(outer_error.to_string().contains("file not found"));
55     }
56 }
57