2021-08-20 22:00:03 +08:00
|
|
|
use bytes::Bytes;
|
2021-07-10 16:27:20 +08:00
|
|
|
use derive_more::Display;
|
2021-07-11 21:54:55 +08:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2021-07-12 13:53:32 +08:00
|
|
|
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
|
2021-09-01 16:37:46 +08:00
|
|
|
|
2021-08-24 13:10:53 +08:00
|
|
|
use std::{
|
|
|
|
convert::TryInto,
|
|
|
|
fmt::{Debug, Formatter},
|
|
|
|
};
|
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-08-30 22:44:17 +08:00
|
|
|
pub code: ErrorCode,
|
2021-07-11 21:54:55 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserError {
|
2021-09-03 12:44:48 +08:00
|
|
|
pub(crate) fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
|
2021-07-11 21:54:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:10:53 +08:00
|
|
|
#[derive(Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
|
2021-08-30 22:44:17 +08:00
|
|
|
pub enum ErrorCode {
|
2021-07-11 21:54:55 +08:00
|
|
|
#[display(fmt = "Unknown")]
|
|
|
|
Unknown = 0,
|
|
|
|
#[display(fmt = "Database init failed")]
|
2021-07-19 16:15:20 +08:00
|
|
|
UserDatabaseInitFailed = 1,
|
2021-09-03 16:43:03 +08:00
|
|
|
#[display(fmt = "Acquire database write lock failed")]
|
|
|
|
AcquireWriteLockedFailed = 2,
|
|
|
|
#[display(fmt = "Acquire database read lock failed")]
|
|
|
|
AcquireReadLockedFailed = 3,
|
2021-07-11 21:54:55 +08:00
|
|
|
#[display(fmt = "Opening database is not belonging to the current user")]
|
2021-07-19 16:15:20 +08:00
|
|
|
UserDatabaseDidNotMatch = 4,
|
2021-07-11 21:54:55 +08:00
|
|
|
|
2021-08-21 17:17:54 +08:00
|
|
|
#[display(fmt = "Email can not be empty or whitespace")]
|
|
|
|
EmailIsEmpty = 20,
|
|
|
|
#[display(fmt = "Email format is not valid")]
|
|
|
|
EmailFormatInvalid = 21,
|
|
|
|
#[display(fmt = "Email already exists")]
|
|
|
|
EmailAlreadyExists = 22,
|
|
|
|
#[display(fmt = "Password can not be empty or whitespace")]
|
|
|
|
PasswordIsEmpty = 30,
|
|
|
|
#[display(fmt = "Password format too long")]
|
|
|
|
PasswordTooLong = 31,
|
|
|
|
#[display(fmt = "Password contains forbidden characters.")]
|
|
|
|
PasswordContainsForbidCharacters = 32,
|
2021-09-03 12:44:48 +08:00
|
|
|
#[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
|
2021-08-21 17:17:54 +08:00
|
|
|
PasswordFormatInvalid = 33,
|
2021-09-01 16:08:32 +08:00
|
|
|
#[display(fmt = "Password not match")]
|
|
|
|
PasswordNotMatch = 34,
|
2021-08-23 23:02:42 +08:00
|
|
|
|
2021-08-21 17:17:54 +08:00
|
|
|
#[display(fmt = "User name is too long")]
|
|
|
|
UserNameTooLong = 40,
|
|
|
|
#[display(fmt = "User name contain forbidden characters")]
|
2021-09-08 13:50:20 +08:00
|
|
|
ContainForbiddenCharacters = 41,
|
2021-08-21 17:17:54 +08:00
|
|
|
#[display(fmt = "User name can not be empty or whitespace")]
|
|
|
|
UserNameIsEmpty = 42,
|
2021-07-14 21:12:52 +08:00
|
|
|
#[display(fmt = "User workspace is invalid")]
|
2021-08-21 17:17:54 +08:00
|
|
|
UserWorkspaceInvalid = 50,
|
2021-07-14 21:12:52 +08:00
|
|
|
#[display(fmt = "User id is invalid")]
|
2021-08-21 17:17:54 +08:00
|
|
|
UserIdInvalid = 51,
|
2021-09-07 23:30:43 +08:00
|
|
|
#[display(fmt = "User token is invalid")]
|
2021-09-08 13:50:20 +08:00
|
|
|
UserUnauthorized = 54,
|
2021-09-07 23:30:43 +08:00
|
|
|
#[display(fmt = "User not exist")]
|
|
|
|
UserNotExist = 55,
|
|
|
|
|
2021-09-08 13:50:20 +08:00
|
|
|
#[display(fmt = "Internal error")]
|
|
|
|
InternalError = 100,
|
2021-08-24 13:10:53 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl Debug for ErrorCode {
|
2021-08-24 13:10:53 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{}", self)) }
|
2021-07-11 21:54:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl ErrorCode {
|
2021-08-23 23:02:42 +08:00
|
|
|
pub fn to_string(&self) -> String { format!("{}", self) }
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl std::default::Default for ErrorCode {
|
|
|
|
fn default() -> Self { ErrorCode::Unknown }
|
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-08 13:50:20 +08:00
|
|
|
fn from(error: flowy_database::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
|
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-08 13:50:20 +08:00
|
|
|
fn from(error: r2d2::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
|
2021-08-31 23:01:46 +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-08 13:50:20 +08:00
|
|
|
fn from(error: flowy_sqlite::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
|
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-09-07 23:30:43 +08:00
|
|
|
let code = server_error_to_user_error(error.code);
|
|
|
|
ErrorBuilder::new(code).error(error.msg).build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use flowy_net::errors::ErrorCode as ServerErrorCode;
|
|
|
|
fn server_error_to_user_error(code: ServerErrorCode) -> ErrorCode {
|
|
|
|
match code {
|
2021-09-08 13:50:20 +08:00
|
|
|
ServerErrorCode::Unauthorized => ErrorCode::UserUnauthorized,
|
2021-09-07 23:30:43 +08:00
|
|
|
ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
|
|
|
|
ServerErrorCode::RecordNotFound => ErrorCode::UserNotExist,
|
2021-09-08 13:50:20 +08:00
|
|
|
_ => ErrorCode::InternalError,
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, UserError>;
|
2021-07-11 21:54:55 +08:00
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl flowy_infra::errors::Build<ErrorCode> for UserError {
|
|
|
|
fn build(code: ErrorCode, msg: String) -> Self {
|
2021-09-03 12:44:48 +08:00
|
|
|
let msg = if msg.is_empty() { format!("{}", code) } else { msg };
|
2021-08-21 17:17:54 +08:00
|
|
|
UserError::new(code, &msg)
|
|
|
|
}
|
2021-07-09 23:31:44 +08:00
|
|
|
}
|