mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-22 20:52:27 +00:00
improve the From impl for AuthErrorOrOther #20
This commit is contained in:
parent
0b06528742
commit
912f030bfd
3 changed files with 26 additions and 9 deletions
|
@ -41,6 +41,7 @@ uuid = { version = "0.8.1", optional = true }
|
|||
diesel = { version = "1.4.4", features = ["postgres"] }
|
||||
futures-executor = "0.3.4"
|
||||
paste = "0.1.12"
|
||||
thiserror = "1.0.18"
|
||||
trybuild = "1.0.26"
|
||||
|
||||
[features]
|
||||
|
|
|
@ -167,7 +167,7 @@ impl ErrorVariant
|
|||
})
|
||||
}
|
||||
|
||||
fn into_match_arm(self, krate : &TokenStream, enum_ident : &Ident) -> TokenStream
|
||||
fn into_match_arm(self, krate : &TokenStream, enum_ident : &Ident) -> Result<TokenStream>
|
||||
{
|
||||
let ident = &self.ident;
|
||||
let fields_pat = self.fields_pat();
|
||||
|
@ -185,21 +185,23 @@ impl ErrorVariant
|
|||
});
|
||||
|
||||
// the response will come directly from the from_ty if present
|
||||
let res = match self.from_ty {
|
||||
Some((from_index, _)) => {
|
||||
let res = match (self.from_ty, status) {
|
||||
(Some((from_index, _)), None) => {
|
||||
let from_field = &self.fields[from_index].ident;
|
||||
quote!(#from_field.into_response_error())
|
||||
},
|
||||
None => quote!(Ok(#krate::Response {
|
||||
(Some(_), Some(_)) => return Err(Error::new(ident.span(), "When #[from] is used, #[status] must not be used!")),
|
||||
(None, Some(status)) => quote!(Ok(#krate::Response {
|
||||
status: { #status }.into(),
|
||||
body: #krate::gotham::hyper::Body::empty(),
|
||||
mime: None
|
||||
}))
|
||||
})),
|
||||
(None, None) => return Err(Error::new(ident.span(), "Missing #[status(code)] for this variant"))
|
||||
};
|
||||
|
||||
quote! {
|
||||
Ok(quote! {
|
||||
#enum_ident::#ident #fields_pat => #res
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn were(&self) -> Option<TokenStream>
|
||||
|
@ -293,7 +295,9 @@ pub fn expand_resource_error(input : DeriveInput) -> Result<TokenStream>
|
|||
}
|
||||
|
||||
let were = variants.iter().filter_map(|variant| variant.were()).collect::<Vec<_>>();
|
||||
let variants = variants.into_iter().map(|variant| variant.into_match_arm(&krate, &ident));
|
||||
let variants = variants.into_iter()
|
||||
.map(|variant| variant.into_match_arm(&krate, &ident))
|
||||
.collect_to_result()?;
|
||||
|
||||
Ok(quote! {
|
||||
#display_impl
|
||||
|
|
|
@ -62,8 +62,9 @@ pub enum AuthErrorOrOther<E>
|
|||
#[status(FORBIDDEN)]
|
||||
#[display("Forbidden")]
|
||||
Forbidden,
|
||||
#[status(INTERNAL_SERVER_ERROR)]
|
||||
#[display("{0}")]
|
||||
Other(#[from] E)
|
||||
Other(E)
|
||||
}
|
||||
|
||||
impl<E> From<AuthError> for AuthErrorOrOther<E>
|
||||
|
@ -76,6 +77,17 @@ impl<E> From<AuthError> for AuthErrorOrOther<E>
|
|||
}
|
||||
}
|
||||
|
||||
impl<E, F> From<F> for AuthErrorOrOther<E>
|
||||
where
|
||||
// TODO https://gitlab.com/msrd0/gotham-restful/-/issues/20
|
||||
F : std::error::Error + Into<E>
|
||||
{
|
||||
fn from(err : F) -> Self
|
||||
{
|
||||
Self::Other(err.into())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This return type can be used to map another `ResourceResult` that can only be returned if the
|
||||
client is authenticated. Otherwise, an empty _403 Forbidden_ response will be issued. Use can
|
||||
|
|
Loading…
Add table
Reference in a new issue