1
0
Fork 0

Allow changing error codes and create an empty error.

Return 404 instead of 400 when no accounts breached.
Dieser Commit ist enthalten in:
Daniel García 2019-03-14 00:17:36 +01:00
Ursprung a25bfdd16d
Commit 61515160a7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: FC8A7D14C3CD543A
2 geänderte Dateien mit 30 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -33,10 +33,10 @@ use rocket::Route;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use serde_json::Value; use serde_json::Value;
use crate::db::DbConn;
use crate::api::{EmptyResult, JsonResult, JsonUpcase}; use crate::api::{EmptyResult, JsonResult, JsonUpcase};
use crate::auth::Headers; use crate::auth::Headers;
use crate::db::DbConn;
use crate::error::Error;
#[put("/devices/identifier/<uuid>/clear-token")] #[put("/devices/identifier/<uuid>/clear-token")]
fn clear_device_token(uuid: String) -> EmptyResult { fn clear_device_token(uuid: String) -> EmptyResult {
@ -137,12 +137,13 @@ fn hibp_breach(username: String) -> JsonResult {
use reqwest::{header::USER_AGENT, Client}; use reqwest::{header::USER_AGENT, Client};
let value: Value = Client::new() let res = Client::new().get(&url).header(USER_AGENT, user_agent).send()?;
.get(&url)
.header(USER_AGENT, user_agent)
.send()?
.error_for_status()?
.json()?;
// If we get a 404, return a 404, it means no breached accounts
if res.status() == 404 {
return Err(Error::empty().with_code(404));
}
let value: Value = res.error_for_status()?.json()?;
Ok(Json(value)) Ok(Json(value))
} }

Datei anzeigen

@ -5,16 +5,18 @@ use std::error::Error as StdError;
macro_rules! make_error { macro_rules! make_error {
( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => { ( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => {
const BAD_REQUEST: u16 = 400;
#[derive(Display)] #[derive(Display)]
pub enum ErrorKind { $($name( $ty )),+ } pub enum ErrorKind { $($name( $ty )),+ }
pub struct Error { message: String, error: ErrorKind } pub struct Error { message: String, error: ErrorKind, error_code: u16 }
$(impl From<$ty> for Error { $(impl From<$ty> for Error {
fn from(err: $ty) -> Self { Error::from((stringify!($name), err)) } fn from(err: $ty) -> Self { Error::from((stringify!($name), err)) }
})+ })+
$(impl<S: Into<String>> From<(S, $ty)> for Error { $(impl<S: Into<String>> From<(S, $ty)> for Error {
fn from(val: (S, $ty)) -> Self { fn from(val: (S, $ty)) -> Self {
Error { message: val.0.into(), error: ErrorKind::$name(val.1) } Error { message: val.0.into(), error: ErrorKind::$name(val.1), error_code: BAD_REQUEST }
} }
})+ })+
impl StdError for Error { impl StdError for Error {
@ -43,12 +45,17 @@ use std::time::SystemTimeError as TimeErr;
use u2f::u2ferror::U2fError as U2fErr; use u2f::u2ferror::U2fError as U2fErr;
use yubico::yubicoerror::YubicoError as YubiErr; use yubico::yubicoerror::YubicoError as YubiErr;
#[derive(Display, Serialize)]
pub struct Empty {}
// Error struct // Error struct
// Contains a String error message, meant for the user and an enum variant, with an error of different types. // Contains a String error message, meant for the user and an enum variant, with an error of different types.
// //
// After the variant itself, there are two expressions. The first one indicates whether the error contains a source error (that we pretty print). // After the variant itself, there are two expressions. The first one indicates whether the error contains a source error (that we pretty print).
// The second one contains the function used to obtain the response sent to the client // The second one contains the function used to obtain the response sent to the client
make_error! { make_error! {
// Just an empty error
EmptyError(Empty): _no_source, _serialize,
// Used to represent err! calls // Used to represent err! calls
SimpleError(String): _no_source, _api_error, SimpleError(String): _no_source, _api_error,
// Used for special return values, like 2FA errors // Used for special return values, like 2FA errors
@ -80,10 +87,19 @@ impl Error {
(usr_msg, log_msg.into()).into() (usr_msg, log_msg.into()).into()
} }
pub fn empty() -> Self {
Empty {}.into()
}
pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self { pub fn with_msg<M: Into<String>>(mut self, msg: M) -> Self {
self.message = msg.into(); self.message = msg.into();
self self
} }
pub fn with_code(mut self, code: u16) -> Self {
self.error_code = code;
self
}
} }
pub trait MapResult<S> { pub trait MapResult<S> {
@ -142,8 +158,10 @@ impl<'r> Responder<'r> for Error {
let usr_msg = format!("{}", self); let usr_msg = format!("{}", self);
error!("{:#?}", self); error!("{:#?}", self);
let code = Status::from_code(self.error_code).unwrap_or(Status::BadRequest);
Response::build() Response::build()
.status(Status::BadRequest) .status(code)
.header(ContentType::JSON) .header(ContentType::JSON)
.sized_body(Cursor::new(usr_msg)) .sized_body(Cursor::new(usr_msg))
.ok() .ok()