123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- use bytes::Bytes;
- use derive_more::Display;
- use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
- use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
- use std::{
- convert::TryInto,
- fmt::{Debug, Formatter},
- };
- #[derive(Debug, Default, Clone, ProtoBuf)]
- pub struct UserError {
- #[pb(index = 1)]
- pub code: ErrorCode,
- #[pb(index = 2)]
- pub msg: String,
- }
- impl UserError {
- pub(crate) fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
- }
- #[derive(Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
- pub enum ErrorCode {
- #[display(fmt = "Unknown")]
- Unknown = 0,
- #[display(fmt = "Database init failed")]
- UserDatabaseInitFailed = 1,
- #[display(fmt = "Acquire database write lock failed")]
- AcquireWriteLockedFailed = 2,
- #[display(fmt = "Acquire database read lock failed")]
- AcquireReadLockedFailed = 3,
- #[display(fmt = "Opening database is not belonging to the current user")]
- UserDatabaseDidNotMatch = 4,
- #[display(fmt = "Database internal error")]
- UserDatabaseInternalError = 5,
- #[display(fmt = "Sql internal error")]
- SqlInternalError = 6,
- #[display(fmt = "r2d2 connection error")]
- DatabaseConnectError = 7,
- #[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,
- #[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,
- #[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
- PasswordFormatInvalid = 33,
- #[display(fmt = "Password not match")]
- PasswordNotMatch = 34,
- #[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,
- #[display(fmt = "User workspace is invalid")]
- UserWorkspaceInvalid = 50,
- #[display(fmt = "User id is invalid")]
- UserIdInvalid = 51,
- #[display(fmt = "Create user default workspace failed")]
- CreateDefaultWorkspaceFailed = 52,
- #[display(fmt = "User default workspace already exists")]
- DefaultWorkspaceAlreadyExist = 53,
- #[display(fmt = "Server error")]
- ServerError = 100,
- }
- impl Debug for ErrorCode {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{}", self)) }
- }
- impl ErrorCode {
- pub fn to_string(&self) -> String { format!("{}", self) }
- }
- impl std::default::Default for ErrorCode {
- fn default() -> Self { ErrorCode::Unknown }
- }
- impl std::convert::From<flowy_database::result::Error> for UserError {
- fn from(error: flowy_database::result::Error) -> Self { ErrorBuilder::new(ErrorCode::UserDatabaseInternalError).error(error).build() }
- }
- impl std::convert::From<::r2d2::Error> for UserError {
- fn from(error: r2d2::Error) -> Self { ErrorBuilder::new(ErrorCode::DatabaseConnectError).error(error).build() }
- }
- // use diesel::result::{Error, DatabaseErrorKind};
- // use flowy_sqlite::ErrorKind;
- impl std::convert::From<flowy_sqlite::Error> for UserError {
- fn from(error: flowy_sqlite::Error) -> Self {
- // 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 { .. } => {},
- // }
- ErrorBuilder::new(ErrorCode::SqlInternalError).error(error).build()
- }
- }
- impl std::convert::From<flowy_net::errors::ServerError> for UserError {
- fn from(error: flowy_net::errors::ServerError) -> Self {
- match error.code {
- flowy_net::errors::ErrorCode::PasswordNotMatch => ErrorBuilder::new(ErrorCode::PasswordNotMatch).error(error.msg).build(),
- _ => ErrorBuilder::new(ErrorCode::ServerError).error(error.msg).build(),
- }
- }
- }
- impl flowy_dispatch::Error for UserError {
- fn as_response(&self) -> EventResponse {
- let bytes: Bytes = self.clone().try_into().unwrap();
- ResponseBuilder::Err().data(bytes).build()
- }
- }
- pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, UserError>;
- impl flowy_infra::errors::Build<ErrorCode> for UserError {
- fn build(code: ErrorCode, msg: String) -> Self {
- let msg = if msg.is_empty() { format!("{}", code) } else { msg };
- UserError::new(code, &msg)
- }
- }
|