|
1 | 1 | use crate::{dialog::DialogId, transaction::key::TransactionKey, transport::SipAddr}; |
2 | 2 | use std::env::VarError; |
| 3 | +use thiserror::Error as ThisError; |
3 | 4 | use wasm_bindgen::prelude::*; |
4 | 5 |
|
5 | | -#[derive(Debug, Eq, PartialEq, Clone)] |
| 6 | +#[derive(Debug, ThisError)] |
6 | 7 | pub enum Error { |
7 | | - SipMessageError(String), |
| 8 | + #[error("SIP message error: {0}")] |
| 9 | + SipMessageError(#[from] rsip::Error), |
| 10 | + |
| 11 | + #[error("DNS resolution error: {0}")] |
8 | 12 | DnsResolutionError(String), |
| 13 | + |
| 14 | + #[error("Transport layer error: {0}: {1}")] |
9 | 15 | TransportLayerError(String, SipAddr), |
| 16 | + |
| 17 | + #[error("Transaction error: {0}: {1}")] |
10 | 18 | TransactionError(String, TransactionKey), |
| 19 | + |
| 20 | + #[error("Endpoint error: {0}")] |
11 | 21 | EndpointError(String), |
| 22 | + |
| 23 | + #[error("Dialog error: {0}: {1}")] |
12 | 24 | DialogError(String, DialogId), |
13 | | - Error(String), |
14 | | -} |
15 | 25 |
|
16 | | -impl std::fmt::Display for Error { |
17 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
18 | | - match self { |
19 | | - Error::SipMessageError(e) => write!(f, "SIP message error: {}", e), |
20 | | - Error::DnsResolutionError(e) => write!(f, "DNS resolution error: {}", e), |
21 | | - Error::TransportLayerError(e, addr) => { |
22 | | - write!(f, "Transport layer error: {}: {}", e, addr) |
23 | | - } |
24 | | - Error::TransactionError(e, key) => write!(f, "Transaction error: {}: {}", e, key), |
25 | | - Error::EndpointError(e) => write!(f, "Endpoint error: {}", e), |
26 | | - Error::DialogError(e, id) => write!(f, "Dialog error: {}: {}", e, id), |
27 | | - Error::Error(e) => write!(f, "Error: {}", e), |
28 | | - } |
29 | | - } |
30 | | -} |
| 26 | + #[error("I/O error: {0}")] |
| 27 | + IoError(#[from] std::io::Error), |
31 | 28 |
|
32 | | -impl Into<JsValue> for Error { |
33 | | - fn into(self) -> JsValue { |
34 | | - match self { |
35 | | - Error::DnsResolutionError(e) => e.into(), |
36 | | - Error::SipMessageError(e) => e.into(), |
37 | | - Error::TransportLayerError(e, _) => e.into(), |
38 | | - Error::TransactionError(e, key) => format!("{}: {}", e, key.to_string()).into(), |
39 | | - Error::EndpointError(e) => e.into(), |
40 | | - Error::DialogError(e, id) => format!("{}: {}", e, id.to_string()).into(), |
41 | | - Error::Error(e) => e.into(), |
42 | | - } |
43 | | - } |
44 | | -} |
45 | | -impl From<rsip::Error> for Error { |
46 | | - fn from(e: rsip::Error) -> Self { |
47 | | - Error::SipMessageError(e.to_string()) |
48 | | - } |
49 | | -} |
| 29 | + #[error("Format error: {0}")] |
| 30 | + FormatError(#[from] std::fmt::Error), |
50 | 31 |
|
51 | | -impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error { |
52 | | - fn from(e: tokio::sync::mpsc::error::SendError<T>) -> Self { |
53 | | - Error::Error(e.to_string()) |
54 | | - } |
55 | | -} |
| 32 | + #[error("Environment variable error: {0}")] |
| 33 | + VarError(#[from] VarError), |
56 | 34 |
|
57 | | -impl From<tokio::sync::broadcast::error::RecvError> for Error { |
58 | | - fn from(e: tokio::sync::broadcast::error::RecvError) -> Self { |
59 | | - Error::Error(e.to_string()) |
60 | | - } |
61 | | -} |
62 | | -impl From<std::io::Error> for Error { |
63 | | - fn from(e: std::io::Error) -> Self { |
64 | | - Error::Error(e.to_string()) |
65 | | - } |
66 | | -} |
| 35 | + #[error("Address parse error: {0}")] |
| 36 | + AddrParseError(#[from] std::net::AddrParseError), |
67 | 37 |
|
68 | | -impl From<std::fmt::Error> for Error { |
69 | | - fn from(e: std::fmt::Error) -> Self { |
70 | | - Error::Error(e.to_string()) |
71 | | - } |
72 | | -} |
73 | | -impl From<VarError> for Error { |
74 | | - fn from(e: VarError) -> Self { |
75 | | - Error::Error(e.to_string()) |
76 | | - } |
77 | | -} |
| 38 | + #[error("TLS error: {0}")] |
| 39 | + TlsError(#[from] tokio_rustls::rustls::Error), |
78 | 40 |
|
79 | | -impl From<std::net::AddrParseError> for Error { |
80 | | - fn from(e: std::net::AddrParseError) -> Self { |
81 | | - Error::Error(e.to_string()) |
82 | | - } |
| 41 | + #[error("WebSocket error: {0}")] |
| 42 | + WebSocketError(#[from] tokio_tungstenite::tungstenite::Error), |
| 43 | + |
| 44 | + #[error("Channel send error: {0}")] |
| 45 | + ChannelSendError(String), |
| 46 | + |
| 47 | + #[error("Broadcast receive error: {0}")] |
| 48 | + BroadcastRecvError(#[from] tokio::sync::broadcast::error::RecvError), |
| 49 | + |
| 50 | + #[error("Error: {0}")] |
| 51 | + Error(String), |
83 | 52 | } |
84 | 53 |
|
85 | | -impl From<tokio_rustls::rustls::Error> for Error { |
86 | | - fn from(e: tokio_rustls::rustls::Error) -> Self { |
87 | | - Error::Error(e.to_string()) |
| 54 | +impl From<Error> for JsValue { |
| 55 | + fn from(e: Error) -> Self { |
| 56 | + e.to_string().into() |
88 | 57 | } |
89 | 58 | } |
90 | 59 |
|
91 | | -impl From<tokio_tungstenite::tungstenite::Error> for Error { |
92 | | - fn from(e: tokio_tungstenite::tungstenite::Error) -> Self { |
93 | | - Error::Error(e.to_string()) |
| 60 | +impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error { |
| 61 | + fn from(e: tokio::sync::mpsc::error::SendError<T>) -> Self { |
| 62 | + Error::ChannelSendError(e.to_string()) |
94 | 63 | } |
95 | 64 | } |
0 commit comments