2021-08-20 14:00:03 +00:00
|
|
|
use bytes::Bytes;
|
2021-07-10 08:27:20 +00:00
|
|
|
use derive_more::Display;
|
2021-07-11 13:54:55 +00:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2021-07-12 05:53:32 +00:00
|
|
|
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
|
2021-09-01 08:37:46 +00:00
|
|
|
|
2021-08-24 05:10:53 +00:00
|
|
|
use std::{
|
|
|
|
convert::TryInto,
|
|
|
|
fmt::{Debug, Formatter},
|
|
|
|
};
|
2021-07-09 08:34:50 +00:00
|
|
|
|
2021-07-11 13:54:55 +00:00
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf)]
|
|
|
|
pub struct UserError {
|
|
|
|
#[pb(index = 1)]
|
2021-08-30 14:44:17 +00:00
|
|
|
pub code: ErrorCode,
|
2021-07-11 13:54:55 +00:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserError {
|
2021-09-03 04:44:48 +00:00
|
|
|
pub(crate) fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
|
2021-07-11 13:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 05:10:53 +00:00
|
|
|
#[derive(Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
|
2021-08-30 14:44:17 +00:00
|
|
|
pub enum ErrorCode {
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "Unknown")]
|
|
|
|
Unknown = 0,
|
|
|
|
#[display(fmt = "Database init failed")]
|
2021-07-19 08:15:20 +00:00
|
|
|
UserDatabaseInitFailed = 1,
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "Get database write lock failed")]
|
2021-07-19 08:15:20 +00:00
|
|
|
UserDatabaseWriteLocked = 2,
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "Get database read lock failed")]
|
2021-07-19 08:15:20 +00:00
|
|
|
UserDatabaseReadLocked = 3,
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "Opening database is not belonging to the current user")]
|
2021-07-19 08:15:20 +00:00
|
|
|
UserDatabaseDidNotMatch = 4,
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "Database internal error")]
|
2021-07-19 08:15:20 +00:00
|
|
|
UserDatabaseInternalError = 5,
|
2021-07-11 13:54:55 +00:00
|
|
|
|
2021-07-24 10:55:13 +00:00
|
|
|
#[display(fmt = "Sql internal error")]
|
|
|
|
SqlInternalError = 6,
|
|
|
|
|
2021-08-31 15:01:46 +00:00
|
|
|
#[display(fmt = "r2d2 connection error")]
|
|
|
|
DatabaseConnectError = 7,
|
|
|
|
|
2021-07-11 13:54:55 +00:00
|
|
|
#[display(fmt = "User not login yet")]
|
|
|
|
UserNotLoginYet = 10,
|
|
|
|
#[display(fmt = "Get current id read lock failed")]
|
|
|
|
ReadCurrentIdFailed = 11,
|
|
|
|
#[display(fmt = "Get current id write lock failed")]
|
|
|
|
WriteCurrentIdFailed = 12,
|
|
|
|
|
2021-08-21 09:17:54 +00: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 04:44:48 +00:00
|
|
|
#[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
|
2021-08-21 09:17:54 +00:00
|
|
|
PasswordFormatInvalid = 33,
|
2021-09-01 08:08:32 +00:00
|
|
|
#[display(fmt = "Password not match")]
|
|
|
|
PasswordNotMatch = 34,
|
2021-08-23 15:02:42 +00:00
|
|
|
|
2021-08-21 09:17:54 +00:00
|
|
|
#[display(fmt = "User name is too long")]
|
|
|
|
UserNameTooLong = 40,
|
|
|
|
#[display(fmt = "User name contain forbidden characters")]
|
|
|
|
UserNameContainsForbiddenCharacters = 41,
|
|
|
|
#[display(fmt = "User name can not be empty or whitespace")]
|
|
|
|
UserNameIsEmpty = 42,
|
2021-07-14 13:12:52 +00:00
|
|
|
#[display(fmt = "User workspace is invalid")]
|
2021-08-21 09:17:54 +00:00
|
|
|
UserWorkspaceInvalid = 50,
|
2021-07-14 13:12:52 +00:00
|
|
|
#[display(fmt = "User id is invalid")]
|
2021-08-21 09:17:54 +00:00
|
|
|
UserIdInvalid = 51,
|
2021-07-19 13:05:49 +00:00
|
|
|
#[display(fmt = "Create user default workspace failed")]
|
2021-08-21 09:17:54 +00:00
|
|
|
CreateDefaultWorkspaceFailed = 52,
|
2021-07-24 07:05:47 +00:00
|
|
|
|
|
|
|
#[display(fmt = "User default workspace already exists")]
|
2021-08-21 09:17:54 +00:00
|
|
|
DefaultWorkspaceAlreadyExist = 53,
|
2021-08-21 05:35:15 +00:00
|
|
|
|
2021-08-24 05:10:53 +00:00
|
|
|
#[display(fmt = "Server error")]
|
|
|
|
ServerError = 100,
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:44:17 +00:00
|
|
|
impl Debug for ErrorCode {
|
2021-08-24 05:10:53 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{}", self)) }
|
2021-07-11 13:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 14:44:17 +00:00
|
|
|
impl ErrorCode {
|
2021-08-23 15:02:42 +00:00
|
|
|
pub fn to_string(&self) -> String { format!("{}", self) }
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:44:17 +00:00
|
|
|
impl std::default::Default for ErrorCode {
|
|
|
|
fn default() -> Self { ErrorCode::Unknown }
|
2021-07-09 08:34:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 08:27:20 +00:00
|
|
|
impl std::convert::From<flowy_database::result::Error> for UserError {
|
2021-09-03 04:44:48 +00:00
|
|
|
fn from(error: flowy_database::result::Error) -> Self { ErrorBuilder::new(ErrorCode::UserDatabaseInternalError).error(error).build() }
|
2021-07-09 08:34:50 +00:00
|
|
|
}
|
2021-08-31 15:01:46 +00:00
|
|
|
|
|
|
|
impl std::convert::From<::r2d2::Error> for UserError {
|
2021-09-03 04:44:48 +00:00
|
|
|
fn from(error: r2d2::Error) -> Self { ErrorBuilder::new(ErrorCode::DatabaseConnectError).error(error).build() }
|
2021-08-31 15:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 10:55:13 +00:00
|
|
|
// use diesel::result::{Error, DatabaseErrorKind};
|
|
|
|
// use flowy_sqlite::ErrorKind;
|
2021-07-10 08:27:20 +00:00
|
|
|
impl std::convert::From<flowy_sqlite::Error> for UserError {
|
2021-07-11 13:54:55 +00:00
|
|
|
fn from(error: flowy_sqlite::Error) -> Self {
|
2021-07-19 13:05:49 +00:00
|
|
|
// match error.kind() {
|
|
|
|
// ErrorKind::Msg(_) => {},
|
|
|
|
// ErrorKind::R2D2(_) => {},
|
|
|
|
// ErrorKind::Migrations(_) => {},
|
|
|
|
// ErrorKind::Diesel(diesel_err) => match diesel_err {
|
|
|
|
// Error::InvalidCString(_) => {},
|
|
|
|
// Error::DatabaseError(kind, _) => {
|
|
|
|
// match kind {
|
|
|
|
// DatabaseErrorKind::UniqueViolation => {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// _ => {}
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// Error::NotFound => {},
|
|
|
|
// Error::QueryBuilderError(_) => {},
|
|
|
|
// Error::DeserializationError(_) => {},
|
|
|
|
// Error::SerializationError(_) => {},
|
|
|
|
// Error::RollbackTransaction => {},
|
|
|
|
// Error::AlreadyInTransaction => {},
|
|
|
|
// Error::__Nonexhaustive => {},
|
|
|
|
// },
|
|
|
|
// ErrorKind::Connection(_) => {},
|
|
|
|
// ErrorKind::Io(_) => {},
|
|
|
|
// ErrorKind::UnknownMigrationExists(_) => {},
|
|
|
|
// ErrorKind::__Nonexhaustive { .. } => {},
|
|
|
|
// }
|
|
|
|
|
2021-09-03 04:44:48 +00:00
|
|
|
ErrorBuilder::new(ErrorCode::SqlInternalError).error(error).build()
|
2021-07-11 13:54:55 +00:00
|
|
|
}
|
2021-07-09 08:34:50 +00:00
|
|
|
}
|
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-01 08:08:32 +00:00
|
|
|
match error.code {
|
2021-09-03 04:44:48 +00:00
|
|
|
flowy_net::errors::ErrorCode::PasswordNotMatch => ErrorBuilder::new(ErrorCode::PasswordNotMatch).error(error.msg).build(),
|
|
|
|
_ => ErrorBuilder::new(ErrorCode::ServerError).error(error.msg).build(),
|
2021-09-01 08:08:32 +00:00
|
|
|
}
|
2021-08-21 05:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:54:55 +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();
|
2021-07-11 13:54:55 +00:00
|
|
|
ResponseBuilder::Err().data(bytes).build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 14:44:17 +00:00
|
|
|
pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, UserError>;
|
2021-07-11 13:54:55 +00:00
|
|
|
|
2021-08-30 14:44:17 +00:00
|
|
|
impl flowy_infra::errors::Build<ErrorCode> for UserError {
|
|
|
|
fn build(code: ErrorCode, msg: String) -> Self {
|
2021-09-03 04:44:48 +00:00
|
|
|
let msg = if msg.is_empty() { format!("{}", code) } else { msg };
|
2021-08-21 09:17:54 +00:00
|
|
|
UserError::new(code, &msg)
|
|
|
|
}
|
2021-07-09 15:31:44 +00:00
|
|
|
}
|