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 EditorError { #[pb(index = 1)] pub code: UserErrorCode, #[pb(index = 2)] pub msg: String, } impl EditorError { fn new(code: EditorErrorCode, msg: &str) -> Self { Self { code, msg: msg.to_owned(), } } } #[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)] pub enum EditorErrorCode { #[display(fmt = "Unknown")] Unknown = 0, #[display(fmt = "EditorDBInternalError")] EditorDBInternalError = 1, #[display(fmt = "DocNameInvalid")] DocNameInvalid = 10, #[display(fmt = "DocViewIdInvalid")] DocViewIdInvalid = 11, } impl std::default::Default for UserErrorCode { fn default() -> Self { UserErrorCode::Unknown } } impl std::convert::From for EditorError { fn from(error: flowy_database::result::Error) -> Self { ErrorBuilder::new(EditorErrorCode::EditorDBInternalError) .error(error) .build() } } impl flowy_dispatch::Error for EditorError { fn as_response(&self) -> EventResponse { let bytes: Vec = self.clone().try_into().unwrap(); ResponseBuilder::Err().data(bytes).build() } } pub struct ErrorBuilder { pub code: UserErrorCode, pub msg: Option, } impl ErrorBuilder { pub fn new(code: EditorErrorCode) -> Self { ErrorBuilder { code, msg: None } } pub fn msg(mut self, msg: T) -> Self where T: Into, { self.msg = Some(msg.into()); self } pub fn error(mut self, msg: T) -> Self where T: std::fmt::Debug, { self.msg = Some(format!("{:?}", msg)); self } pub fn build(mut self) -> EditorError { EditorError::new(self.code, &self.msg.take().unwrap_or("".to_owned())) } }