121 lines
3.6 KiB
Rust
Raw Normal View History

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-09-11 14:26:30 +08:00
use flowy_document::errors::DocError;
use flowy_net::errors::ErrorCode as ServerErrorCode;
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
2021-09-14 16:22:44 +08:00
#[display(fmt = "View data is invalid")]
ViewDataInvalid = 24,
2021-08-29 22:00:42 +08:00
#[display(fmt = "UserIn is empty")]
2021-09-11 14:26:30 +08:00
UserIdIsEmpty = 100,
#[display(fmt = "User unauthorized")]
2021-09-11 14:26:30 +08:00
UserUnauthorized = 101,
2021-08-29 22:00:42 +08:00
2021-08-24 21:38:53 +08:00
#[display(fmt = "Server error")]
InternalError = 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-09-11 14:26:30 +08:00
impl std::convert::From<flowy_document::errors::DocError> for WorkspaceError {
fn from(error: DocError) -> Self { ErrorBuilder::new(ErrorCode::InternalError).error(error).build() }
}
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 {
let code = server_error_to_workspace_error(error.code);
ErrorBuilder::new(code).error(error.msg).build()
2021-08-24 21:38:53 +08:00
}
}
impl std::convert::From<flowy_database::Error> for WorkspaceError {
2021-09-11 14:26:30 +08:00
fn from(error: flowy_database::Error) -> Self { ErrorBuilder::new(ErrorCode::InternalError).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 {
2021-09-13 13:05:46 +08:00
fn build(code: ErrorCode, msg: String) -> Self {
let msg = if msg.is_empty() { format!("{}", code) } else { msg };
WorkspaceError::new(code, &msg)
}
2021-07-13 17:19:39 +08:00
}
fn server_error_to_workspace_error(code: ServerErrorCode) -> ErrorCode {
match code {
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
ServerErrorCode::RecordNotFound => ErrorCode::RecordNotFound,
_ => ErrorCode::InternalError,
}
}