register account proper err handling
This commit is contained in:
parent
b289ed92de
commit
c44cd44933
@ -23,11 +23,10 @@ const addState = connect(
|
|||||||
|
|
||||||
function submitRegister(name, password, code) {
|
function submitRegister(name, password, code) {
|
||||||
postData('/register', { name, password, code })
|
postData('/register', { name, password, code })
|
||||||
.then(res => {
|
.then(res => res.json())
|
||||||
if (!res.ok) return errorToast(res);
|
.then(data => {
|
||||||
res.text()
|
if (!data.success) return errorToast(data.error_message);
|
||||||
})
|
console.log(data.response);
|
||||||
.then(res => {
|
|
||||||
ws.connect();
|
ws.connect();
|
||||||
})
|
})
|
||||||
.catch(error => errorToast(error));
|
.catch(error => errorToast(error));
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use serde_cbor::{from_slice};
|
|||||||
|
|
||||||
use postgres::transaction::Transaction;
|
use postgres::transaction::Transaction;
|
||||||
|
|
||||||
|
use net::MnmlHttpError;
|
||||||
use names::{name as generate_name};
|
use names::{name as generate_name};
|
||||||
use construct::{Construct, construct_recover, construct_spawn};
|
use construct::{Construct, construct_recover, construct_spawn};
|
||||||
use instance::{Instance, instance_delete};
|
use instance::{Instance, instance_delete};
|
||||||
@ -212,17 +213,17 @@ pub fn set_subscribed(tx: &mut Transaction, id: Uuid, subscribed: bool) -> Resul
|
|||||||
Ok(name)
|
Ok(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result<String, Error> {
|
pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result<String, MnmlHttpError> {
|
||||||
if password.len() < PASSWORD_MIN_LEN {
|
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" {
|
if code.to_lowercase() != "grep842" {
|
||||||
return Err(err_msg("https://discord.gg/YJJgurM"));
|
return Err(MnmlHttpError::InvalidCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
if name.len() == 0 {
|
if name.len() == 0 {
|
||||||
return Err(err_msg("account name not supplied"));
|
return Err(MnmlHttpError::AccountNameNotProvided);
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = Uuid::new_v4();
|
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() {
|
match result.iter().next() {
|
||||||
Some(row) => row,
|
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
|
// 3 constructs for a team and 1 to swap
|
||||||
|
|||||||
@ -32,6 +32,8 @@ pub enum MnmlHttpError {
|
|||||||
#[fail(display="bad request")]
|
#[fail(display="bad request")]
|
||||||
BadRequest,
|
BadRequest,
|
||||||
#[fail(display="account name taken or invalid")]
|
#[fail(display="account name taken or invalid")]
|
||||||
|
AccountNameNotProvided,
|
||||||
|
#[fail(display="account name not provided")]
|
||||||
AccountNameTaken,
|
AccountNameTaken,
|
||||||
#[fail(display="password unacceptable. must be > 11 characters")]
|
#[fail(display="password unacceptable. must be > 11 characters")]
|
||||||
PasswordUnacceptable,
|
PasswordUnacceptable,
|
||||||
@ -39,6 +41,24 @@ pub enum MnmlHttpError {
|
|||||||
InvalidCode,
|
InvalidCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<bcrypt::BcryptError> for MnmlHttpError {
|
||||||
|
fn from(_err: bcrypt::BcryptError) -> Self {
|
||||||
|
MnmlHttpError::ServerError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<postgres::Error> for MnmlHttpError {
|
||||||
|
fn from(_err: postgres::Error) -> Self {
|
||||||
|
MnmlHttpError::DbError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<failure::Error> for MnmlHttpError {
|
||||||
|
fn from(_err: failure::Error) -> Self {
|
||||||
|
MnmlHttpError::ServerError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct JsonResponse {
|
struct JsonResponse {
|
||||||
response: Option<String>,
|
response: Option<String>,
|
||||||
@ -73,6 +93,7 @@ impl From<MnmlHttpError> for IronError {
|
|||||||
MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError),
|
MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError),
|
||||||
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
|
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
|
||||||
MnmlHttpError::BadRequest => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::BadRequest => (m_err.compat(), status::BadRequest),
|
||||||
|
MnmlHttpError::AccountNameNotProvided => (m_err.compat(), status::BadRequest),
|
||||||
MnmlHttpError::AccountNameTaken => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::AccountNameTaken => (m_err.compat(), status::BadRequest),
|
||||||
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
|
||||||
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
|
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
|
||||||
@ -169,7 +190,7 @@ fn register(req: &mut Request) -> IronResult<Response> {
|
|||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("{:?}", e);
|
warn!("{:?}", e);
|
||||||
Err(IronError::from(MnmlHttpError::BadRequest))
|
Err(IronError::from(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user