AppFlowy/rust-lib/flowy-user/src/errors.rs

159 lines
5.4 KiB
Rust
Raw Normal View History

2021-08-20 14:00:03 +00:00
use bytes::Bytes;
use derive_more::Display;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
2021-09-13 07:51:13 +00:00
use std::{convert::TryInto, fmt::Debug};
#[derive(Debug, Default, Clone, ProtoBuf)]
pub struct UserError {
#[pb(index = 1)]
2021-08-30 14:44:17 +00:00
pub code: ErrorCode,
#[pb(index = 2)]
pub msg: String,
}
2021-09-16 04:35:55 +00:00
macro_rules! static_user_error {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
pub fn $name() -> UserError {
UserError {
code: $status,
msg: format!("{}", $status),
}
}
};
}
impl UserError {
2021-09-27 15:23:23 +00:00
pub(crate) fn new(code: ErrorCode, msg: &str) -> Self {
Self {
code,
msg: msg.to_owned(),
}
}
2021-09-16 04:35:55 +00:00
2021-09-27 15:23:23 +00:00
pub(crate) fn code(code: ErrorCode) -> Self {
Self {
code,
msg: "".to_owned(),
}
}
2021-09-16 04:35:55 +00: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-09-13 05:05:46 +00:00
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
2021-08-30 14:44:17 +00:00
pub enum ErrorCode {
2021-08-21 09:17:54 +00:00
#[display(fmt = "Email can not be empty or whitespace")]
2021-09-16 04:35:55 +00:00
EmailIsEmpty = 0,
2021-08-21 09:17:54 +00:00
#[display(fmt = "Email format is not valid")]
2021-09-16 04:35:55 +00:00
EmailFormatInvalid = 1,
2021-08-21 09:17:54 +00:00
#[display(fmt = "Email already exists")]
2021-09-16 04:35:55 +00:00
EmailAlreadyExists = 2,
2021-08-21 09:17:54 +00:00
#[display(fmt = "Password can not be empty or whitespace")]
2021-09-16 04:35:55 +00:00
PasswordIsEmpty = 10,
2021-08-21 09:17:54 +00:00
#[display(fmt = "Password format too long")]
2021-09-16 04:35:55 +00:00
PasswordTooLong = 11,
2021-08-21 09:17:54 +00:00
#[display(fmt = "Password contains forbidden characters.")]
2021-09-16 04:35:55 +00:00
PasswordContainsForbidCharacters = 12,
2021-09-03 04:44:48 +00:00
#[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
2021-09-16 04:35:55 +00:00
PasswordFormatInvalid = 13,
2021-09-01 08:08:32 +00:00
#[display(fmt = "Password not match")]
2021-09-16 04:35:55 +00:00
PasswordNotMatch = 14,
2021-08-21 09:17:54 +00:00
#[display(fmt = "User name is too long")]
2021-09-16 04:35:55 +00:00
UserNameTooLong = 20,
2021-08-21 09:17:54 +00:00
#[display(fmt = "User name contain forbidden characters")]
2021-09-16 04:35:55 +00:00
UserNameContainForbiddenCharacters = 21,
2021-08-21 09:17:54 +00:00
#[display(fmt = "User name can not be empty or whitespace")]
2021-09-16 04:35:55 +00:00
UserNameIsEmpty = 22,
#[display(fmt = "User id is invalid")]
2021-09-16 04:35:55 +00:00
UserIdInvalid = 23,
2021-09-07 15:30:43 +00:00
#[display(fmt = "User token is invalid")]
2021-09-16 04:35:55 +00:00
UserUnauthorized = 24,
2021-09-07 15:30:43 +00:00
#[display(fmt = "User not exist")]
2021-09-16 04:35:55 +00:00
UserNotExist = 25,
2021-09-07 15:30:43 +00:00
#[display(fmt = "Internal error")]
2021-09-16 04:35:55 +00:00
InternalError = 100,
}
impl std::convert::Into<UserError> for ErrorCode {
fn into(self) -> UserError { UserError::new(self, "") }
2021-08-24 05:10:53 +00:00
}
2021-08-30 14:44:17 +00:00
impl std::default::Default for ErrorCode {
2021-09-16 04:35:55 +00:00
fn default() -> Self { ErrorCode::InternalError }
}
impl std::convert::From<flowy_database::Error> for UserError {
fn from(error: flowy_database::Error) -> Self {
match error {
2021-09-16 04:35:55 +00:00
flowy_database::Error::NotFound => UserError::user_not_exist().context(error),
_ => UserError::internal().context(error),
}
}
}
2021-08-31 15:01:46 +00:00
impl std::convert::From<::r2d2::Error> for UserError {
2021-09-16 04:35:55 +00:00
fn from(error: r2d2::Error) -> Self { UserError::internal().context(error) }
2021-08-31 15:01:46 +00:00
}
2021-09-18 14:32:00 +00:00
impl std::convert::From<flowy_ws::errors::WsError> for UserError {
2021-09-23 07:49:10 +00: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 14:32:00 +00:00
}
2021-07-24 10:55:13 +00:00
// use diesel::result::{Error, DatabaseErrorKind};
// use flowy_sqlite::ErrorKind;
impl std::convert::From<flowy_sqlite::Error> for UserError {
2021-09-16 04:35:55 +00:00
fn from(error: flowy_sqlite::Error) -> Self { UserError::internal().context(error) }
}
2021-07-09 15:31:44 +00:00
2021-08-23 00:27:29 +00:00
impl std::convert::From<flowy_net::errors::ServerError> for UserError {
fn from(error: flowy_net::errors::ServerError) -> Self {
2021-09-07 15:30:43 +00:00
let code = server_error_to_user_error(error.code);
2021-09-16 04:35:55 +00:00
UserError::new(code, &error.msg)
2021-09-07 15:30:43 +00:00
}
}
use flowy_net::errors::ErrorCode as ServerErrorCode;
fn server_error_to_user_error(code: ServerErrorCode) -> ErrorCode {
match code {
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
2021-09-07 15:30:43 +00:00
ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
_ => ErrorCode::InternalError,
2021-08-21 05:35:15 +00:00
}
}
impl flowy_dispatch::Error for UserError {
fn as_response(&self) -> EventResponse {
2021-08-20 14:00:03 +00:00
let bytes: Bytes = self.clone().try_into().unwrap();
ResponseBuilder::Err().data(bytes).build()
}
}