From c44cd44933d5d5a2d45a01f5ed0b7e1a57eb99df Mon Sep 17 00:00:00 2001 From: Mashy Date: Sat, 20 Jul 2019 01:04:02 +1000 Subject: [PATCH] register account proper err handling --- client/src/components/login.jsx | 9 ++++----- server/src/account.rs | 11 ++++++----- server/src/net.rs | 23 ++++++++++++++++++++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/client/src/components/login.jsx b/client/src/components/login.jsx index 22fbdaff..4d822807 100644 --- a/client/src/components/login.jsx +++ b/client/src/components/login.jsx @@ -23,11 +23,10 @@ const addState = connect( function submitRegister(name, password, code) { postData('/register', { name, password, code }) - .then(res => { - if (!res.ok) return errorToast(res); - res.text() - }) - .then(res => { + .then(res => res.json()) + .then(data => { + if (!data.success) return errorToast(data.error_message); + console.log(data.response); ws.connect(); }) .catch(error => errorToast(error)); diff --git a/server/src/account.rs b/server/src/account.rs index 7232351a..3f2358af 100644 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -8,6 +8,7 @@ use serde_cbor::{from_slice}; use postgres::transaction::Transaction; +use net::MnmlHttpError; use names::{name as generate_name}; use construct::{Construct, construct_recover, construct_spawn}; use instance::{Instance, instance_delete}; @@ -212,17 +213,17 @@ pub fn set_subscribed(tx: &mut Transaction, id: Uuid, subscribed: bool) -> Resul Ok(name) } -pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result { +pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result { if password.len() < PASSWORD_MIN_LEN { - return Err(err_msg("password must be at least 12 characters")); + return Err(MnmlHttpError::PasswordUnacceptable); } if code.to_lowercase() != "grep842" { - return Err(err_msg("https://discord.gg/YJJgurM")); + return Err(MnmlHttpError::InvalidCode); } if name.len() == 0 { - return Err(err_msg("account name not supplied")); + return Err(MnmlHttpError::AccountNameNotProvided); } let id = Uuid::new_v4(); @@ -246,7 +247,7 @@ pub fn create(name: &String, password: &String, code: &String, tx: &mut Transact match result.iter().next() { Some(row) => row, - None => return Err(err_msg("account not created")), + None => return Err(MnmlHttpError::DbError), }; // 3 constructs for a team and 1 to swap diff --git a/server/src/net.rs b/server/src/net.rs index b7d1ffc6..f030a896 100644 --- a/server/src/net.rs +++ b/server/src/net.rs @@ -32,6 +32,8 @@ pub enum MnmlHttpError { #[fail(display="bad request")] BadRequest, #[fail(display="account name taken or invalid")] + AccountNameNotProvided, + #[fail(display="account name not provided")] AccountNameTaken, #[fail(display="password unacceptable. must be > 11 characters")] PasswordUnacceptable, @@ -39,6 +41,24 @@ pub enum MnmlHttpError { InvalidCode, } +impl From for MnmlHttpError { + fn from(_err: bcrypt::BcryptError) -> Self { + MnmlHttpError::ServerError + } +} + +impl From for MnmlHttpError { + fn from(_err: postgres::Error) -> Self { + MnmlHttpError::DbError + } +} + +impl From for MnmlHttpError { + fn from(_err: failure::Error) -> Self { + MnmlHttpError::ServerError + } +} + #[derive(Serialize, Deserialize)] struct JsonResponse { response: Option, @@ -73,6 +93,7 @@ impl From for IronError { MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError), MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized), MnmlHttpError::BadRequest => (m_err.compat(), status::BadRequest), + MnmlHttpError::AccountNameNotProvided => (m_err.compat(), status::BadRequest), MnmlHttpError::AccountNameTaken => (m_err.compat(), status::BadRequest), MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest), MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized), @@ -169,7 +190,7 @@ fn register(req: &mut Request) -> IronResult { }, Err(e) => { warn!("{:?}", e); - Err(IronError::from(MnmlHttpError::BadRequest)) + Err(IronError::from(e)) } } }