Commit 67e12239 authored by Gaël Berthaud-Müller's avatar Gaël Berthaud-Müller
Browse files

better error handling

parent 7df4792e
......@@ -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()
}
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment