2021-08-20 22:00:03 +08:00
|
|
|
use bytes::Bytes;
|
2021-07-13 17:19:39 +08:00
|
|
|
use derive_more::Display;
|
|
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
|
|
|
|
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
|
2021-08-30 22:44:17 +08:00
|
|
|
use flowy_net::errors::ErrorCode as NetworkErrorCode;
|
2021-09-05 13:50:23 +08:00
|
|
|
use std::{convert::TryInto, fmt};
|
2021-07-13 17:19:39 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf)]
|
|
|
|
|
pub struct WorkspaceError {
|
|
|
|
|
#[pb(index = 1)]
|
2021-08-30 22:44:17 +08:00
|
|
|
pub code: ErrorCode,
|
2021-07-13 17:19:39 +08:00
|
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
|
pub msg: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WorkspaceError {
|
2021-09-05 13:50:23 +08:00
|
|
|
pub fn new(code: ErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned() } }
|
2021-07-13 17:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
|
2021-08-30 22:44:17 +08:00
|
|
|
pub enum ErrorCode {
|
2021-07-13 17:19:39 +08:00
|
|
|
#[display(fmt = "Unknown")]
|
|
|
|
|
Unknown = 0,
|
|
|
|
|
|
|
|
|
|
#[display(fmt = "Workspace name is invalid")]
|
|
|
|
|
WorkspaceNameInvalid = 1,
|
|
|
|
|
|
|
|
|
|
#[display(fmt = "Workspace id is invalid")]
|
|
|
|
|
WorkspaceIdInvalid = 2,
|
|
|
|
|
|
2021-07-23 22:42:44 +08:00
|
|
|
#[display(fmt = "Color style of the App is invalid")]
|
2021-07-13 17:19:39 +08:00
|
|
|
AppColorStyleInvalid = 3,
|
|
|
|
|
|
2021-08-25 17:34:20 +08:00
|
|
|
#[display(fmt = "Workspace desc is invalid")]
|
|
|
|
|
WorkspaceDescInvalid = 4,
|
|
|
|
|
|
2021-08-28 23:08:12 +08:00
|
|
|
#[display(fmt = "Current workspace not found")]
|
|
|
|
|
CurrentWorkspaceNotFound = 5,
|
|
|
|
|
|
2021-07-23 22:42:44 +08:00
|
|
|
#[display(fmt = "Id of the App is invalid")]
|
2021-07-19 22:44:37 +08:00
|
|
|
AppIdInvalid = 10,
|
|
|
|
|
|
2021-07-23 22:42:44 +08:00
|
|
|
#[display(fmt = "Name of the App is invalid")]
|
2021-07-19 22:44:37 +08:00
|
|
|
AppNameInvalid = 11,
|
|
|
|
|
|
2021-07-23 22:42:44 +08:00
|
|
|
#[display(fmt = "Name of the View is invalid")]
|
2021-07-19 22:44:37 +08:00
|
|
|
ViewNameInvalid = 20,
|
|
|
|
|
|
|
|
|
|
#[display(fmt = "Thumbnail of the view is invalid")]
|
2021-07-23 22:42:44 +08:00
|
|
|
ViewThumbnailInvalid = 21,
|
|
|
|
|
|
|
|
|
|
#[display(fmt = "Id of the View is invalid")]
|
|
|
|
|
ViewIdInvalid = 22,
|
|
|
|
|
|
|
|
|
|
#[display(fmt = "Description of the View is invalid")]
|
|
|
|
|
ViewDescInvalid = 23,
|
2021-07-13 23:08:20 +08:00
|
|
|
|
|
|
|
|
#[display(fmt = "Get database connection failed")]
|
2021-07-19 22:44:37 +08:00
|
|
|
DatabaseConnectionFail = 100,
|
2021-07-13 23:08:20 +08:00
|
|
|
|
2021-07-13 17:19:39 +08:00
|
|
|
#[display(fmt = "Database internal error")]
|
2021-07-19 22:44:37 +08:00
|
|
|
WorkspaceDatabaseError = 101,
|
2021-07-14 21:12:52 +08:00
|
|
|
|
|
|
|
|
#[display(fmt = "User internal error")]
|
2021-07-19 22:44:37 +08:00
|
|
|
UserInternalError = 102,
|
2021-07-16 23:18:12 +08:00
|
|
|
|
|
|
|
|
#[display(fmt = "User not login yet")]
|
2021-07-19 22:44:37 +08:00
|
|
|
UserNotLoginYet = 103,
|
2021-08-24 21:38:53 +08:00
|
|
|
|
2021-08-29 22:00:42 +08:00
|
|
|
#[display(fmt = "UserIn is empty")]
|
|
|
|
|
UserIdIsEmpty = 104,
|
|
|
|
|
|
2021-08-24 21:38:53 +08:00
|
|
|
#[display(fmt = "Server error")]
|
|
|
|
|
ServerError = 1000,
|
2021-08-25 17:34:20 +08:00
|
|
|
#[display(fmt = "Record not found")]
|
|
|
|
|
RecordNotFound = 1001,
|
2021-07-13 17:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl std::default::Default for ErrorCode {
|
|
|
|
|
fn default() -> Self { ErrorCode::Unknown }
|
2021-07-13 17:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 21:38:53 +08:00
|
|
|
impl std::convert::From<flowy_net::errors::ServerError> for WorkspaceError {
|
|
|
|
|
fn from(error: flowy_net::errors::ServerError) -> Self {
|
2021-08-25 17:34:20 +08:00
|
|
|
match error.code {
|
2021-09-05 13:50:23 +08:00
|
|
|
NetworkErrorCode::RecordNotFound => ErrorBuilder::new(ErrorCode::RecordNotFound).error(error.msg).build(),
|
2021-08-25 17:34:20 +08:00
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
_ => ErrorBuilder::new(ErrorCode::ServerError).error(error.msg).build(),
|
2021-08-25 17:34:20 +08:00
|
|
|
}
|
2021-08-24 21:38:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 17:19:39 +08:00
|
|
|
impl std::convert::From<flowy_database::result::Error> for WorkspaceError {
|
2021-09-05 13:50:23 +08:00
|
|
|
fn from(error: flowy_database::result::Error) -> Self { ErrorBuilder::new(ErrorCode::WorkspaceDatabaseError).error(error).build() }
|
2021-07-13 17:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl flowy_dispatch::Error for WorkspaceError {
|
|
|
|
|
fn as_response(&self) -> EventResponse {
|
2021-08-20 22:00:03 +08:00
|
|
|
let bytes: Bytes = self.clone().try_into().unwrap();
|
2021-07-13 17:19:39 +08:00
|
|
|
ResponseBuilder::Err().data(bytes).build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 13:50:23 +08:00
|
|
|
impl fmt::Display for WorkspaceError {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}: {}", &self.code, &self.msg) }
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
pub type ErrorBuilder = flowy_infra::errors::Builder<ErrorCode, WorkspaceError>;
|
2021-07-13 17:19:39 +08:00
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl flowy_infra::errors::Build<ErrorCode> for WorkspaceError {
|
|
|
|
|
fn build(code: ErrorCode, msg: String) -> Self { WorkspaceError::new(code, &msg) }
|
2021-07-13 17:19:39 +08:00
|
|
|
}
|