AppFlowy/rust-lib/flowy-net/src/errors.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

2021-08-23 00:27:29 +00:00
use bytes::Bytes;
use serde::{Deserialize, Serialize, __private::Formatter};
use serde_repr::*;
use std::{fmt, fmt::Debug};
use crate::response::FlowyResponse;
#[derive(thiserror::Error, Debug, Serialize, Deserialize, Clone)]
pub struct ServerError {
pub code: Code,
pub msg: String,
}
macro_rules! static_error {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
2021-08-23 10:39:10 +00:00
pub fn $name() -> ServerError {
ServerError {
code: $status,
msg: format!("{}", $status),
}
2021-08-23 00:27:29 +00:00
}
};
}
impl ServerError {
static_error!(internal, Code::InternalError);
static_error!(http, Code::HttpError);
static_error!(payload_none, Code::PayloadUnexpectedNone);
2021-08-23 10:39:10 +00:00
static_error!(unauthorized, Code::Unauthorized);
static_error!(passwordNotMatch, Code::PasswordNotMatch);
pub fn with_msg<T: Debug>(mut self, error: T) -> Self {
self.msg = format!("{:?}", error);
self
}
2021-08-23 00:27:29 +00:00
}
impl std::fmt::Display for ServerError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let msg = format!("{:?}:{}", self.code, self.msg);
f.write_str(&msg)
}
}
impl std::convert::From<&ServerError> for FlowyResponse {
fn from(error: &ServerError) -> Self {
FlowyResponse {
data: Bytes::from(vec![]),
error: Some(error.clone()),
}
}
}
2021-08-23 10:39:10 +00:00
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug, Clone, derive_more::Display)]
2021-08-23 00:27:29 +00:00
#[repr(u16)]
pub enum Code {
2021-08-23 10:39:10 +00:00
#[display(fmt = "Token is invalid")]
2021-08-23 00:27:29 +00:00
InvalidToken = 1,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Unauthorized")]
Unauthorized = 2,
#[display(fmt = "Payload too large")]
PayloadOverflow = 3,
#[display(fmt = "Payload deserialize failed")]
PayloadSerdeFail = 4,
#[display(fmt = "Unexpected empty payload")]
PayloadUnexpectedNone = 5,
2021-08-23 00:27:29 +00:00
2021-08-23 10:39:10 +00:00
#[display(fmt = "Protobuf serde error")]
2021-08-23 00:27:29 +00:00
ProtobufError = 10,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Json serde Error")]
2021-08-23 00:27:29 +00:00
SerdeError = 11,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Email address already exists")]
2021-08-23 00:27:29 +00:00
EmailAlreadyExists = 50,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Username and password do not match")]
PasswordNotMatch = 51,
#[display(fmt = "Connect refused")]
2021-08-23 00:27:29 +00:00
ConnectRefused = 100,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Connection timeout")]
2021-08-23 00:27:29 +00:00
ConnectTimeout = 101,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Connection closed")]
2021-08-23 00:27:29 +00:00
ConnectClose = 102,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Connection canceled")]
2021-08-23 00:27:29 +00:00
ConnectCancel = 103,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Sql error")]
2021-08-23 00:27:29 +00:00
SqlError = 200,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Http request error")]
2021-08-23 00:27:29 +00:00
HttpError = 300,
2021-08-23 10:39:10 +00:00
#[display(fmt = "Internal error")]
2021-08-23 00:27:29 +00:00
InternalError = 1000,
}