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-16 12:35:55 +08:00
|
|
|
use std::{convert::TryInto, fmt, fmt::Debug};
|
2021-07-13 17:19:39 +08:00
|
|
|
|
2021-10-13 23:11:45 +08:00
|
|
|
pub type WorkspaceResult<T> = std::result::Result<T, WorkspaceError>;
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-16 12:35:55 +08:00
|
|
|
macro_rules! static_workspace_error {
|
|
|
|
|
($name:ident, $status:expr) => {
|
|
|
|
|
#[allow(non_snake_case, missing_docs)]
|
|
|
|
|
pub fn $name() -> WorkspaceError {
|
|
|
|
|
WorkspaceError {
|
|
|
|
|
code: $status,
|
|
|
|
|
msg: format!("{}", $status),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 17:19:39 +08:00
|
|
|
impl WorkspaceError {
|
2021-10-01 19:39:08 +08:00
|
|
|
pub fn new(code: ErrorCode, msg: &str) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
code,
|
|
|
|
|
msg: msg.to_owned(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-16 12:35:55 +08:00
|
|
|
|
|
|
|
|
static_workspace_error!(workspace_name, ErrorCode::WorkspaceNameInvalid);
|
|
|
|
|
static_workspace_error!(workspace_id, ErrorCode::WorkspaceIdInvalid);
|
|
|
|
|
static_workspace_error!(color_style, ErrorCode::AppColorStyleInvalid);
|
|
|
|
|
static_workspace_error!(workspace_desc, ErrorCode::WorkspaceDescInvalid);
|
|
|
|
|
static_workspace_error!(app_name, ErrorCode::AppNameInvalid);
|
|
|
|
|
static_workspace_error!(app_id, ErrorCode::AppIdInvalid);
|
|
|
|
|
static_workspace_error!(view_name, ErrorCode::ViewNameInvalid);
|
|
|
|
|
static_workspace_error!(view_thumbnail, ErrorCode::ViewThumbnailInvalid);
|
|
|
|
|
static_workspace_error!(view_id, ErrorCode::ViewIdInvalid);
|
|
|
|
|
static_workspace_error!(view_desc, ErrorCode::ViewDescInvalid);
|
|
|
|
|
static_workspace_error!(view_data, ErrorCode::ViewDataInvalid);
|
|
|
|
|
static_workspace_error!(unauthorized, ErrorCode::UserUnauthorized);
|
|
|
|
|
static_workspace_error!(internal, ErrorCode::InternalError);
|
|
|
|
|
static_workspace_error!(not_found, ErrorCode::RecordNotFound);
|
2021-09-20 15:38:55 +08:00
|
|
|
static_workspace_error!(ws, ErrorCode::WsConnectError);
|
2021-09-16 12:35:55 +08:00
|
|
|
|
|
|
|
|
pub fn context<T: Debug>(mut self, error: T) -> Self {
|
|
|
|
|
self.msg = format!("{:?}", error);
|
|
|
|
|
self
|
|
|
|
|
}
|
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 = "Workspace name is invalid")]
|
2021-09-16 12:35:55 +08:00
|
|
|
WorkspaceNameInvalid = 0,
|
2021-07-13 17:19:39 +08:00
|
|
|
|
|
|
|
|
#[display(fmt = "Workspace id is invalid")]
|
2021-09-16 12:35:55 +08:00
|
|
|
WorkspaceIdInvalid = 1,
|
2021-07-13 17:19:39 +08:00
|
|
|
|
2021-07-23 22:42:44 +08:00
|
|
|
#[display(fmt = "Color style of the App is invalid")]
|
2021-09-16 12:35:55 +08:00
|
|
|
AppColorStyleInvalid = 2,
|
2021-07-13 17:19:39 +08:00
|
|
|
|
2021-08-25 17:34:20 +08:00
|
|
|
#[display(fmt = "Workspace desc is invalid")]
|
2021-09-16 12:35:55 +08:00
|
|
|
WorkspaceDescInvalid = 3,
|
2021-08-28 23:08:12 +08:00
|
|
|
|
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-09-08 13:50:20 +08:00
|
|
|
#[display(fmt = "User unauthorized")]
|
2021-09-16 12:35:55 +08:00
|
|
|
UserUnauthorized = 100,
|
2021-08-29 22:00:42 +08:00
|
|
|
|
2021-09-20 15:38:55 +08:00
|
|
|
#[display(fmt = "Workspace websocket error")]
|
|
|
|
|
WsConnectError = 200,
|
|
|
|
|
|
2021-08-24 21:38:53 +08:00
|
|
|
#[display(fmt = "Server error")]
|
2021-09-08 13:50:20 +08:00
|
|
|
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-10-01 19:39:08 +08:00
|
|
|
pub fn internal_error<T>(e: T) -> WorkspaceError
|
|
|
|
|
where
|
|
|
|
|
T: std::fmt::Debug,
|
|
|
|
|
{
|
|
|
|
|
WorkspaceError::internal().context(e)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 22:44:17 +08:00
|
|
|
impl std::default::Default for ErrorCode {
|
2021-09-16 12:35:55 +08:00
|
|
|
fn default() -> Self { ErrorCode::InternalError }
|
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 {
|
2021-09-16 12:35:55 +08:00
|
|
|
fn from(error: DocError) -> Self { WorkspaceError::internal().context(error) }
|
2021-09-11 14:26:30 +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-09-08 13:50:20 +08:00
|
|
|
let code = server_error_to_workspace_error(error.code);
|
2021-09-16 12:35:55 +08:00
|
|
|
WorkspaceError::new(code, &error.msg)
|
2021-08-24 21:38:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 21:31:04 +08:00
|
|
|
impl std::convert::From<flowy_database::Error> for WorkspaceError {
|
2021-09-16 12:35:55 +08:00
|
|
|
fn from(error: flowy_database::Error) -> Self { WorkspaceError::internal().context(error) }
|
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-09-08 13:50:20 +08:00
|
|
|
fn server_error_to_workspace_error(code: ServerErrorCode) -> ErrorCode {
|
|
|
|
|
match code {
|
2021-09-09 15:43:05 +08:00
|
|
|
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
|
2021-09-08 13:50:20 +08:00
|
|
|
ServerErrorCode::RecordNotFound => ErrorCode::RecordNotFound,
|
|
|
|
|
_ => ErrorCode::InternalError,
|
|
|
|
|
}
|
|
|
|
|
}
|