127 lines
4.0 KiB
Rust
Raw Normal View History

2023-01-30 11:11:19 +08:00
use std::fmt::Debug;
use anyhow::Result;
use thiserror::Error;
2021-12-14 18:04:51 +08:00
use flowy_derive::ProtoBuf;
use crate::code::ErrorCode;
pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
2021-12-14 15:31:44 +08:00
#[derive(Debug, Default, Clone, ProtoBuf, Error)]
#[error("{code:?}: {msg}")]
2021-12-14 15:31:44 +08:00
pub struct FlowyError {
#[pb(index = 1)]
pub code: i32,
2021-12-14 15:31:44 +08:00
#[pb(index = 2)]
pub msg: String,
2021-12-14 15:31:44 +08:00
}
2021-12-14 18:04:51 +08:00
macro_rules! static_flowy_error {
($name:ident, $code:expr) => {
#[allow(non_snake_case, missing_docs)]
pub fn $name() -> FlowyError {
$code.into()
}
};
2021-12-14 15:31:44 +08:00
}
impl FlowyError {
pub fn new<T: ToString>(code: ErrorCode, msg: T) -> Self {
Self {
code: code.value(),
msg: msg.to_string(),
2021-12-14 15:31:44 +08:00
}
}
pub fn context<T: Debug>(mut self, error: T) -> Self {
self.msg = format!("{:?}", error);
self
}
2021-12-14 15:31:44 +08:00
pub fn is_record_not_found(&self) -> bool {
self.code == ErrorCode::RecordNotFound.value()
}
2023-01-30 11:11:19 +08: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);
static_flowy_error!(invalid_view_id, ErrorCode::ViewIdIsInvalid);
static_flowy_error!(view_desc, ErrorCode::ViewDescTooLong);
static_flowy_error!(view_data, ErrorCode::ViewDataInvalid);
static_flowy_error!(unauthorized, ErrorCode::UserUnauthorized);
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::InvalidParams);
static_flowy_error!(out_of_bounds, ErrorCode::OutOfBounds);
static_flowy_error!(serde, ErrorCode::Serde);
static_flowy_error!(field_record_not_found, ErrorCode::FieldRecordNotFound);
static_flowy_error!(payload_none, ErrorCode::UnexpectedEmpty);
static_flowy_error!(http, ErrorCode::HttpError);
static_flowy_error!(
unexpect_calendar_field_type,
ErrorCode::UnexpectedCalendarFieldType
);
2021-12-14 15:31:44 +08:00
}
impl std::convert::From<ErrorCode> for FlowyError {
fn from(code: ErrorCode) -> Self {
FlowyError {
code: code.value(),
msg: format!("{}", code),
2021-12-14 15:31:44 +08:00
}
}
2021-12-14 15:31:44 +08:00
}
pub fn internal_error<T>(e: T) -> FlowyError
where
T: std::fmt::Debug,
2021-12-14 15:31:44 +08:00
{
FlowyError::internal().context(e)
2021-12-14 15:31:44 +08:00
}
2021-12-14 18:04:51 +08:00
impl std::convert::From<std::io::Error> for FlowyError {
fn from(error: std::io::Error) -> Self {
FlowyError::internal().context(error)
}
2021-12-14 18:04:51 +08:00
}
impl std::convert::From<protobuf::ProtobufError> for FlowyError {
fn from(e: protobuf::ProtobufError) -> Self {
FlowyError::internal().context(e)
}
2021-12-14 18:04:51 +08:00
}
impl From<anyhow::Error> for FlowyError {
fn from(e: anyhow::Error) -> Self {
feat: migrate user data to cloud (#3078) * refactor: weak passed-in params in handler * refactor: rename struct * chore: update tables * chore: update schema * chore: add permission * chore: update tables * chore: support transaction mode * chore: workspace database id * chore: add user workspace * feat: return list of workspaces * chore: add user to workspace * feat: separate database row table * refactor: update schema * chore: partition table * chore: use transaction * refactor: dir * refactor: collab db ref * fix: collab db lock * chore: rename files * chore: add tables descriptions * chore: update readme * docs: update documentation * chore: rename crate * chore: update ref * chore: update tests * chore: update tests * refactor: crate deps * chore: update crate ref * chore: remove unused deps * chore: remove unused deps * chore: update collab crate refs * chore: replace client with transaction in pooler * refactor: return error type * refactor: use anyhow error in deps * feat: supabase postgrest user signin (wip) * fix: Cargo.toml source git deps, changed Error to anyhow::Error * fix: uuid serialization * chore: fix conflict * chore: extend the response * feat: add implementation place holders * feat: impl get_user_workspaces * feat: impl get_user_profile * test: create workspace * fix: postgrest: field names and alias * chore: implement folder restful api * chore: implement collab storate with restful api * feat: added placeholders for impl: update_user_profile, check_user * feat: impl: update_user_profile * feat: impl: check_user * fix: use UidResponse, add more debug info for serde serialization error * fix: get_user_profile: use Optional<UserProfileResponse> * chore: imple init sync * chore: support soft delete * feat: postgresql: add migration test * feat: postgresql migration test: added UID display and colored output * feat: postgresql migration test: workspace role * feat: postgresql migration test: create shared common utils * feat: postgresql migration test: fixed shebang * chore: add flush_collab_update pg function * chore: implement datbaase and document restful api * chore: migrate to use restful api * chore: update table schema * chore: fix tests * chore: remove unused code * chore: format code * chore: remove unused env * fix: tauri build * fix: tauri build --------- Co-authored-by: Fu Zi Xiang <speed2exe@live.com.sg>
2023-07-29 09:46:24 +08:00
e.downcast::<FlowyError>()
.unwrap_or_else(|err| FlowyError::new(ErrorCode::Internal, err))
}
}