2021-08-20 22:00:03 +08:00
|
|
|
use bytes::Bytes;
|
2021-11-07 21:45:18 +08:00
|
|
|
|
|
|
|
use flowy_derive::ProtoBuf;
|
2021-07-12 13:53:32 +08:00
|
|
|
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
|
2021-11-07 21:45:18 +08:00
|
|
|
pub use flowy_user_infra::errors::ErrorCode;
|
2021-09-28 15:29:29 +08:00
|
|
|
use std::{convert::TryInto, fmt, fmt::Debug};
|
2021-07-09 16:34:50 +08:00
|
|
|
|
2021-07-11 21:54:55 +08:00
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf)]
|
|
|
|
pub struct UserError {
|
|
|
|
#[pb(index = 1)]
|
2021-11-07 21:45:18 +08:00
|
|
|
pub code: i32,
|
2021-07-11 21:54:55 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
2021-09-28 15:29:29 +08:00
|
|
|
impl std::fmt::Display for UserError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:35:55 +08:00
|
|
|
macro_rules! static_user_error {
|
2021-11-07 21:45:18 +08:00
|
|
|
($name:ident, $code:expr) => {
|
2021-09-16 12:35:55 +08:00
|
|
|
#[allow(non_snake_case, missing_docs)]
|
2021-11-07 21:45:18 +08:00
|
|
|
pub fn $name() -> UserError { $code.into() }
|
2021-09-16 12:35:55 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-11 21:54:55 +08:00
|
|
|
impl UserError {
|
2021-09-27 23:23:23 +08:00
|
|
|
pub(crate) fn new(code: ErrorCode, msg: &str) -> Self {
|
|
|
|
Self {
|
2021-11-07 21:45:18 +08:00
|
|
|
code: code.value(),
|
2021-09-27 23:23:23 +08:00
|
|
|
msg: msg.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 12:35:55 +08:00
|
|
|
|
|
|
|
pub fn context<T: Debug>(mut self, error: T) -> Self {
|
|
|
|
self.msg = format!("{:?}", error);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
static_user_error!(email_empty, ErrorCode::EmailIsEmpty);
|
|
|
|
static_user_error!(email_format, ErrorCode::EmailFormatInvalid);
|
|
|
|
static_user_error!(email_exist, ErrorCode::EmailAlreadyExists);
|
|
|
|
static_user_error!(password_empty, ErrorCode::PasswordIsEmpty);
|
|
|
|
static_user_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
|
|
|
|
static_user_error!(password_forbid_char, ErrorCode::PasswordContainsForbidCharacters);
|
|
|
|
static_user_error!(password_format, ErrorCode::PasswordFormatInvalid);
|
|
|
|
static_user_error!(password_not_match, ErrorCode::PasswordNotMatch);
|
|
|
|
static_user_error!(name_too_long, ErrorCode::UserNameTooLong);
|
|
|
|
static_user_error!(name_forbid_char, ErrorCode::UserNameContainForbiddenCharacters);
|
|
|
|
static_user_error!(name_empty, ErrorCode::UserNameIsEmpty);
|
|
|
|
static_user_error!(user_id, ErrorCode::UserIdInvalid);
|
|
|
|
static_user_error!(unauthorized, ErrorCode::UserUnauthorized);
|
|
|
|
static_user_error!(user_not_exist, ErrorCode::UserNotExist);
|
|
|
|
static_user_error!(internal, ErrorCode::InternalError);
|
2021-07-11 21:54:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 21:45:18 +08:00
|
|
|
impl std::convert::From<ErrorCode> for UserError {
|
|
|
|
fn from(code: ErrorCode) -> Self {
|
|
|
|
UserError {
|
|
|
|
code: code.value(),
|
|
|
|
msg: format!("{}", code),
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 16:34:50 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
impl std::convert::From<flowy_database::Error> for UserError {
|
2021-09-11 20:09:46 +08:00
|
|
|
fn from(error: flowy_database::Error) -> Self {
|
|
|
|
match error {
|
2021-09-16 12:35:55 +08:00
|
|
|
flowy_database::Error::NotFound => UserError::user_not_exist().context(error),
|
|
|
|
_ => UserError::internal().context(error),
|
2021-09-11 20:09:46 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 16:34:50 +08:00
|
|
|
}
|
2021-08-31 23:01:46 +08:00
|
|
|
|
|
|
|
impl std::convert::From<::r2d2::Error> for UserError {
|
2021-09-16 12:35:55 +08:00
|
|
|
fn from(error: r2d2::Error) -> Self { UserError::internal().context(error) }
|
2021-08-31 23:01:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-18 22:32:00 +08:00
|
|
|
impl std::convert::From<flowy_ws::errors::WsError> for UserError {
|
2021-09-23 15:49:10 +08:00
|
|
|
fn from(error: flowy_ws::errors::WsError) -> Self {
|
|
|
|
match error.code {
|
|
|
|
flowy_ws::errors::ErrorCode::InternalError => UserError::internal().context(error.msg),
|
|
|
|
_ => UserError::internal().context(error),
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 22:32:00 +08:00
|
|
|
}
|
|
|
|
|
2021-07-24 18:55:13 +08:00
|
|
|
// use diesel::result::{Error, DatabaseErrorKind};
|
|
|
|
// use flowy_sqlite::ErrorKind;
|
2021-07-10 16:27:20 +08:00
|
|
|
impl std::convert::From<flowy_sqlite::Error> for UserError {
|
2021-09-16 12:35:55 +08:00
|
|
|
fn from(error: flowy_sqlite::Error) -> Self { UserError::internal().context(error) }
|
2021-07-09 16:34:50 +08:00
|
|
|
}
|
2021-07-09 23:31:44 +08:00
|
|
|
|
2021-08-23 08:27:29 +08:00
|
|
|
impl std::convert::From<flowy_net::errors::ServerError> for UserError {
|
|
|
|
fn from(error: flowy_net::errors::ServerError) -> Self {
|
2021-10-09 16:43:56 +08:00
|
|
|
let (code, msg) = server_error_to_user_error(error);
|
|
|
|
UserError::new(code, &msg)
|
2021-09-07 23:30:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use flowy_net::errors::ErrorCode as ServerErrorCode;
|
2021-10-09 16:43:56 +08:00
|
|
|
fn server_error_to_user_error(error: flowy_net::errors::ServerError) -> (ErrorCode, String) {
|
|
|
|
let code = match error.code {
|
2021-09-09 15:43:05 +08:00
|
|
|
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
|
2021-09-07 23:30:43 +08:00
|
|
|
ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
|
|
|
|
ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
|
2021-11-07 21:45:18 +08:00
|
|
|
ServerErrorCode::ConnectRefused | ServerErrorCode::ConnectTimeout | ServerErrorCode::ConnectClose => {
|
|
|
|
ErrorCode::ServerError
|
|
|
|
},
|
2021-09-08 13:50:20 +08:00
|
|
|
_ => ErrorCode::InternalError,
|
2021-10-09 16:43:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if code != ErrorCode::InternalError {
|
|
|
|
let msg = format!("{}", &code);
|
|
|
|
(code, msg)
|
|
|
|
} else {
|
|
|
|
(code, error.msg)
|
2021-08-21 13:35:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 21:54:55 +08:00
|
|
|
impl flowy_dispatch::Error for UserError {
|
|
|
|
fn as_response(&self) -> EventResponse {
|
2021-08-20 22:00:03 +08:00
|
|
|
let bytes: Bytes = self.clone().try_into().unwrap();
|
2021-07-11 21:54:55 +08:00
|
|
|
ResponseBuilder::Err().data(bytes).build()
|
|
|
|
}
|
|
|
|
}
|