better error handling

main
Hannaeko 2021-04-02 17:35:25 -04:00
parent 7df4792ec5
commit 67e12239b3
2 changed files with 14 additions and 6 deletions

View File

@ -71,12 +71,21 @@ impl From<UserError> for ErrorResponse {
impl<S> Into<Outcome<S, ErrorResponse>> for ErrorResponse {
fn into(self) -> Outcome<S, ErrorResponse> {
Outcome::Failure((self.status.clone(), self))
Outcome::Failure(self.into())
}
}
impl Into<(Status, ErrorResponse)> for ErrorResponse {
fn into(self) -> (Status, ErrorResponse) {
(self.status.clone(), self)
}
}
pub fn make_500<E: std::fmt::Debug>(e: E) -> ErrorResponse {
println!("{:?}", e);
println!("Making 500 for Error: {:?}", e);
ErrorResponse::new(Status::InternalServerError, "An unexpected error occured.".into())
}
pub fn make_500_tuple<E: std::fmt::Debug>(e: E) -> (Status, ErrorResponse) {
make_500(e).into()
}

View File

@ -20,7 +20,7 @@ use jsonwebtoken::{
use crate::schema::*;
use crate::DbConn;
use crate::config::Config;
use crate::models::errors::ErrorResponse;
use crate::models::errors::{ErrorResponse, make_500_tuple};
const BEARER: &'static str = "Bearer ";
@ -109,9 +109,8 @@ impl<'r> FromRequest<'r> for UserInfo {
return ErrorResponse::from(UserError::MalformedHeader).into()
};
// TODO: Better error handling
let config = request.guard::<State<Config>>().await.unwrap();
let conn = request.guard::<DbConn>().await.unwrap();
let config = try_outcome!(request.guard::<State<Config>>().await.map_failure(make_500_tuple));
let conn = try_outcome!(request.guard::<DbConn>().await.map_failure(make_500_tuple));
let token_data = AuthClaims::decode(
token, &config.web_app.secret