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-07-11 13:54:55 +00:00
|
|
|
use std::convert::TryInto;
|
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)]
|
|
|
|
pub code: UserErrorCode,
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserError {
|
|
|
|
fn new(code: UserErrorCode, msg: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
code,
|
|
|
|
msg: msg.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
|
|
|
|
pub enum UserErrorCode {
|
|
|
|
#[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
|
|
|
|
|
|
|
#[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 format is not correct")]
|
|
|
|
EmailInvalid = 20,
|
|
|
|
#[display(fmt = "Password format is not correct")]
|
|
|
|
PasswordInvalid = 21,
|
2021-07-15 00:46:16 +00:00
|
|
|
#[display(fmt = "User name is invalid")]
|
2021-07-11 13:54:55 +00:00
|
|
|
UserNameInvalid = 22,
|
2021-07-14 13:12:52 +00:00
|
|
|
#[display(fmt = "User workspace is invalid")]
|
|
|
|
UserWorkspaceInvalid = 23,
|
|
|
|
#[display(fmt = "User id is invalid")]
|
|
|
|
UserIdInvalid = 24,
|
2021-07-19 13:05:49 +00:00
|
|
|
#[display(fmt = "Create user default workspace failed")]
|
|
|
|
CreateDefaultWorkspaceFailed = 25,
|
2021-07-11 13:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::default::Default for UserErrorCode {
|
|
|
|
fn default() -> Self { UserErrorCode::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 {
|
|
|
|
fn from(error: flowy_database::result::Error) -> Self {
|
2021-07-19 08:15:20 +00:00
|
|
|
ErrorBuilder::new(UserErrorCode::UserDatabaseInternalError)
|
2021-07-11 13:54:55 +00:00
|
|
|
.error(error)
|
|
|
|
.build()
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
2021-07-09 08:34:50 +00:00
|
|
|
}
|
2021-07-20 06:03:21 +00:00
|
|
|
|
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-07-19 08:15:20 +00:00
|
|
|
ErrorBuilder::new(UserErrorCode::UserDatabaseInternalError)
|
2021-07-11 13:54:55 +00:00
|
|
|
.error(error)
|
|
|
|
.build()
|
|
|
|
}
|
2021-07-09 08:34:50 +00:00
|
|
|
}
|
2021-07-09 15:31:44 +00:00
|
|
|
|
2021-07-11 13:54:55 +00:00
|
|
|
impl flowy_dispatch::Error for UserError {
|
|
|
|
fn as_response(&self) -> EventResponse {
|
|
|
|
let bytes: Vec<u8> = self.clone().try_into().unwrap();
|
|
|
|
ResponseBuilder::Err().data(bytes).build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ErrorBuilder {
|
|
|
|
pub code: UserErrorCode,
|
|
|
|
pub msg: Option<String>,
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:54:55 +00:00
|
|
|
impl ErrorBuilder {
|
|
|
|
pub fn new(code: UserErrorCode) -> Self { ErrorBuilder { code, msg: None } }
|
|
|
|
|
|
|
|
pub fn msg<T>(mut self, msg: T) -> Self
|
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
|
|
|
self.msg = Some(msg.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error<T>(mut self, msg: T) -> Self
|
|
|
|
where
|
|
|
|
T: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
self.msg = Some(format!("{:?}", msg));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(mut self) -> UserError {
|
|
|
|
UserError::new(self.code, &self.msg.take().unwrap_or("".to_owned()))
|
2021-07-10 08:27:20 +00:00
|
|
|
}
|
2021-07-09 15:31:44 +00:00
|
|
|
}
|