-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fbf1b4
commit c263a6c
Showing
12 changed files
with
149 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#[macro_use] extern crate rocket; | ||
use rocket::catcher::TypedError; | ||
use rocket::http::Status; | ||
|
||
fn boxed_error<'r>(_val: Box<dyn TypedError<'r> + 'r>) {} | ||
|
||
#[derive(TypedError)] | ||
pub enum Foo<'r> { | ||
First(String), | ||
Second(Vec<u8>), | ||
Third { | ||
#[error(source)] | ||
responder: std::io::Error, | ||
}, | ||
#[error(status = 400)] | ||
Fourth { | ||
string: &'r str, | ||
}, | ||
} | ||
|
||
#[test] | ||
fn validate_foo() { | ||
let first = Foo::First("".into()); | ||
assert_eq!(first.status(), Status::InternalServerError); | ||
assert!(first.source().is_none()); | ||
boxed_error(Box::new(first)); | ||
let second = Foo::Second(vec![]); | ||
assert_eq!(second.status(), Status::InternalServerError); | ||
assert!(second.source().is_none()); | ||
boxed_error(Box::new(second)); | ||
let third = Foo::Third { | ||
responder: std::io::Error::new(std::io::ErrorKind::NotFound, ""), | ||
}; | ||
assert_eq!(third.status(), Status::InternalServerError); | ||
assert!(std::ptr::eq( | ||
third.source().unwrap(), | ||
if let Foo::Third { responder } = &third { responder } else { panic!() } | ||
)); | ||
boxed_error(Box::new(third)); | ||
let fourth = Foo::Fourth { string: "" }; | ||
assert_eq!(fourth.status(), Status::BadRequest); | ||
assert!(fourth.source().is_none()); | ||
boxed_error(Box::new(fourth)); | ||
} | ||
|
||
#[derive(TypedError)] | ||
pub struct InfallibleError { | ||
#[error(source)] | ||
_inner: std::convert::Infallible, | ||
} | ||
|
||
#[derive(TypedError)] | ||
pub struct StaticError { | ||
#[error(source)] | ||
inner: std::string::FromUtf8Error, | ||
} | ||
|
||
#[test] | ||
fn validate_static() { | ||
let val = StaticError { | ||
inner: String::from_utf8(vec![0xFF]).unwrap_err(), | ||
}; | ||
assert_eq!(val.status(), Status::InternalServerError); | ||
assert!(std::ptr::eq( | ||
val.source().unwrap(), | ||
&val.inner, | ||
)); | ||
boxed_error(Box::new(val)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[macro_use] extern crate rocket; | ||
|
||
#[derive(TypedError)] | ||
struct InnerError; | ||
struct InnerNonError; | ||
|
||
#[derive(TypedError)] | ||
struct Thing1<'a, 'b> { | ||
a: &'a str, | ||
b: &'b str, | ||
} | ||
|
||
#[derive(TypedError)] | ||
struct Thing2 { | ||
#[error(source)] | ||
inner: InnerNonError, | ||
} | ||
|
||
#[derive(TypedError)] | ||
enum Thing3<'a, 'b> { | ||
A(&'a str), | ||
B(&'b str), | ||
} | ||
|
||
#[derive(TypedError)] | ||
enum Thing4 { | ||
A(#[error(source)] InnerNonError), | ||
B(#[error(source)] InnerError), | ||
} | ||
|
||
#[derive(TypedError)] | ||
enum EmptyEnum { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters