94 lines
2.5 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};
use std::convert::TryInto;
#[derive(Debug, Default, Clone, ProtoBuf)]
pub struct WorkspaceError {
#[pb(index = 1)]
2021-07-25 08:13:59 +08:00
pub code: WsErrCode,
2021-07-13 17:19:39 +08:00
#[pb(index = 2)]
pub msg: String,
}
impl WorkspaceError {
2021-07-25 08:13:59 +08:00
pub fn new(code: WsErrCode, msg: &str) -> Self {
2021-07-13 17:19:39 +08:00
Self {
code,
msg: msg.to_owned(),
}
}
}
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
2021-07-25 08:13:59 +08:00
pub enum WsErrCode {
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-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,
#[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-07-13 17:19:39 +08:00
}
2021-07-25 08:13:59 +08:00
impl std::default::Default for WsErrCode {
fn default() -> Self { WsErrCode::Unknown }
2021-07-13 17:19:39 +08:00
}
impl std::convert::From<flowy_database::result::Error> for WorkspaceError {
fn from(error: flowy_database::result::Error) -> Self {
2021-07-25 08:13:59 +08:00
ErrorBuilder::new(WsErrCode::WorkspaceDatabaseError)
2021-07-13 17:19:39 +08:00
.error(error)
.build()
}
}
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-08-21 13:35:15 +08:00
pub type ErrorBuilder = flowy_infra::errors::Builder<WsErrCode, WorkspaceError>;
2021-07-13 17:19:39 +08:00
2021-08-21 13:35:15 +08:00
impl flowy_infra::errors::Build<WsErrCode> for WorkspaceError {
fn build(code: WsErrCode, msg: String) -> Self { WorkspaceError::new(code, &msg) }
2021-07-13 17:19:39 +08:00
}