@@ -4,6 +4,7 @@ use std::error::Error as StdError;
4
4
use std:: fmt:: { self , Debug , Display } ;
5
5
6
6
use crate :: StatusCode ;
7
+ use std:: convert:: TryInto ;
7
8
8
9
/// A specialized `Result` type for HTTP operations.
9
10
///
@@ -23,24 +24,33 @@ impl Error {
23
24
/// The error type must be threadsafe and 'static, so that the Error will be
24
25
/// as well. If the error type does not provide a backtrace, a backtrace will
25
26
/// be created here to ensure that a backtrace exists.
26
- pub fn new ( status : StatusCode , error : impl Into < anyhow:: Error > ) -> Self {
27
+ pub fn new < S > ( status : S , error : impl Into < anyhow:: Error > ) -> Self
28
+ where
29
+ S : TryInto < StatusCode > ,
30
+ S :: Error : Debug ,
31
+ {
27
32
Self {
28
- status,
33
+ status : status
34
+ . try_into ( )
35
+ . expect ( "Could not convert into a valid `StatusCode`" ) ,
29
36
error : error. into ( ) ,
30
37
}
31
38
}
32
39
33
40
/// Create a new error object from static string.
34
- pub fn from_str < M > ( status : StatusCode , msg : M ) -> Self
41
+ pub fn from_str < S , M > ( status : S , msg : M ) -> Self
35
42
where
43
+ S : TryInto < StatusCode > ,
44
+ S :: Error : Debug ,
36
45
M : Display + Debug + Send + Sync + ' static ,
37
46
{
38
47
Self {
39
- status,
48
+ status : status
49
+ . try_into ( )
50
+ . expect ( "Could not convert into a valid `StatusCode`" ) ,
40
51
error : anyhow:: Error :: msg ( msg) ,
41
52
}
42
53
}
43
-
44
54
/// Create a new error from a message.
45
55
pub ( crate ) fn new_adhoc < M > ( message : M ) -> Error
46
56
where
0 commit comments