265 lines
8.3 KiB
Rust
Raw Normal View History

use collab::error::CollabError;
use protobuf::ProtobufError;
use std::convert::TryInto;
use std::fmt::{Debug, Display};
use thiserror::Error;
use tokio::task::JoinError;
use validator::{ValidationError, ValidationErrors};
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:{code}, message:{msg}")]
2021-12-14 15:31:44 +08:00
pub struct FlowyError {
#[pb(index = 1)]
pub code: ErrorCode,
2021-12-14 15:31:44 +08:00
#[pb(index = 2)]
pub msg: String,
#[pb(index = 3)]
pub payload: Vec<u8>,
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,
msg: msg.to_string(),
payload: vec![],
2021-12-14 15:31:44 +08:00
}
}
pub fn with_context<T: Display>(mut self, error: T) -> Self {
self.msg = format!("{}", error);
self
}
2021-12-14 15:31:44 +08:00
pub fn with_payload<T: TryInto<Vec<u8>, Error = ProtobufError>>(mut self, payload: T) -> Self {
self.payload = payload.try_into().unwrap_or_default();
self
}
pub fn is_record_not_found(&self) -> bool {
self.code == ErrorCode::RecordNotFound
}
2023-01-30 11:11:19 +08:00
pub fn is_already_exists(&self) -> bool {
self.code == ErrorCode::RecordAlreadyExists
}
pub fn is_unauthorized(&self) -> bool {
self.code == ErrorCode::UserUnauthorized || self.code == ErrorCode::RecordNotFound
}
pub fn is_invalid_data(&self) -> bool {
self.code == ErrorCode::InvalidParams
}
pub fn is_local_version_not_support(&self) -> bool {
self.code == ErrorCode::LocalVersionNotSupport
}
feat: ai billing (#5741) * feat: start on AI plan+billing UI * chore: enable plan and billing * feat: cache workspace subscription + minor fixes (#5705) * feat: update api from billing * feat: add api for workspace subscription info (#5717) * feat: refactor and start integrating AI plans * feat: refine UI and add business logic for AI * feat: complete UIUX for AI and limits * chore: remove resolved todo * chore: localize remove addon dialog * chore: fix spacing issue for usage * fix: interpret subscription + usage on action * chore: update api for billing (#5735) * chore: update revisions * fix: remove subscription cache * fix: copy improvements + use consistent dialog * chore: update to the latest client api * feat: support updating billing period * Feat/ai billing cancel reason (#5752) * chore: add cancellation reason field * fix: ci add one retry for concurrent sign up * chore: merge with main * chore: half merge * chore: fix conflict * chore: observer error * chore: remove unneeded protobuf and remove unwrap * feat: added subscription plan details * chore: check error code and update sidebar toast * chore: periodically check billing state * chore: editor ai error * chore: return file upload error * chore: fmt * chore: clippy * chore: disable upload image when exceed storage limitation * chore: remove todo * chore: remove openai i18n * chore: update log * chore: update client-api to fix stream error * chore: clippy * chore: fix language file * chore: disable billing UI --------- Co-authored-by: Zack Fu Zi Xiang <speed2exe@live.com.sg> Co-authored-by: nathan <nathan@appflowy.io>
2024-07-22 09:43:48 +02:00
pub fn is_file_limit_exceeded(&self) -> bool {
self.code == ErrorCode::FileStorageLimitExceeded
}
pub fn is_single_file_limit_exceeded(&self) -> bool {
self.code == ErrorCode::SingleUploadLimitExceeded
}
pub fn should_retry_upload(&self) -> bool {
!matches!(
self.code,
ErrorCode::FileStorageLimitExceeded | ErrorCode::SingleUploadLimitExceeded
)
feat: ai billing (#5741) * feat: start on AI plan+billing UI * chore: enable plan and billing * feat: cache workspace subscription + minor fixes (#5705) * feat: update api from billing * feat: add api for workspace subscription info (#5717) * feat: refactor and start integrating AI plans * feat: refine UI and add business logic for AI * feat: complete UIUX for AI and limits * chore: remove resolved todo * chore: localize remove addon dialog * chore: fix spacing issue for usage * fix: interpret subscription + usage on action * chore: update api for billing (#5735) * chore: update revisions * fix: remove subscription cache * fix: copy improvements + use consistent dialog * chore: update to the latest client api * feat: support updating billing period * Feat/ai billing cancel reason (#5752) * chore: add cancellation reason field * fix: ci add one retry for concurrent sign up * chore: merge with main * chore: half merge * chore: fix conflict * chore: observer error * chore: remove unneeded protobuf and remove unwrap * feat: added subscription plan details * chore: check error code and update sidebar toast * chore: periodically check billing state * chore: editor ai error * chore: return file upload error * chore: fmt * chore: clippy * chore: disable upload image when exceed storage limitation * chore: remove todo * chore: remove openai i18n * chore: update log * chore: update client-api to fix stream error * chore: clippy * chore: fix language file * chore: disable billing UI --------- Co-authored-by: Zack Fu Zi Xiang <speed2exe@live.com.sg> Co-authored-by: nathan <nathan@appflowy.io>
2024-07-22 09:43:48 +02:00
}
pub fn is_ai_response_limit_exceeded(&self) -> bool {
self.code == ErrorCode::AIResponseLimitExceeded
}
2025-01-22 09:42:24 +08:00
pub fn is_ai_image_response_limit_exceeded(&self) -> bool {
self.code == ErrorCode::AIImageResponseLimitExceeded
}
pub fn is_local_ai_not_ready(&self) -> bool {
self.code == ErrorCode::LocalAINotReady
}
2025-03-30 15:15:59 +08:00
pub fn is_local_ai_disabled(&self) -> bool {
self.code == ErrorCode::LocalAIDisabled
}
pub fn is_ai_max_required(&self) -> bool {
self.code == ErrorCode::AIMaxRequired
}
static_flowy_error!(internal, ErrorCode::Internal);
static_flowy_error!(record_not_found, ErrorCode::RecordNotFound);
static_flowy_error!(workspace_initialize, ErrorCode::WorkspaceInitializeError);
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_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!(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);
2025-03-05 10:23:28 +08:00
static_flowy_error!(http, ErrorCode::NetworkError);
static_flowy_error!(
unexpect_calendar_field_type,
ErrorCode::UnexpectedCalendarFieldType
);
static_flowy_error!(collab_not_sync, ErrorCode::CollabDataNotSync);
static_flowy_error!(server_error, ErrorCode::InternalServerError);
static_flowy_error!(not_support, ErrorCode::NotSupportYet);
static_flowy_error!(local_version_not_support, ErrorCode::LocalVersionNotSupport);
feat: search mvp (#5064) * feat: implement folder indexer * feat: sqlite search views using fts5 * feat: add view indexing to user manager * feat: implement folder indexer * feat: add sqlite search documents * feat: add document indexing to user manager * feat: add document indexing to folder indexer * chore: update collab rev * feat: search frontend integration * refactor: search index * test: add event test * chore: fix ci * feat: initial command palette overlay impl (#4619) * chore: test search engine * chore: initial structure * chore: replace old search request * chore: enable log for lib-dispatch * chore: move search manager to core * feat: move traits and responsibility to search crate * feat: move search to search crate * feat: replace sqlite with tantivy * feat: deserialize tantivy documents * chore: fixes after rebase * chore: clean code * feat: fetch and sort results * fix: code review + cleaning * feat: support custom icons * feat: support view layout icons * feat: rename bloc and fix indexing * fix: prettify dialog * feat: score results * chore: update collab rev * feat: add recent view history to command palette * test: add integration_tests * fix: clippy changes * fix: focus traversal in cmd palette * fix: remove file after merging main * chore: code review and panic-safe * feat: index all views if index does not exist * chore: improve logic with conditional * chore: add is_empty check * chore: abstract logic from folder manager init * chore: update collab rev * chore: code review * chore: fixes after merge + update lock file * chore: revert cargo lock * fix: set icon type when removing icon * fix: code review + dependency inversion * fix: remove icon fix for not persisting icon type * test: simple tests manipulating views * test: create 100 views * fix: tauri build * chore: create 1000 views * chore: create util methods * chore: test * chore: test * chore: remove logs * chore: fix build.rs * chore: export models * chore: enable clear cache on Rust-CI * fix: navigate to newly created views * fix: force disable setting workspace listener on rebuilds * fix: remove late final * fix: missing returns * fix: localization and minor fixes * test: add index assert to large test * fix: missing section param after merging main * chore: try fix unzip file error * chore: lower the test * feat: show hint when result is in trash * feat: one index_writer per index * fix: minor changes after merge * fix: make create_log_filter public after merge * chore: fix test * chore: fix test * chore: flutter analyze * chore: flutter analyze * chore: fix tauri build --------- Co-authored-by: nathan <nathan@appflowy.io> Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io> Co-authored-by: Nathan.fooo <86001920+appflowy@users.noreply.github.com>
2024-04-12 10:21:41 +02:00
static_flowy_error!(
folder_index_manager_unavailable,
ErrorCode::FolderIndexManagerUnavailable
);
static_flowy_error!(workspace_data_not_match, ErrorCode::WorkspaceDataNotMatch);
static_flowy_error!(local_ai, ErrorCode::LocalAIError);
static_flowy_error!(local_ai_unavailable, ErrorCode::LocalAIUnavailable);
feat: ai billing (#5741) * feat: start on AI plan+billing UI * chore: enable plan and billing * feat: cache workspace subscription + minor fixes (#5705) * feat: update api from billing * feat: add api for workspace subscription info (#5717) * feat: refactor and start integrating AI plans * feat: refine UI and add business logic for AI * feat: complete UIUX for AI and limits * chore: remove resolved todo * chore: localize remove addon dialog * chore: fix spacing issue for usage * fix: interpret subscription + usage on action * chore: update api for billing (#5735) * chore: update revisions * fix: remove subscription cache * fix: copy improvements + use consistent dialog * chore: update to the latest client api * feat: support updating billing period * Feat/ai billing cancel reason (#5752) * chore: add cancellation reason field * fix: ci add one retry for concurrent sign up * chore: merge with main * chore: half merge * chore: fix conflict * chore: observer error * chore: remove unneeded protobuf and remove unwrap * feat: added subscription plan details * chore: check error code and update sidebar toast * chore: periodically check billing state * chore: editor ai error * chore: return file upload error * chore: fmt * chore: clippy * chore: disable upload image when exceed storage limitation * chore: remove todo * chore: remove openai i18n * chore: update log * chore: update client-api to fix stream error * chore: clippy * chore: fix language file * chore: disable billing UI --------- Co-authored-by: Zack Fu Zi Xiang <speed2exe@live.com.sg> Co-authored-by: nathan <nathan@appflowy.io>
2024-07-22 09:43:48 +02:00
static_flowy_error!(response_timeout, ErrorCode::ResponseTimeout);
static_flowy_error!(file_storage_limit, ErrorCode::FileStorageLimitExceeded);
static_flowy_error!(view_is_locked, ErrorCode::ViewIsLocked);
static_flowy_error!(local_ai_not_ready, ErrorCode::LocalAINotReady);
2025-03-30 15:15:59 +08:00
static_flowy_error!(local_ai_disabled, ErrorCode::LocalAIDisabled);
2021-12-14 15:31:44 +08:00
}
impl std::convert::From<ErrorCode> for FlowyError {
fn from(code: ErrorCode) -> Self {
let msg = format!("{}", code);
FlowyError {
code,
msg,
payload: vec![],
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().with_context(format!("{:?}", 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().with_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().with_context(e)
}
2021-12-14 18:04:51 +08:00
}
impl From<ValidationError> for FlowyError {
fn from(value: ValidationError) -> Self {
FlowyError::new(ErrorCode::InvalidParams, value)
}
}
impl From<ValidationErrors> for FlowyError {
fn from(value: ValidationErrors) -> Self {
FlowyError::new(ErrorCode::InvalidParams, value)
}
}
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))
}
}
2023-10-02 09:12:24 +02:00
impl From<fancy_regex::Error> for FlowyError {
fn from(e: fancy_regex::Error) -> Self {
FlowyError::internal().with_context(e)
}
}
impl From<JoinError> for FlowyError {
fn from(e: JoinError) -> Self {
FlowyError::internal().with_context(e)
}
}
impl From<tokio::sync::oneshot::error::RecvError> for FlowyError {
fn from(e: tokio::sync::oneshot::error::RecvError) -> Self {
FlowyError::internal().with_context(e)
}
}
feat: ai billing (#5741) * feat: start on AI plan+billing UI * chore: enable plan and billing * feat: cache workspace subscription + minor fixes (#5705) * feat: update api from billing * feat: add api for workspace subscription info (#5717) * feat: refactor and start integrating AI plans * feat: refine UI and add business logic for AI * feat: complete UIUX for AI and limits * chore: remove resolved todo * chore: localize remove addon dialog * chore: fix spacing issue for usage * fix: interpret subscription + usage on action * chore: update api for billing (#5735) * chore: update revisions * fix: remove subscription cache * fix: copy improvements + use consistent dialog * chore: update to the latest client api * feat: support updating billing period * Feat/ai billing cancel reason (#5752) * chore: add cancellation reason field * fix: ci add one retry for concurrent sign up * chore: merge with main * chore: half merge * chore: fix conflict * chore: observer error * chore: remove unneeded protobuf and remove unwrap * feat: added subscription plan details * chore: check error code and update sidebar toast * chore: periodically check billing state * chore: editor ai error * chore: return file upload error * chore: fmt * chore: clippy * chore: disable upload image when exceed storage limitation * chore: remove todo * chore: remove openai i18n * chore: update log * chore: update client-api to fix stream error * chore: clippy * chore: fix language file * chore: disable billing UI --------- Co-authored-by: Zack Fu Zi Xiang <speed2exe@live.com.sg> Co-authored-by: nathan <nathan@appflowy.io>
2024-07-22 09:43:48 +02:00
impl From<String> for FlowyError {
fn from(e: String) -> Self {
FlowyError::internal().with_context(e)
}
}
impl From<collab::error::CollabError> for FlowyError {
fn from(value: CollabError) -> Self {
match value {
CollabError::SerdeJson(err) => FlowyError::serde().with_context(err),
CollabError::UnexpectedEmpty(err) => FlowyError::payload_none().with_context(err),
CollabError::AcquiredWriteTxnFail => FlowyError::internal(),
CollabError::AcquiredReadTxnFail => FlowyError::internal(),
CollabError::YrsTransactionError(err) => FlowyError::internal().with_context(err),
CollabError::YrsEncodeStateError(err) => FlowyError::internal().with_context(err),
CollabError::UndoManagerNotEnabled => {
FlowyError::not_support().with_context("UndoManager is not enabled")
},
CollabError::DecodeUpdate(err) => FlowyError::internal().with_context(err),
CollabError::NoRequiredData(err) => FlowyError::internal().with_context(err),
CollabError::Awareness(err) => FlowyError::internal().with_context(err),
CollabError::UpdateFailed(err) => FlowyError::internal().with_context(err),
CollabError::Internal(err) => FlowyError::internal().with_context(err),
}
}
}
2025-04-07 19:24:58 +08:00
impl From<uuid::Error> for FlowyError {
fn from(value: uuid::Error) -> Self {
FlowyError::internal().with_context(value)
}
}