Feat/http server adapt (#1754)

This commit is contained in:
Nathan.fooo
2023-01-30 11:11:19 +08:00
committed by GitHub
parent 000569a836
commit 0d8adaa921
301 changed files with 2181 additions and 1768 deletions

View File

@ -7,26 +7,34 @@ edition = "2018"
[dependencies]
flowy-derive = { path = "../flowy-derive" }
lib-dispatch = { path = "../lib-dispatch" }
protobuf = {version = "2.20.0"}
bytes = "1.0"
anyhow = "1.0"
thiserror = "1.0"
flowy-sync = { path = "../flowy-sync", optional = true}
flowy-client-sync = { path = "../flowy-client-sync", optional = true}
lib-dispatch = { path = "../lib-dispatch", optional = true }
lib-ot = { path = "../../../shared-lib/lib-ot", optional = true}
user-model = { path = "../../../shared-lib/user-model", optional = true}
flowy-client-ws = { path = "../../../shared-lib/flowy-client-ws", optional = true }
serde_json = {version = "1.0", optional = true}
http-flowy = { git = "https://github.com/AppFlowy-IO/AppFlowy-Server", optional = true}
serde_repr = { version = "0.1" }
serde = "1.0"
reqwest = { version = "0.11", optional = true }
http-error-code = { git = "https://github.com/AppFlowy-IO/AppFlowy-Server", branch = "refactor/appflowy_server", optional = true }
flowy-database = { path = "../flowy-database", optional = true}
r2d2 = { version = "0.8", optional = true}
[features]
collaboration = ["flowy-sync"]
ot = ["lib-ot"]
serde = ["serde_json"]
http_server = ["http-flowy"]
db = ["flowy-database", "r2d2"]
adaptor_sync = ["flowy-client-sync"]
adaptor_ot = ["lib-ot"]
adaptor_dispatch = ["lib-dispatch"]
adaptor_serde = ["serde_json"]
adaptor_reqwest = ["reqwest"]
adaptor_database = ["flowy-database", "r2d2"]
adaptor_ws= ["flowy-client-ws"]
adaptor_user= ["user-model"]
adaptor_server_error = ["http-error-code"]
dart = ["flowy-codegen/dart"]
ts = ["flowy-codegen/ts"]

View File

@ -1,7 +1,9 @@
use flowy_derive::ProtoBuf_Enum;
use serde_repr::*;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error, ProtoBuf_Enum)]
#[derive(Debug, Clone, PartialEq, Eq, Error, Serialize_repr, Deserialize_repr, ProtoBuf_Enum)]
#[repr(u8)]
pub enum ErrorCode {
#[error("Internal error")]
Internal = 0,
@ -92,6 +94,7 @@ pub enum ErrorCode {
#[error("user id is empty or whitespace")]
UserIdInvalid = 30,
#[error("User not exist")]
UserNotExist = 31,
@ -160,6 +163,27 @@ pub enum ErrorCode {
#[error("Sort id is empty")]
SortIdIsEmpty = 53,
#[error("Connect refused")]
ConnectRefused = 54,
#[error("Connection timeout")]
ConnectTimeout = 55,
#[error("Connection closed")]
ConnectClose = 56,
#[error("Connection canceled")]
ConnectCancel = 57,
#[error("Sql error")]
SqlError = 58,
#[error("Http request error")]
HttpError = 59,
#[error("Payload should not be empty")]
UnexpectedEmptyPayload = 60,
}
impl ErrorCode {

View File

@ -1,9 +1,7 @@
use crate::ErrorCode;
use crate::code::ErrorCode;
use anyhow::Result;
use bytes::Bytes;
use flowy_derive::ProtoBuf;
use lib_dispatch::prelude::{AFPluginEventResponse, ResponseBuilder};
use std::{convert::TryInto, fmt::Debug};
use std::fmt::Debug;
use thiserror::Error;
pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
@ -39,6 +37,10 @@ impl FlowyError {
self
}
pub fn is_record_not_found(&self) -> bool {
self.code == ErrorCode::RecordNotFound.value()
}
static_flowy_error!(internal, ErrorCode::Internal);
static_flowy_error!(record_not_found, ErrorCode::RecordNotFound);
static_flowy_error!(workspace_name, ErrorCode::WorkspaceNameInvalid);
@ -72,6 +74,8 @@ impl FlowyError {
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::UnexpectedEmptyPayload);
static_flowy_error!(http, ErrorCode::HttpError);
}
impl std::convert::From<ErrorCode> for FlowyError {
@ -90,22 +94,6 @@ where
FlowyError::internal().context(e)
}
// Not needed because of thiserror derive macro
/* impl fmt::Display for FlowyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", &self.code, &self.msg)
}
}
*/
impl lib_dispatch::Error for FlowyError {
fn as_response(&self) -> AFPluginEventResponse {
let bytes: Bytes = self.clone().try_into().unwrap();
println!("Serialize FlowyError: {:?} to event response", self);
ResponseBuilder::Err().data(bytes).build()
}
}
impl std::convert::From<std::io::Error> for FlowyError {
fn from(error: std::io::Error) -> Self {
FlowyError::internal().context(error)
@ -117,5 +105,3 @@ impl std::convert::From<protobuf::ProtobufError> for FlowyError {
FlowyError::internal().context(e)
}
}
//impl std::error::Error for FlowyError {}

View File

@ -0,0 +1,12 @@
use crate::FlowyError;
use bytes::Bytes;
use lib_dispatch::prelude::{AFPluginEventResponse, ResponseBuilder};
use std::convert::TryInto;
impl lib_dispatch::Error for FlowyError {
fn as_response(&self) -> AFPluginEventResponse {
let bytes: Bytes = self.clone().try_into().unwrap();
println!("Serialize FlowyError: {:?} to event response", self);
ResponseBuilder::Err().data(bytes).build()
}
}

View File

@ -1,21 +1,16 @@
use crate::{ErrorCode, FlowyError};
use http_flowy::errors::{ErrorCode as ServerErrorCode, ServerError};
use crate::code::ErrorCode;
use http_error_code::ErrorCode as ServerErrorCode;
impl std::convert::From<ServerError> for FlowyError {
fn from(error: ServerError) -> Self {
let code = server_error_to_flowy_error(error.code);
FlowyError::new(code, &error.msg)
}
}
fn server_error_to_flowy_error(code: ServerErrorCode) -> ErrorCode {
match code {
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
ServerErrorCode::RecordNotFound => ErrorCode::RecordNotFound,
ServerErrorCode::ConnectRefused | ServerErrorCode::ConnectTimeout | ServerErrorCode::ConnectClose => {
ErrorCode::HttpServerConnectError
impl std::convert::From<ServerErrorCode> for ErrorCode {
fn from(code: ServerErrorCode) -> Self {
match code {
ServerErrorCode::UserUnauthorized => ErrorCode::UserUnauthorized,
ServerErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
ServerErrorCode::RecordNotFound => ErrorCode::RecordNotFound,
ServerErrorCode::ConnectRefused | ServerErrorCode::ConnectTimeout | ServerErrorCode::ConnectClose => {
ErrorCode::HttpServerConnectError
}
_ => ErrorCode::Internal,
}
_ => ErrorCode::Internal,
}
}

View File

@ -1,27 +1,26 @@
#[cfg(feature = "collaboration")]
mod collaborate;
#[cfg(feature = "collaboration")]
pub use collaborate::*;
#[cfg(feature = "adaptor_sync")]
pub mod sync;
//
#[cfg(feature = "ot")]
mod ot;
#[cfg(feature = "ot")]
pub use ot::*;
#[cfg(feature = "adaptor_ot")]
pub mod ot;
//
#[cfg(feature = "serde")]
mod serde;
#[cfg(feature = "serde")]
pub use serde::*;
#[cfg(feature = "adaptor_serde")]
pub mod serde;
//
#[cfg(feature = "http_server")]
mod http_server;
#[cfg(feature = "http_server")]
pub use http_server::*;
#[cfg(feature = "adaptor_dispatch")]
pub mod dispatch;
#[cfg(feature = "db")]
mod database;
#[cfg(feature = "db")]
pub use database::*;
#[cfg(feature = "adaptor_reqwest")]
pub mod reqwest;
#[cfg(feature = "adaptor_database")]
pub mod database;
#[cfg(feature = "adaptor_ws")]
pub mod ws;
#[cfg(feature = "adaptor_user")]
pub mod user;
#[cfg(feature = "adaptor_server_error")]
pub mod http_server;

View File

@ -0,0 +1,8 @@
use crate::FlowyError;
use reqwest::Error;
impl std::convert::From<reqwest::Error> for FlowyError {
fn from(error: Error) -> Self {
FlowyError::connection().context(error)
}
}

View File

@ -2,6 +2,6 @@ use crate::FlowyError;
impl std::convert::From<serde_json::Error> for FlowyError {
fn from(error: serde_json::Error) -> Self {
FlowyError::internal().context(error)
FlowyError::serde().context(error)
}
}

View File

@ -1,9 +1,9 @@
use crate::FlowyError;
use flowy_sync::errors::ErrorCode;
use flowy_client_sync::errors::ErrorCode;
impl std::convert::From<flowy_sync::errors::CollaborateError> for FlowyError {
fn from(error: flowy_sync::errors::CollaborateError) -> Self {
impl std::convert::From<flowy_client_sync::errors::SyncError> for FlowyError {
fn from(error: flowy_client_sync::errors::SyncError) -> Self {
match error.code {
ErrorCode::RecordNotFound => FlowyError::record_not_found().context(error.msg),
_ => FlowyError::internal().context(error.msg),

View File

@ -0,0 +1,23 @@
use crate::code::ErrorCode;
use user_model::errors::UserErrorCode;
impl std::convert::From<UserErrorCode> for ErrorCode {
fn from(code: UserErrorCode) -> Self {
match code {
UserErrorCode::Internal => ErrorCode::Internal,
UserErrorCode::WorkspaceIdInvalid => ErrorCode::WorkspaceIdInvalid,
UserErrorCode::EmailIsEmpty => ErrorCode::EmailIsEmpty,
UserErrorCode::EmailFormatInvalid => ErrorCode::EmailFormatInvalid,
UserErrorCode::UserIdInvalid => ErrorCode::UserIdInvalid,
UserErrorCode::UserNameContainForbiddenCharacters => ErrorCode::UserNameContainForbiddenCharacters,
UserErrorCode::UserNameIsEmpty => ErrorCode::UserNameIsEmpty,
UserErrorCode::UserNotExist => ErrorCode::UserNotExist,
UserErrorCode::PasswordIsEmpty => ErrorCode::PasswordIsEmpty,
UserErrorCode::PasswordTooLong => ErrorCode::PasswordTooLong,
UserErrorCode::PasswordContainsForbidCharacters => ErrorCode::PasswordContainsForbidCharacters,
UserErrorCode::PasswordFormatInvalid => ErrorCode::PasswordFormatInvalid,
UserErrorCode::PasswordNotMatch => ErrorCode::PasswordNotMatch,
UserErrorCode::UserNameTooLong => ErrorCode::UserNameTooLong,
}
}
}

View File

@ -0,0 +1,10 @@
use crate::FlowyError;
use flowy_client_ws::WSErrorCode;
impl std::convert::From<WSErrorCode> for FlowyError {
fn from(code: WSErrorCode) -> Self {
match code {
WSErrorCode::Internal => FlowyError::internal().context(code),
}
}
}

View File

@ -1,4 +1,4 @@
mod code;
pub mod code;
mod errors;
mod ext;
pub mod protobuf;