2021-09-16 18:31:25 +08:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
|
|
|
use futures_channel::mpsc::TrySendError;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use strum_macros::Display;
|
2022-02-07 10:37:01 +08:00
|
|
|
use tokio::sync::oneshot::error::RecvError;
|
2021-09-19 18:39:56 +08:00
|
|
|
use tokio_tungstenite::tungstenite::{http::StatusCode, Message};
|
2021-09-16 18:31:25 +08:00
|
|
|
use url::ParseError;
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, ProtoBuf)]
|
2021-12-16 22:24:05 +08:00
|
|
|
pub struct WSError {
|
2021-09-16 18:31:25 +08:00
|
|
|
#[pb(index = 1)]
|
2021-09-23 15:49:10 +08:00
|
|
|
pub code: ErrorCode,
|
2021-09-16 18:31:25 +08:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
2021-09-23 15:49:10 +08:00
|
|
|
pub msg: String,
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
macro_rules! static_ws_error {
|
2021-09-16 18:31:25 +08:00
|
|
|
($name:ident, $status:expr) => {
|
|
|
|
#[allow(non_snake_case, missing_docs)]
|
2021-12-16 22:24:05 +08:00
|
|
|
pub fn $name() -> WSError {
|
|
|
|
WSError {
|
2021-09-16 18:31:25 +08:00
|
|
|
code: $status,
|
|
|
|
msg: format!("{}", $status),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
impl WSError {
|
2021-09-17 19:03:46 +08:00
|
|
|
#[allow(dead_code)]
|
2021-12-16 22:24:05 +08:00
|
|
|
pub(crate) fn new(code: ErrorCode) -> WSError {
|
|
|
|
WSError {
|
2021-10-01 19:39:08 +08:00
|
|
|
code,
|
|
|
|
msg: "".to_string(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 18:31:25 +08:00
|
|
|
|
|
|
|
pub fn context<T: Debug>(mut self, error: T) -> Self {
|
|
|
|
self.msg = format!("{:?}", error);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
static_ws_error!(internal, ErrorCode::InternalError);
|
|
|
|
static_ws_error!(unsupported_message, ErrorCode::UnsupportedMessage);
|
|
|
|
static_ws_error!(unauthorized, ErrorCode::Unauthorized);
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-23 22:52:38 +08:00
|
|
|
pub(crate) fn internal_error<T>(e: T) -> WSError
|
2021-10-05 17:54:11 +08:00
|
|
|
where
|
|
|
|
T: std::fmt::Debug,
|
|
|
|
{
|
2021-12-16 22:24:05 +08:00
|
|
|
WSError::internal().context(e)
|
2021-10-05 17:54:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:31:25 +08:00
|
|
|
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
|
|
|
|
pub enum ErrorCode {
|
2022-01-23 12:14:00 +08:00
|
|
|
InternalError = 0,
|
2021-09-20 15:38:55 +08:00
|
|
|
UnsupportedMessage = 1,
|
2022-01-23 12:14:00 +08:00
|
|
|
Unauthorized = 2,
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::default::Default for ErrorCode {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn default() -> Self {
|
|
|
|
ErrorCode::InternalError
|
|
|
|
}
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
impl std::convert::From<url::ParseError> for WSError {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(error: ParseError) -> Self {
|
|
|
|
WSError::internal().context(error)
|
|
|
|
}
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
impl std::convert::From<protobuf::ProtobufError> for WSError {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(error: protobuf::ProtobufError) -> Self {
|
|
|
|
WSError::internal().context(error)
|
|
|
|
}
|
2021-09-18 22:32:00 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
impl std::convert::From<futures_channel::mpsc::TrySendError<Message>> for WSError {
|
2022-01-23 12:14:00 +08:00
|
|
|
fn from(error: TrySendError<Message>) -> Self {
|
|
|
|
WSError::internal().context(error)
|
|
|
|
}
|
2021-09-16 18:31:25 +08:00
|
|
|
}
|
2021-09-19 12:54:28 +08:00
|
|
|
|
2022-02-07 10:37:01 +08:00
|
|
|
impl std::convert::From<RecvError> for WSError {
|
|
|
|
fn from(error: RecvError) -> Self {
|
|
|
|
WSError::internal().context(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:24:05 +08:00
|
|
|
impl std::convert::From<tokio_tungstenite::tungstenite::Error> for WSError {
|
2021-09-19 18:39:56 +08:00
|
|
|
fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
|
2021-11-27 19:19:41 +08:00
|
|
|
match error {
|
2021-09-19 18:39:56 +08:00
|
|
|
tokio_tungstenite::tungstenite::Error::Http(response) => {
|
|
|
|
if response.status() == StatusCode::UNAUTHORIZED {
|
2021-12-16 22:24:05 +08:00
|
|
|
WSError::unauthorized()
|
2021-09-19 18:39:56 +08:00
|
|
|
} else {
|
2021-12-16 22:24:05 +08:00
|
|
|
WSError::internal().context(response)
|
2021-09-19 18:39:56 +08:00
|
|
|
}
|
2022-01-23 12:14:00 +08:00
|
|
|
}
|
2021-12-16 22:24:05 +08:00
|
|
|
_ => WSError::internal().context(error),
|
2021-11-27 19:19:41 +08:00
|
|
|
}
|
2021-09-19 18:39:56 +08:00
|
|
|
}
|
2021-09-19 12:54:28 +08:00
|
|
|
}
|