misskey_websocket/
error.rs1use std::convert::Infallible;
2use std::sync::Arc;
3
4#[cfg(not(feature = "async-tungstenite09"))]
5use async_tungstenite::tungstenite;
6#[cfg(feature = "async-tungstenite09")]
7use async_tungstenite09::tungstenite;
8use thiserror::Error;
9
10#[derive(Debug, Error, Clone)]
12pub enum Error {
13 #[error("websocket error: {0}")]
15 WebSocket(#[source] Arc<tungstenite::Error>),
16 #[error("websocket unexpected message: {0}")]
18 UnexpectedMessage(tungstenite::Message),
19 #[error("JSON error: {0}")]
21 Json(#[source] Arc<serde_json::Error>),
22}
23
24impl From<Infallible> for Error {
25 fn from(x: Infallible) -> Error {
26 match x {}
27 }
28}
29
30impl From<url::ParseError> for Error {
31 fn from(_: url::ParseError) -> Error {
32 tungstenite::Error::Url("Failed to parse URL".into()).into()
33 }
34}
35
36impl From<tungstenite::Error> for Error {
37 fn from(err: tungstenite::Error) -> Error {
38 Error::WebSocket(Arc::new(err))
39 }
40}
41
42impl From<serde_json::Error> for Error {
43 fn from(err: serde_json::Error) -> Error {
44 Error::Json(Arc::new(err))
45 }
46}
47
48pub type Result<T> = std::result::Result<T, Error>;
50
51#[cfg(test)]
52mod tests {
53 use super::Error;
54
55 #[test]
56 fn test_send() {
57 fn assert_send<T: Send>() {}
58 assert_send::<Error>();
59 }
60
61 #[test]
62 fn test_sync() {
63 fn assert_send<T: Sync>() {}
64 assert_send::<Error>();
65 }
66}