Skip to content

Commit d4f85be

Browse files
authored
Merge pull request #216 from yusuke-ota/make_statuscode_from_u16_in_error
Make statuscode from u16 in error
2 parents 6274ecd + f26568c commit d4f85be

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/error.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::error::Error as StdError;
44
use std::fmt::{self, Debug, Display};
55

66
use crate::StatusCode;
7+
use std::convert::TryInto;
78

89
/// A specialized `Result` type for HTTP operations.
910
///
@@ -23,24 +24,33 @@ impl Error {
2324
/// The error type must be threadsafe and 'static, so that the Error will be
2425
/// as well. If the error type does not provide a backtrace, a backtrace will
2526
/// 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+
{
2732
Self {
28-
status,
33+
status: status
34+
.try_into()
35+
.expect("Could not convert into a valid `StatusCode`"),
2936
error: error.into(),
3037
}
3138
}
3239

3340
/// 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
3542
where
43+
S: TryInto<StatusCode>,
44+
S::Error: Debug,
3645
M: Display + Debug + Send + Sync + 'static,
3746
{
3847
Self {
39-
status,
48+
status: status
49+
.try_into()
50+
.expect("Could not convert into a valid `StatusCode`"),
4051
error: anyhow::Error::msg(msg),
4152
}
4253
}
43-
4454
/// Create a new error from a message.
4555
pub(crate) fn new_adhoc<M>(message: M) -> Error
4656
where

tests/error.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,31 @@ fn normal_error_into_http_types_error() {
9696
);
9797
assert_eq!(http_types_error.status(), StatusCode::ImATeapot);
9898
}
99+
100+
#[test]
101+
fn u16_into_status_code_in_http_types_error() {
102+
let http_types_error = Error::new(404, io::Error::new(io::ErrorKind::Other, "Not Found"));
103+
let http_types_error2 = Error::new(
104+
StatusCode::NotFound,
105+
io::Error::new(io::ErrorKind::Other, "Not Found"),
106+
);
107+
assert_eq!(http_types_error.status(), http_types_error2.status());
108+
109+
let http_types_error = Error::from_str(404, "Not Found");
110+
assert_eq!(http_types_error.status(), StatusCode::NotFound);
111+
}
112+
113+
#[test]
114+
#[should_panic]
115+
fn fail_test_u16_into_status_code_in_http_types_error_new() {
116+
let _http_types_error = Error::new(
117+
1000,
118+
io::Error::new(io::ErrorKind::Other, "Incorrect status code"),
119+
);
120+
}
121+
122+
#[test]
123+
#[should_panic]
124+
fn fail_test_u16_into_status_code_in_http_types_error_from_str() {
125+
let _http_types_error = Error::from_str(1000, "Incorrect status code");
126+
}

0 commit comments

Comments
 (0)