Skip to content

Commit ae11df5

Browse files
authored
Merge pull request #26 from yeoleobun/patch1
improve: improve error with this error
2 parents 82962ef + 1dbc4eb commit ae11df5

2 files changed

Lines changed: 42 additions & 72 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async-trait = "0.1.89"
2323
futures = "0.3.31"
2424
log = "0.4.27"
2525
rsip = { version = "0.4.0" }
26+
thiserror = "2.0.16"
2627
tracing = "0.1.41"
2728
wasm-bindgen = "0.2.84"
2829
# The `console_error_panic_hook` crate provides better debugging of panics by

‎src/error.rs‎

Lines changed: 41 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,64 @@
11
use crate::{dialog::DialogId, transaction::key::TransactionKey, transport::SipAddr};
22
use std::env::VarError;
3+
use thiserror::Error as ThisError;
34
use wasm_bindgen::prelude::*;
45

5-
#[derive(Debug, Eq, PartialEq, Clone)]
6+
#[derive(Debug, ThisError)]
67
pub enum Error {
7-
SipMessageError(String),
8+
#[error("SIP message error: {0}")]
9+
SipMessageError(#[from] rsip::Error),
10+
11+
#[error("DNS resolution error: {0}")]
812
DnsResolutionError(String),
13+
14+
#[error("Transport layer error: {0}: {1}")]
915
TransportLayerError(String, SipAddr),
16+
17+
#[error("Transaction error: {0}: {1}")]
1018
TransactionError(String, TransactionKey),
19+
20+
#[error("Endpoint error: {0}")]
1121
EndpointError(String),
22+
23+
#[error("Dialog error: {0}: {1}")]
1224
DialogError(String, DialogId),
13-
Error(String),
14-
}
1525

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),
3128

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),
5031

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),
5634

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),
6737

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),
7840

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),
8352
}
8453

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()
8857
}
8958
}
9059

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())
9463
}
9564
}

0 commit comments

Comments
 (0)