Skip to content

Commit

Permalink
Fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
the10thWiz committed Nov 22, 2024
1 parent 0b72867 commit 1a64607
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
3 changes: 2 additions & 1 deletion core/codegen/src/attribute/catch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub fn _catch(
::rocket::trace::error!(
downcast_to = stringify!(#ty),
error_name = #__error.name(),
"Failed to downcast error. This should never happen, please open an issue with details."
"Failed to downcast error. This should never happen, please \
open an issue with details."
);
return #_Err(#Status::InternalServerError);
},
Expand Down
15 changes: 11 additions & 4 deletions core/codegen/src/attribute/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ fn query_decls(route: &Route) -> Option<TokenStream> {
"{_err}"
); } }
);
::rocket::trace::info!(
name: "forward",
target: concat!("rocket::codegen::route::", module_path!()),
error_name = #TypedError::name(&__e),
"parameter guard forwarding"
);

return #Outcome::Forward((
#__data,
Expand Down Expand Up @@ -141,6 +147,7 @@ fn request_guard_decl(guard: &Guard) -> TokenStream {
parameter = stringify!(#ident),
type_name = stringify!(#ty),
status = #TypedError::status(&__e).code,
error_name = #TypedError::name(&__e),
"request guard forwarding"
);

Expand All @@ -156,8 +163,8 @@ fn request_guard_decl(guard: &Guard) -> TokenStream {
target: concat!("rocket::codegen::route::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
status = #TypedError::status(&__c).code,
error_name = #TypedError::name(&__c),
// reason = %#display_hack!(__e),
"request guard failed"
);

Expand All @@ -181,8 +188,7 @@ fn param_guard_decl(guard: &Guard) -> TokenStream {
target: concat!("rocket::codegen::route::", module_path!()),
parameter = #name,
type_name = stringify!(#ty),
name = #TypedError::name(&__error),
// reason = %#display_hack!(__error),
error_name = #TypedError::name(&__error),
"path guard forwarding"
);

Expand Down Expand Up @@ -248,6 +254,7 @@ fn data_guard_decl(guard: &Guard) -> TokenStream {
parameter = stringify!(#ident),
type_name = stringify!(#ty),
status = #TypedError::status(&__e).code,
error_name = #TypedError::name(&__e),
"data guard forwarding"
);

Expand All @@ -263,7 +270,7 @@ fn data_guard_decl(guard: &Guard) -> TokenStream {
target: concat!("rocket::codegen::route::", module_path!()),
parameter = stringify!(#ident),
type_name = stringify!(#ty),
// reason = %#display_hack!(__e),
error_name = #TypedError::name(&__e),
"data guard failed"
);

Expand Down
13 changes: 8 additions & 5 deletions core/codegen/src/derive/typed_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ pub fn derive_typed_error(input: proc_macro::TokenStream) -> TokenStream {
})
)
.validator(ValidatorBuild::new()
.input_validate(|_, i| match i.generics().lifetimes().count() > 1 {
true => Err(i.generics().span().error("only one lifetime is supported")),
false => Ok(())
.input_validate(|_, i| if i.generics().lifetimes().count() > 1 {
Err(i.generics().span().error("only one lifetime is supported"))
} else if i.generics().const_params().count() > 0 {
Err(i.generics().span().error("const params are not supported"))
} else {
Ok(())
})
)
.inner_mapper(MapperBuild::new()
Expand All @@ -146,14 +149,14 @@ pub fn derive_typed_error(input: proc_macro::TokenStream) -> TokenStream {
match g {
syn::GenericParam::Lifetime(_) => quote!{ 'static },
syn::GenericParam::Type(TypeParam { ident, .. }) => quote! { #ident },
syn::GenericParam::Const(ConstParam { .. }) => todo!(),
syn::GenericParam::Const(ConstParam { .. }) => unreachable!(),
}
});
let trans = input.generics()
.lifetimes()
.map(|LifetimeParam { lifetime, .. }| quote!{#_catcher::Inv<#lifetime>});
quote!{
type Static = #name <#(#args)*>;
type Static = #name <#(#args,)*>;
type Transience = (#(#trans,)*);
}
})
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ pub fn derive_responder(input: TokenStream) -> TokenStream {
/// name: &'r str,
/// value: &'r str,
/// }
///
///
/// #[derive(TypedError)]
/// enum HeaderError {
/// InvalidValue,
Expand Down
6 changes: 4 additions & 2 deletions core/codegen/tests/catcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ fn test_basic_params() {
#[catch(default, error = "<e>")]
fn test_io_error(e: &io::Error) -> String { format!("{e:?}") }
#[catch(default, error = "<_e>")]
fn test_parse_int_error(_e: &ParseIntError) -> String { println!("ParseIntError"); format!("ParseIntError") }
fn test_parse_int_error(_e: &ParseIntError) -> String { format!("ParseIntError") }
#[catch(default, error = "<_e>")]
fn test_parse_bool_error(_e: &ParseBoolError) -> String { format!("ParseBoolError") }
#[catch(default, error = "<e>")]
fn test_param_parse_bool_error(e: &FromParamError<'_, ParseBoolError>) -> String { format!("ParseBoolError: {}", e.raw) }
fn test_param_parse_bool_error(e: &FromParamError<'_, ParseBoolError>) -> String {
format!("ParseBoolError: {}", e.raw)
}


#[test]
Expand Down
11 changes: 11 additions & 0 deletions core/codegen/tests/typed_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ fn validate_static() {
));
boxed_error(Box::new(val));
}

#[derive(TypedError)]
pub enum Generic<E> {
First(E),
}

#[derive(TypedError)]
pub struct GenericWithLifetime<'r, E> {
s: &'r str,
inner: E,
}
4 changes: 3 additions & 1 deletion core/lib/src/router/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ impl Catcher {
/// assert!(!a.collides_with(&b));
/// ```
pub fn collides_with(&self, other: &Self) -> bool {
self.code == other.code && self.base().segments().eq(other.base().segments()) && self.type_id == other.type_id
self.code == other.code
&& self.base().segments().eq(other.base().segments())
&& self.type_id == other.type_id
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/lib/src/router/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ mod test {
{
let mut router = Router::new();
for (status, base, ty) in catchers {
let mut catcher = Catcher::new(status.map(|s| s.code), catcher::dummy_handler).rebase(Origin::parse(base).unwrap());
let mut catcher = Catcher::new(status.map(|s| s.code), catcher::dummy_handler)
.rebase(Origin::parse(base).unwrap());
catcher.type_id = ty;
router.catchers.push(catcher);
}
Expand Down Expand Up @@ -646,7 +647,7 @@ mod test {
let request = client.req(Method::Get, Origin::parse(uri).unwrap());
router.catch_any(status, ty(&()), &request)
}

#[test]
fn test_catch_vs_catch_any() {
let router = make_router_catches([(None, "/", None)]).unwrap();
Expand Down

0 comments on commit 1a64607

Please sign in to comment.