1
+ use crate :: response:: Response ;
1
2
use std:: fmt;
2
3
use std:: io:: { self , ErrorKind } ;
3
4
4
- /// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic).
5
5
#[ derive( Debug ) ]
6
6
pub enum Error {
7
- /// The url could not be understood. Synthetic error `400`.
7
+ /// The url could not be understood.
8
8
BadUrl ( String ) ,
9
- /// The url scheme could not be understood. Synthetic error `400`.
9
+ /// The url scheme could not be understood.
10
10
UnknownScheme ( String ) ,
11
- /// DNS lookup failed. Synthetic error `400`.
11
+ /// DNS lookup failed.
12
12
DnsFailed ( String ) ,
13
- /// Connection to server failed. Synthetic error `500`.
13
+ /// Connection to server failed.
14
14
ConnectionFailed ( String ) ,
15
- /// Too many redirects. Synthetic error `500`.
15
+ /// Too many redirects.
16
16
TooManyRedirects ,
17
- /// A status line we don't understand `HTTP/1.1 200 OK`. Synthetic error `500`.
17
+ /// A status line we don't understand `HTTP/1.1 200 OK`.
18
18
BadStatus ,
19
- /// A header line that couldn't be parsed. Synthetic error `500`.
19
+ /// A header line that couldn't be parsed.
20
20
BadHeader ,
21
- /// Some unspecified `std::io::Error`. Synthetic error `500`.
21
+ /// Some unspecified `std::io::Error`.
22
22
Io ( io:: Error ) ,
23
23
/// Proxy information was not properly formatted
24
24
BadProxy ,
@@ -28,6 +28,10 @@ pub enum Error {
28
28
ProxyConnect ,
29
29
/// Incorrect credentials for proxy
30
30
InvalidProxyCreds ,
31
+ /// HTTP status code indicating an error (e.g. 4xx, 5xx)
32
+ /// Read the inner response body for details and to return
33
+ /// the connection to the pool.
34
+ HTTP ( Box < Response > ) ,
31
35
/// TLS Error
32
36
#[ cfg( feature = "native-tls" ) ]
33
37
TlsError ( native_tls:: Error ) ,
@@ -42,66 +46,6 @@ impl Error {
42
46
_ => false ,
43
47
}
44
48
}
45
-
46
- /// For synthetic responses, this is the error code.
47
- pub fn status ( & self ) -> u16 {
48
- match self {
49
- Error :: BadUrl ( _) => 400 ,
50
- Error :: UnknownScheme ( _) => 400 ,
51
- Error :: DnsFailed ( _) => 400 ,
52
- Error :: ConnectionFailed ( _) => 500 ,
53
- Error :: TooManyRedirects => 500 ,
54
- Error :: BadStatus => 500 ,
55
- Error :: BadHeader => 500 ,
56
- Error :: Io ( _) => 500 ,
57
- Error :: BadProxy => 500 ,
58
- Error :: BadProxyCreds => 500 ,
59
- Error :: ProxyConnect => 500 ,
60
- Error :: InvalidProxyCreds => 500 ,
61
- #[ cfg( feature = "native-tls" ) ]
62
- Error :: TlsError ( _) => 500 ,
63
- }
64
- }
65
-
66
- /// For synthetic responses, this is the status text.
67
- pub fn status_text ( & self ) -> & str {
68
- match self {
69
- Error :: BadUrl ( _) => "Bad URL" ,
70
- Error :: UnknownScheme ( _) => "Unknown Scheme" ,
71
- Error :: DnsFailed ( _) => "Dns Failed" ,
72
- Error :: ConnectionFailed ( _) => "Connection Failed" ,
73
- Error :: TooManyRedirects => "Too Many Redirects" ,
74
- Error :: BadStatus => "Bad Status" ,
75
- Error :: BadHeader => "Bad Header" ,
76
- Error :: Io ( _) => "Network Error" ,
77
- Error :: BadProxy => "Malformed proxy" ,
78
- Error :: BadProxyCreds => "Failed to parse proxy credentials" ,
79
- Error :: ProxyConnect => "Proxy failed to connect" ,
80
- Error :: InvalidProxyCreds => "Provided proxy credentials are incorrect" ,
81
- #[ cfg( feature = "native-tls" ) ]
82
- Error :: TlsError ( _) => "TLS Error" ,
83
- }
84
- }
85
-
86
- /// For synthetic responses, this is the body text.
87
- pub fn body_text ( & self ) -> String {
88
- match self {
89
- Error :: BadUrl ( url) => format ! ( "Bad URL: {}" , url) ,
90
- Error :: UnknownScheme ( scheme) => format ! ( "Unknown Scheme: {}" , scheme) ,
91
- Error :: DnsFailed ( err) => format ! ( "Dns Failed: {}" , err) ,
92
- Error :: ConnectionFailed ( err) => format ! ( "Connection Failed: {}" , err) ,
93
- Error :: TooManyRedirects => "Too Many Redirects" . to_string ( ) ,
94
- Error :: BadStatus => "Bad Status" . to_string ( ) ,
95
- Error :: BadHeader => "Bad Header" . to_string ( ) ,
96
- Error :: Io ( ioe) => format ! ( "Network Error: {}" , ioe) ,
97
- Error :: BadProxy => "Malformed proxy" . to_string ( ) ,
98
- Error :: BadProxyCreds => "Failed to parse proxy credentials" . to_string ( ) ,
99
- Error :: ProxyConnect => "Proxy failed to connect" . to_string ( ) ,
100
- Error :: InvalidProxyCreds => "Provided proxy credentials are incorrect" . to_string ( ) ,
101
- #[ cfg( feature = "native-tls" ) ]
102
- Error :: TlsError ( err) => format ! ( "TLS Error: {}" , err) ,
103
- }
104
- }
105
49
}
106
50
107
51
impl From < io:: Error > for Error {
@@ -112,7 +56,23 @@ impl From<io::Error> for Error {
112
56
113
57
impl fmt:: Display for Error {
114
58
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
115
- write ! ( f, "{}" , self . body_text( ) )
59
+ match self {
60
+ Error :: BadUrl ( url) => write ! ( f, "Bad URL: {}" , url) ,
61
+ Error :: UnknownScheme ( scheme) => write ! ( f, "Unknown Scheme: {}" , scheme) ,
62
+ Error :: DnsFailed ( err) => write ! ( f, "Dns Failed: {}" , err) ,
63
+ Error :: ConnectionFailed ( err) => write ! ( f, "Connection Failed: {}" , err) ,
64
+ Error :: TooManyRedirects => write ! ( f, "Too Many Redirects" ) ,
65
+ Error :: BadStatus => write ! ( f, "Bad Status" ) ,
66
+ Error :: BadHeader => write ! ( f, "Bad Header" ) ,
67
+ Error :: Io ( ioe) => write ! ( f, "Network Error: {}" , ioe) ,
68
+ Error :: BadProxy => write ! ( f, "Malformed proxy" ) ,
69
+ Error :: BadProxyCreds => write ! ( f, "Failed to parse proxy credentials" ) ,
70
+ Error :: ProxyConnect => write ! ( f, "Proxy failed to connect" ) ,
71
+ Error :: InvalidProxyCreds => write ! ( f, "Provided proxy credentials are incorrect" ) ,
72
+ Error :: HTTP ( response) => write ! ( f, "HTTP status {}" , response. status( ) ) ,
73
+ #[ cfg( feature = "native-tls" ) ]
74
+ Error :: TlsError ( err) => write ! ( f, "TLS Error: {}" , err) ,
75
+ }
116
76
}
117
77
}
118
78
0 commit comments