@@ -8,12 +8,43 @@ use alloy::{
8
8
} ,
9
9
} ;
10
10
11
+ /// Errors when connecting a provider
12
+ #[ derive( Debug , thiserror:: Error , Clone , PartialEq , Eq ) ]
13
+ pub enum ProviderConnectError {
14
+ /// Pubsub is not available for the configured transport
15
+ #[ error( "pubsub is not available for the configured transport" ) ]
16
+ PubsubUnavailable ,
17
+ /// Custom error message
18
+ #[ error( "{0}" ) ]
19
+ Custom ( String ) ,
20
+ }
21
+
22
+ impl From < TransportErrorKind > for ProviderConnectError {
23
+ fn from ( err : TransportErrorKind ) -> Self {
24
+ match err {
25
+ TransportErrorKind :: Custom ( err) => ProviderConnectError :: Custom ( err. to_string ( ) ) ,
26
+ TransportErrorKind :: PubsubUnavailable => ProviderConnectError :: PubsubUnavailable ,
27
+ _ => panic ! ( "Unexpected TransportErrorKind variant: {err:?}" ) ,
28
+ }
29
+ }
30
+ }
31
+
32
+ impl From < TransportError > for ProviderConnectError {
33
+ fn from ( err : TransportError ) -> Self {
34
+ match err {
35
+ TransportError :: Transport ( e) => e. into ( ) ,
36
+ _ => panic ! ( "Unexpected TransportError variant: {err:?}" ) ,
37
+ }
38
+ }
39
+ }
40
+
11
41
impl FromEnvVar for BuiltInConnectionString {
12
- type Error = TransportError ;
42
+ type Error = ProviderConnectError ;
13
43
14
44
fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
15
45
let conn_str = String :: from_env_var ( env_var) . map_err ( FromEnvErr :: infallible_into) ?;
16
- conn_str. parse ( ) . map_err ( Into :: into)
46
+ let built_in = conn_str. parse ( ) . map_err ( ProviderConnectError :: from) ?;
47
+ Ok ( built_in)
17
48
}
18
49
}
19
50
@@ -41,7 +72,7 @@ impl ProviderConfig {
41
72
}
42
73
43
74
impl FromEnvVar for ProviderConfig {
44
- type Error = TransportError ;
75
+ type Error = ProviderConnectError ;
45
76
46
77
fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
47
78
let connection_string = BuiltInConnectionString :: from_env_var ( env_var) ?;
@@ -81,21 +112,21 @@ impl PubSubConfig {
81
112
}
82
113
83
114
impl TryFrom < BuiltInConnectionString > for PubSubConfig {
84
- type Error = TransportError ;
115
+ type Error = ProviderConnectError ;
85
116
86
117
fn try_from ( connection_string : BuiltInConnectionString ) -> Result < Self , Self :: Error > {
87
118
if !matches ! (
88
119
connection_string,
89
120
BuiltInConnectionString :: Ws ( _, _) | BuiltInConnectionString :: Ipc ( _)
90
121
) {
91
- return Err ( TransportErrorKind :: pubsub_unavailable ( ) ) ;
122
+ return Err ( ProviderConnectError :: PubsubUnavailable ) ;
92
123
}
93
124
Ok ( Self { connection_string } )
94
125
}
95
126
}
96
127
97
128
impl FromEnvVar for PubSubConfig {
98
- type Error = TransportError ;
129
+ type Error = ProviderConnectError ;
99
130
100
131
fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
101
132
let cs = BuiltInConnectionString :: from_env_var ( env_var) ?;
@@ -137,3 +168,18 @@ impl PubSubConnect for PubSubConfig {
137
168
}
138
169
}
139
170
}
171
+
172
+ #[ cfg( test) ]
173
+ mod test {
174
+ use super :: * ;
175
+ use crate :: utils:: from_env:: FromEnv ;
176
+
177
+ #[ derive( FromEnv , Debug , Clone , PartialEq , Eq ) ]
178
+ #[ from_env( crate ) ]
179
+ struct CompileCheck {
180
+ #[ from_env( var = "COOL_DUDE" , desc = "provider" ) ]
181
+ cool_dude : ProviderConfig ,
182
+ #[ from_env( var = "COOL_DUDE2" , desc = "provider2" ) ]
183
+ cool_dude2 : PubSubConfig ,
184
+ }
185
+ }
0 commit comments