Skip to content

Commit 2f89773

Browse files
committed
Remove thiserror dependency
1 parent 074a190 commit 2f89773

File tree

12 files changed

+175
-97
lines changed

12 files changed

+175
-97
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ tor = ["dep:arti-client", "dep:tor-hsservice", "dep:tor-hsrproxy", "dep:tor-rtco
1919
[dependencies]
2020
async-utility = "0.2"
2121
futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] }
22-
thiserror = "1.0"
2322
url = { version = "2.5", default-features = false }
2423

2524
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

src/native/error.rs

+51-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,73 @@
11
// Copyright (c) 2022-2024 Yuki Kishimoto
22
// Distributed under the MIT software license
33

4-
use thiserror::Error;
4+
use std::{fmt, io};
5+
56
use tokio_tungstenite::tungstenite::Error as WsError;
67
use url::ParseError;
78

89
#[cfg(feature = "tor")]
910
use super::tor;
1011

11-
#[derive(Debug, Error)]
12+
#[derive(Debug)]
1213
pub enum Error {
1314
/// I/O error
14-
#[error(transparent)]
15-
IO(#[from] std::io::Error),
15+
IO(io::Error),
1616
/// Ws error
17-
#[error(transparent)]
18-
Ws(#[from] WsError),
17+
Ws(WsError),
1918
/// Socks error
2019
#[cfg(feature = "socks")]
21-
#[error(transparent)]
22-
Socks(#[from] tokio_socks::Error),
20+
Socks(tokio_socks::Error),
2321
/// Tor error
2422
#[cfg(feature = "tor")]
25-
#[error(transparent)]
26-
Tor(#[from] tor::Error),
23+
Tor(tor::Error),
2724
/// Url parse error
28-
#[error(transparent)]
29-
Url(#[from] ParseError),
25+
Url(ParseError),
3026
/// Timeout
31-
#[error("timeout")]
3227
Timeout,
33-
/// Invalid DNS name
34-
#[error("invalid DNS name")]
35-
InvalidDNSName,
28+
}
29+
30+
impl std::error::Error for Error {}
31+
32+
impl fmt::Display for Error {
33+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34+
match self {
35+
Self::IO(e) => write!(f, "{e}"),
36+
Self::Ws(e) => write!(f, "{e}"),
37+
#[cfg(feature = "socks")]
38+
Self::Socks(e) => write!(f, "{e}"),
39+
#[cfg(feature = "tor")]
40+
Self::Tor(e) => write!(f, "{e}"),
41+
Self::Url(e) => write!(f, "{e}"),
42+
Self::Timeout => write!(f, "timeout"),
43+
}
44+
}
45+
}
46+
47+
impl From<io::Error> for Error {
48+
fn from(e: io::Error) -> Self {
49+
Self::IO(e)
50+
}
51+
}
52+
53+
impl From<WsError> for Error {
54+
fn from(e: WsError) -> Self {
55+
Self::Ws(e)
56+
}
57+
}
58+
59+
#[cfg(feature = "socks")]
60+
impl From<tokio_socks::Error> for Error {
61+
fn from(e: tokio_socks::Error) -> Self {
62+
Self::Socks(e)
63+
}
64+
}
65+
66+
#[cfg(feature = "tor")]
67+
impl From<tor::Error> for Error {
68+
fn from(e: tor::Error) -> Self {
69+
Self::Tor(e)
70+
}
3671
}
3772

3873
impl Error {

src/native/tor.rs

+43-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Tor
55
6+
use std::fmt;
67
use std::net::SocketAddr;
78
use std::path::PathBuf;
89
use std::sync::Arc;
@@ -11,7 +12,6 @@ use arti_client::config::onion_service::OnionServiceConfigBuilder;
1112
use arti_client::config::{CfgPath, ConfigBuildError, TorClientConfigBuilder};
1213
use arti_client::{DataStream, TorClient, TorClientConfig};
1314
use async_utility::thread;
14-
use thiserror::Error;
1515
use tokio::sync::OnceCell;
1616
use tor_hsrproxy::config::{
1717
Encapsulation, ProxyAction, ProxyConfigBuilder, ProxyConfigError, ProxyPattern, ProxyRule,
@@ -23,20 +23,53 @@ use tor_rtcompat::PreferredRuntime;
2323

2424
static TOR_CLIENT: OnceCell<TorClient<PreferredRuntime>> = OnceCell::const_new();
2525

26-
#[derive(Debug, Clone, Error)]
26+
#[derive(Debug, Clone)]
2727
pub enum Error {
2828
/// Arti Client error
29-
#[error(transparent)]
30-
ArtiClient(#[from] arti_client::Error),
29+
ArtiClient(arti_client::Error),
3130
/// Config builder error
32-
#[error(transparent)]
33-
ConfigBuilder(#[from] ConfigBuildError),
31+
ConfigBuilder(ConfigBuildError),
3432
/// Proxy config error
35-
#[error(transparent)]
36-
ProxyConfig(#[from] ProxyConfigError),
33+
ProxyConfig(ProxyConfigError),
3734
/// Invalid nickname
38-
#[error(transparent)]
39-
InvalidNickname(#[from] InvalidNickname),
35+
InvalidNickname(InvalidNickname),
36+
}
37+
38+
impl std::error::Error for Error {}
39+
40+
impl fmt::Display for Error {
41+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42+
match self {
43+
Self::ArtiClient(e) => write!(f, "{e}"),
44+
Self::ConfigBuilder(e) => write!(f, "{e}"),
45+
Self::ProxyConfig(e) => write!(f, "{e}"),
46+
Self::InvalidNickname(e) => write!(f, "{e}"),
47+
}
48+
}
49+
}
50+
51+
impl From<arti_client::Error> for Error {
52+
fn from(e: arti_client::Error) -> Self {
53+
Self::ArtiClient(e)
54+
}
55+
}
56+
57+
impl From<ConfigBuildError> for Error {
58+
fn from(e: ConfigBuildError) -> Self {
59+
Self::ConfigBuilder(e)
60+
}
61+
}
62+
63+
impl From<ProxyConfigError> for Error {
64+
fn from(e: ProxyConfigError) -> Self {
65+
Self::ProxyConfig(e)
66+
}
67+
}
68+
69+
impl From<InvalidNickname> for Error {
70+
fn from(e: InvalidNickname) -> Self {
71+
Self::InvalidNickname(e)
72+
}
4073
}
4174

4275
async fn init_tor_client(

src/wasm/error.rs

+51-28
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,108 @@
22
// Copyright (c) 2023-2024 Yuki Kishimoto
33
// Distributed under the MIT software license
44

5+
use core::fmt;
56
use core::str::Utf8Error;
67

7-
use thiserror::Error;
8-
98
use crate::wasm::CloseEvent;
109

1110
/// WebSocket Error
12-
#[derive(Debug, Clone, PartialEq, Eq, Error)]
13-
pub enum WsError {
11+
#[derive(Debug, Clone, PartialEq, Eq)]
12+
pub enum Error {
1413
/// UTF-8 error
15-
#[error(transparent)]
16-
Utf8(#[from] Utf8Error),
17-
14+
Utf8(Utf8Error),
1815
/// Invalid input to [WsState::try_from( u16 )](crate::WsState).
19-
#[error("Invalid input to conversion to WsReadyState: {supplied}")]
2016
InvalidWsState {
2117
/// The user supplied value that is invalid.
2218
supplied: u16,
2319
},
24-
2520
/// When trying to send and [WsState](crate::WsState) is anything but [WsState::Open](crate::WsState::Open) this error is returned.
26-
#[error("The connection state is not \"Open\".")]
2721
ConnectionNotOpen,
28-
2922
/// An invalid URL was given to [WsMeta::connect](crate::WsMeta::connect), please see:
3023
/// [HTML Living Standard](https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket).
31-
#[error("An invalid URL was given to the connect method: {supplied}")]
3224
InvalidUrl {
3325
/// The user supplied value that is invalid.
3426
supplied: String,
3527
},
36-
3728
/// An invalid close code was given to a close method. For valid close codes, please see:
3829
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes).
39-
#[error("An invalid close code was given to a close method: {supplied}")]
4030
InvalidCloseCode {
4131
/// The user supplied value that is invalid.
4232
supplied: u16,
4333
},
44-
4534
/// The reason string given to a close method is longer than 123 bytes, please see:
4635
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close).
47-
#[error("The reason string given to a close method is to long.")]
4836
ReasonStringToLong,
49-
5037
/// Failed to connect to the server.
51-
#[error("Failed to connect to the server. CloseEvent: {event:?}")]
5238
ConnectionFailed {
5339
/// The close event that might hold extra code and reason information.
5440
event: CloseEvent,
5541
},
56-
5742
/// When converting the JavaScript Message into a WsMessage, it's possible that
5843
/// a String message doesn't convert correctly as Js does not guarantee that
5944
/// strings are valid Unicode. Happens in `impl TryFrom< MessageEvent > for WsMessage`.
60-
#[error("Received a String message that couldn't be decoded to valid UTF-8")]
6145
InvalidEncoding,
62-
6346
/// When converting the JavaScript Message into a WsMessage, it's not possible to
6447
/// convert Blob type messages, as Blob is a streaming type, that needs to be read
6548
/// asynchronously. If you are using the type without setting up the connection with
6649
/// [`WsMeta::connect`](crate::WsMeta::connect), you have to make sure to set the binary
6750
/// type of the connection to `ArrayBuffer`.
6851
///
6952
/// Happens in `impl TryFrom< MessageEvent > for WsMessage`.
70-
#[error("Received a Blob message that couldn't converted.")]
7153
CantDecodeBlob,
72-
7354
/// When converting the JavaScript Message into a WsMessage, the data type was neither
7455
/// `Arraybuffer`, `String` nor `Blob`. This should never happen. If it does, please
7556
/// try to make a reproducible example and file an issue.
7657
///
7758
/// Happens in `impl TryFrom< MessageEvent > for WsMessage`.
78-
#[error("Received a message that is neither ArrayBuffer, String or Blob.")]
7959
UnknownDataType,
80-
81-
#[error("DOM Exception: {0}")]
8260
Dom(u16),
83-
84-
#[error("{0}")]
8561
Other(String),
62+
Timeout,
63+
}
64+
65+
impl std::error::Error for Error {}
66+
67+
impl fmt::Display for Error {
68+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69+
match self {
70+
Self::Utf8(e) => write!(f, "{e}"),
71+
Self::InvalidWsState { supplied } => {
72+
write!(f, "Invalid input to conversion to WsReadyState: {supplied}")
73+
}
74+
Self::ConnectionNotOpen => write!(f, "The connection state is not \"Open\"."),
75+
Self::InvalidUrl { supplied } => write!(
76+
f,
77+
"An invalid URL was given to the connect method: {supplied}"
78+
),
79+
Self::InvalidCloseCode { supplied } => write!(
80+
f,
81+
"An invalid close code was given to a close method: {supplied}"
82+
),
83+
Self::ReasonStringToLong => {
84+
write!(f, "The reason string given to a close method is to long.")
85+
}
86+
Self::ConnectionFailed { event } => {
87+
write!(f, "Failed to connect to the server. CloseEvent: {event:?}")
88+
}
89+
Self::InvalidEncoding => write!(
90+
f,
91+
"Received a String message that couldn't be decoded to valid UTF-8"
92+
),
93+
Self::CantDecodeBlob => write!(f, "Received a Blob message that couldn't converted."),
94+
Self::UnknownDataType => write!(
95+
f,
96+
"Received a message that is neither ArrayBuffer, String or Blob."
97+
),
98+
Self::Dom(code) => write!(f, "DOM Exception: {code}"),
99+
Self::Other(e) => write!(f, "{e}"),
100+
Self::Timeout => write!(f, "timeout"),
101+
}
102+
}
103+
}
104+
105+
impl From<Utf8Error> for Error {
106+
fn from(e: Utf8Error) -> Self {
107+
Self::Utf8(e)
108+
}
86109
}

src/wasm/event.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use web_sys::CloseEvent as JsCloseEvt;
66

7-
use crate::wasm::WsError;
7+
use crate::wasm::Error;
88

99
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub enum WsEvent {
@@ -24,7 +24,7 @@ pub enum WsEvent {
2424
/// An error happened, not on the connection, but inside _ws_stream_wasm_. This currently happens
2525
/// when an incoming message can not be converted to Rust types, eg. a String message with invalid
2626
/// encoding.
27-
WsErr(WsError),
27+
WsErr(Error),
2828
}
2929

3030
impl WsEvent {

src/wasm/message.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use js_sys::{ArrayBuffer, Uint8Array};
88
use wasm_bindgen::JsCast;
99
use web_sys::{Blob, MessageEvent};
1010

11-
use crate::wasm::WsError;
11+
use crate::wasm::Error;
1212

1313
/// Represents a WebSocket Message, after converting from JavaScript type.
1414
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -46,7 +46,7 @@ impl WsMessage {
4646

4747
/// Attempt to get a &str from the WebSocket message,
4848
/// this will try to convert binary data to utf8.
49-
pub fn to_text(&self) -> Result<&str, WsError> {
49+
pub fn to_text(&self) -> Result<&str, Error> {
5050
match self {
5151
Self::Text(string) => Ok(string),
5252
Self::Binary(data) => Ok(str::from_utf8(data)?),
@@ -58,7 +58,7 @@ impl WsMessage {
5858
/// will only work if the connection is set to use the binary type ArrayBuffer.
5959
/// On binary type Blob, this will panic.
6060
impl TryFrom<MessageEvent> for WsMessage {
61-
type Error = WsError;
61+
type Error = Error;
6262

6363
fn try_from(evt: MessageEvent) -> Result<Self, Self::Error> {
6464
match evt.data() {
@@ -75,15 +75,15 @@ impl TryFrom<MessageEvent> for WsMessage {
7575
// message.
7676
d if d.is_string() => match d.as_string() {
7777
Some(text) => Ok(WsMessage::Text(text)),
78-
None => Err(WsError::InvalidEncoding),
78+
None => Err(Error::InvalidEncoding),
7979
},
8080

8181
// We have set the binary mode to array buffer (WsMeta::connect), so normally this shouldn't happen.
8282
// That is as long as this is used within the context of the WsMeta constructor.
83-
d if d.is_instance_of::<Blob>() => Err(WsError::CantDecodeBlob),
83+
d if d.is_instance_of::<Blob>() => Err(Error::CantDecodeBlob),
8484

8585
// should never happen.
86-
_ => Err(WsError::UnknownDataType),
86+
_ => Err(Error::UnknownDataType),
8787
}
8888
}
8989
}

0 commit comments

Comments
 (0)