File tree Expand file tree Collapse file tree 2 files changed +30
-13
lines changed Expand file tree Collapse file tree 2 files changed +30
-13
lines changed Original file line number Diff line number Diff line change @@ -23,13 +23,10 @@ impl Error {
23
23
/// The error type must be threadsafe and 'static, so that the Error will be
24
24
/// as well. If the error type does not provide a backtrace, a backtrace will
25
25
/// be created here to ensure that a backtrace exists.
26
- pub fn new < E > ( status : StatusCode , error : E ) -> Self
27
- where
28
- E : StdError + Send + Sync + ' static ,
29
- {
26
+ pub fn new ( status : StatusCode , error : impl Into < anyhow:: Error > ) -> Self {
30
27
Self {
31
28
status,
32
- error : anyhow :: Error :: new ( error) ,
29
+ error : error. into ( ) ,
33
30
}
34
31
}
35
32
@@ -119,15 +116,9 @@ impl Debug for Error {
119
116
}
120
117
}
121
118
122
- impl < E > From < E > for Error
123
- where
124
- E : StdError + Send + Sync + ' static ,
125
- {
119
+ impl < E : Into < anyhow:: Error > > From < E > for Error {
126
120
fn from ( error : E ) -> Self {
127
- Self {
128
- error : anyhow:: Error :: new ( error) ,
129
- status : StatusCode :: InternalServerError ,
130
- }
121
+ Self :: new ( StatusCode :: InternalServerError , error)
131
122
}
132
123
}
133
124
Original file line number Diff line number Diff line change @@ -70,3 +70,29 @@ fn option_ext() {
70
70
let err = res. unwrap_err ( ) ;
71
71
assert_eq ! ( err. status( ) , StatusCode :: NotFound ) ;
72
72
}
73
+
74
+ #[ test]
75
+ fn anyhow_error_into_http_types_error ( ) {
76
+ let anyhow_error =
77
+ anyhow:: Error :: new ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "irrelevant" ) ) ;
78
+ let http_types_error: Error = anyhow_error. into ( ) ;
79
+ assert_eq ! ( http_types_error. status( ) , StatusCode :: InternalServerError ) ;
80
+
81
+ let anyhow_error =
82
+ anyhow:: Error :: new ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "irrelevant" ) ) ;
83
+ let http_types_error: Error = Error :: new ( StatusCode :: ImATeapot , anyhow_error) ;
84
+ assert_eq ! ( http_types_error. status( ) , StatusCode :: ImATeapot ) ;
85
+ }
86
+
87
+ #[ test]
88
+ fn normal_error_into_http_types_error ( ) {
89
+ let http_types_error: Error =
90
+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "irrelevant" ) . into ( ) ;
91
+ assert_eq ! ( http_types_error. status( ) , StatusCode :: InternalServerError ) ;
92
+
93
+ let http_types_error = Error :: new (
94
+ StatusCode :: ImATeapot ,
95
+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "irrelevant" ) ,
96
+ ) ;
97
+ assert_eq ! ( http_types_error. status( ) , StatusCode :: ImATeapot ) ;
98
+ }
You can’t perform that action at this time.
0 commit comments