jsonrpc_ws_server/
error.rs

1use std::{error, fmt, io, result};
2
3use crate::ws;
4
5/// WebSockets Server Error
6#[derive(Debug)]
7pub enum Error {
8	/// Io Error
9	Io(io::Error),
10	/// WebSockets Error
11	WsError(ws::Error),
12	/// Connection Closed
13	ConnectionClosed,
14}
15
16/// WebSockets Server Result
17pub type Result<T> = result::Result<T, Error>;
18
19impl fmt::Display for Error {
20	fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
21		match self {
22			Error::ConnectionClosed => write!(f, "Action on closed connection."),
23			Error::WsError(err) => write!(f, "WebSockets Error: {}", err),
24			Error::Io(err) => write!(f, "Io Error: {}", err),
25		}
26	}
27}
28
29impl error::Error for Error {
30	fn source(&self) -> Option<&(dyn error::Error + 'static)> {
31		match self {
32			Error::Io(io) => Some(io),
33			Error::WsError(ws) => Some(ws),
34			Error::ConnectionClosed => None,
35		}
36	}
37}
38
39impl From<io::Error> for Error {
40	fn from(err: io::Error) -> Self {
41		Error::Io(err)
42	}
43}
44
45impl From<ws::Error> for Error {
46	fn from(err: ws::Error) -> Self {
47		match err.kind {
48			ws::ErrorKind::Io(err) => Error::Io(err),
49			_ => Error::WsError(err),
50		}
51	}
52}