106 lines
2.5 KiB
Rust
Raw Normal View History

2021-09-16 18:31:25 +08:00
use futures_channel::mpsc::TrySendError;
use serde::{Deserialize, Serialize};
use serde_repr::*;
2021-09-16 18:31:25 +08:00
use std::fmt::Debug;
use strum_macros::Display;
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, Serialize, Deserialize)]
2021-12-16 22:24:05 +08:00
pub struct WSError {
pub code: ErrorCode,
pub msg: String,
2021-09-16 18:31:25 +08:00
}
2021-12-16 22:24:05 +08:00
macro_rules! static_ws_error {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
pub fn $name() -> WSError {
WSError {
code: $status,
msg: format!("{}", $status),
}
}
};
2021-09-16 18:31:25 +08:00
}
2021-12-16 22:24:05 +08:00
impl WSError {
#[allow(dead_code)]
pub(crate) fn new(code: ErrorCode) -> WSError {
WSError {
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-09-16 18:31:25 +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
}
pub(crate) fn internal_error<T>(e: T) -> WSError
2021-10-05 17:54:11 +08:00
where
T: std::fmt::Debug,
2021-10-05 17:54:11 +08:00
{
WSError::internal().context(e)
2021-10-05 17:54:11 +08:00
}
#[derive(Debug, Clone, Serialize_repr, Deserialize_repr, Display, PartialEq, Eq)]
#[repr(u8)]
2021-09-16 18:31:25 +08:00
pub enum ErrorCode {
InternalError = 0,
UnsupportedMessage = 1,
Unauthorized = 2,
2021-09-16 18:31:25 +08:00
}
impl std::default::Default for ErrorCode {
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 {
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 {
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 {
fn from(error: TrySendError<Message>) -> Self {
WSError::internal().context(error)
}
2021-09-16 18:31:25 +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 {
fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
match error {
tokio_tungstenite::tungstenite::Error::Http(response) => {
if response.status() == StatusCode::UNAUTHORIZED {
WSError::unauthorized()
} else {
WSError::internal().context(response)
2021-11-27 19:19:41 +08:00
}
},
_ => WSError::internal().context(error),
2021-09-19 18:39:56 +08:00
}
}
}