2023-01-30 03:11:19 +00:00
|
|
|
use std::fmt::Debug;
|
2023-05-21 10:53:59 +00:00
|
|
|
|
|
|
|
use anyhow::Result;
|
2022-12-28 13:49:38 +00:00
|
|
|
use thiserror::Error;
|
2021-12-14 10:04:51 +00:00
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
use flowy_derive::ProtoBuf;
|
|
|
|
|
|
|
|
use crate::code::ErrorCode;
|
|
|
|
|
2022-12-28 13:49:38 +00:00
|
|
|
pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
|
2021-12-14 07:31:44 +00:00
|
|
|
|
2022-12-28 13:49:38 +00:00
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf, Error)]
|
|
|
|
#[error("{code:?}: {msg}")]
|
2021-12-14 07:31:44 +00:00
|
|
|
pub struct FlowyError {
|
2023-02-13 01:29:49 +00:00
|
|
|
#[pb(index = 1)]
|
|
|
|
pub code: i32,
|
2021-12-14 07:31:44 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
#[pb(index = 2)]
|
|
|
|
pub msg: String,
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
macro_rules! static_flowy_error {
|
2023-02-13 01:29:49 +00:00
|
|
|
($name:ident, $code:expr) => {
|
|
|
|
#[allow(non_snake_case, missing_docs)]
|
|
|
|
pub fn $name() -> FlowyError {
|
|
|
|
$code.into()
|
|
|
|
}
|
|
|
|
};
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowyError {
|
2023-05-21 10:53:59 +00:00
|
|
|
pub fn new<T: ToString>(code: ErrorCode, msg: T) -> Self {
|
2023-02-13 01:29:49 +00:00
|
|
|
Self {
|
|
|
|
code: code.value() as i32,
|
2023-05-21 10:53:59 +00:00
|
|
|
msg: msg.to_string(),
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
|
|
|
pub fn context<T: Debug>(mut self, error: T) -> Self {
|
|
|
|
self.msg = format!("{:?}", error);
|
|
|
|
self
|
|
|
|
}
|
2021-12-14 07:31:44 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
pub fn is_record_not_found(&self) -> bool {
|
|
|
|
self.code == ErrorCode::RecordNotFound.value()
|
|
|
|
}
|
2023-01-30 03:11:19 +00:00
|
|
|
|
2023-02-13 01:29:49 +00:00
|
|
|
static_flowy_error!(internal, ErrorCode::Internal);
|
|
|
|
static_flowy_error!(record_not_found, ErrorCode::RecordNotFound);
|
|
|
|
static_flowy_error!(workspace_name, ErrorCode::WorkspaceNameInvalid);
|
|
|
|
static_flowy_error!(workspace_id, ErrorCode::WorkspaceIdInvalid);
|
|
|
|
static_flowy_error!(color_style, ErrorCode::AppColorStyleInvalid);
|
|
|
|
static_flowy_error!(workspace_desc, ErrorCode::WorkspaceDescTooLong);
|
|
|
|
static_flowy_error!(app_name, ErrorCode::AppNameInvalid);
|
|
|
|
static_flowy_error!(invalid_app_id, ErrorCode::AppIdInvalid);
|
|
|
|
static_flowy_error!(view_name, ErrorCode::ViewNameInvalid);
|
|
|
|
static_flowy_error!(view_thumbnail, ErrorCode::ViewThumbnailInvalid);
|
2023-02-26 08:27:17 +00:00
|
|
|
static_flowy_error!(invalid_view_id, ErrorCode::ViewIdIsInvalid);
|
2023-02-13 01:29:49 +00:00
|
|
|
static_flowy_error!(view_desc, ErrorCode::ViewDescTooLong);
|
|
|
|
static_flowy_error!(view_data, ErrorCode::ViewDataInvalid);
|
|
|
|
static_flowy_error!(unauthorized, ErrorCode::UserUnauthorized);
|
|
|
|
static_flowy_error!(connection, ErrorCode::HttpServerConnectError);
|
|
|
|
static_flowy_error!(email_empty, ErrorCode::EmailIsEmpty);
|
|
|
|
static_flowy_error!(email_format, ErrorCode::EmailFormatInvalid);
|
|
|
|
static_flowy_error!(email_exist, ErrorCode::EmailAlreadyExists);
|
|
|
|
static_flowy_error!(password_empty, ErrorCode::PasswordIsEmpty);
|
|
|
|
static_flowy_error!(passworkd_too_long, ErrorCode::PasswordTooLong);
|
|
|
|
static_flowy_error!(
|
|
|
|
password_forbid_char,
|
|
|
|
ErrorCode::PasswordContainsForbidCharacters
|
|
|
|
);
|
|
|
|
static_flowy_error!(password_format, ErrorCode::PasswordFormatInvalid);
|
|
|
|
static_flowy_error!(password_not_match, ErrorCode::PasswordNotMatch);
|
|
|
|
static_flowy_error!(name_too_long, ErrorCode::UserNameTooLong);
|
|
|
|
static_flowy_error!(
|
|
|
|
name_forbid_char,
|
|
|
|
ErrorCode::UserNameContainForbiddenCharacters
|
|
|
|
);
|
|
|
|
static_flowy_error!(name_empty, ErrorCode::UserNameIsEmpty);
|
|
|
|
static_flowy_error!(user_id, ErrorCode::UserIdInvalid);
|
|
|
|
static_flowy_error!(user_not_exist, ErrorCode::UserNotExist);
|
|
|
|
static_flowy_error!(text_too_long, ErrorCode::TextTooLong);
|
|
|
|
static_flowy_error!(invalid_data, ErrorCode::InvalidData);
|
|
|
|
static_flowy_error!(out_of_bounds, ErrorCode::OutOfBounds);
|
|
|
|
static_flowy_error!(serde, ErrorCode::Serde);
|
|
|
|
static_flowy_error!(field_record_not_found, ErrorCode::FieldRecordNotFound);
|
2023-05-21 10:53:59 +00:00
|
|
|
static_flowy_error!(payload_none, ErrorCode::UnexpectedEmpty);
|
2023-02-13 01:29:49 +00:00
|
|
|
static_flowy_error!(http, ErrorCode::HttpError);
|
2023-03-08 13:19:44 +00:00
|
|
|
static_flowy_error!(
|
|
|
|
unexpect_calendar_field_type,
|
|
|
|
ErrorCode::UnexpectedCalendarFieldType
|
|
|
|
);
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<ErrorCode> for FlowyError {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn from(code: ErrorCode) -> Self {
|
|
|
|
FlowyError {
|
|
|
|
code: code.value() as i32,
|
|
|
|
msg: format!("{}", code),
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
2023-02-13 01:29:49 +00:00
|
|
|
}
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn internal_error<T>(e: T) -> FlowyError
|
|
|
|
where
|
2023-02-13 01:29:49 +00:00
|
|
|
T: std::fmt::Debug,
|
2021-12-14 07:31:44 +00:00
|
|
|
{
|
2023-02-13 01:29:49 +00:00
|
|
|
FlowyError::internal().context(e)
|
2021-12-14 07:31:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:04:51 +00:00
|
|
|
impl std::convert::From<std::io::Error> for FlowyError {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn from(error: std::io::Error) -> Self {
|
|
|
|
FlowyError::internal().context(error)
|
|
|
|
}
|
2021-12-14 10:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<protobuf::ProtobufError> for FlowyError {
|
2023-02-13 01:29:49 +00:00
|
|
|
fn from(e: protobuf::ProtobufError) -> Self {
|
|
|
|
FlowyError::internal().context(e)
|
|
|
|
}
|
2021-12-14 10:04:51 +00:00
|
|
|
}
|
2023-05-21 10:53:59 +00:00
|
|
|
|
|
|
|
impl From<anyhow::Error> for FlowyError {
|
|
|
|
fn from(e: anyhow::Error) -> Self {
|
|
|
|
FlowyError::internal().context(e)
|
|
|
|
}
|
|
|
|
}
|