Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 112ba07

Browse files
committed
make the errors more generic
1 parent 1b824e0 commit 112ba07

File tree

3 files changed

+92
-50
lines changed

3 files changed

+92
-50
lines changed

Diff for: ethers-providers/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub use provider::{Provider, ProviderError};
127127
#[async_trait]
128128
/// Trait which must be implemented by data transports to be used with the Ethereum
129129
/// JSON-RPC provider.
130-
pub trait JsonRpcClient: Send + Sync {
130+
pub trait JsonRpcClient: Send + Sync + Debug {
131131
/// A JSON-RPC Error
132132
type Error: Error + Into<ProviderError>;
133133

@@ -141,8 +141,8 @@ pub trait JsonRpcClient: Send + Sync {
141141
use ethers_core::types::*;
142142

143143
#[async_trait]
144-
pub trait Middleware: Sync + Send {
145-
type Error;
144+
pub trait Middleware: Sync + Send + Debug {
145+
type Error: Debug;
146146

147147
async fn send_transaction(
148148
&self,
@@ -154,40 +154,40 @@ pub trait Middleware: Sync + Send {
154154

155155
async fn lookup_address(&self, address: Address) -> Result<String, Self::Error>;
156156

157-
async fn get_block_number(&self) -> Result<U64, ProviderError>;
157+
async fn get_block_number(&self) -> Result<U64, Self::Error>;
158158

159159
async fn get_block<T: Into<BlockId> + Send + Sync>(
160160
&self,
161161
block_hash_or_number: T,
162-
) -> Result<Option<Block<TxHash>>, ProviderError>;
162+
) -> Result<Option<Block<TxHash>>, Self::Error>;
163163

164164
async fn get_block_with_txs<T: Into<BlockId> + Send + Sync>(
165165
&self,
166166
block_hash_or_number: T,
167-
) -> Result<Option<Block<Transaction>>, ProviderError>;
167+
) -> Result<Option<Block<Transaction>>, Self::Error>;
168168

169169
async fn get_transaction_count<T: Into<NameOrAddress> + Send + Sync>(
170170
&self,
171171
from: T,
172172
block: Option<BlockNumber>,
173-
) -> Result<U256, ProviderError>;
173+
) -> Result<U256, Self::Error>;
174174

175175
async fn call(
176176
&self,
177177
tx: &TransactionRequest,
178178
block: Option<BlockNumber>,
179-
) -> Result<Bytes, ProviderError>;
179+
) -> Result<Bytes, Self::Error>;
180180

181181
async fn get_code<T: Into<NameOrAddress> + Send + Sync>(
182182
&self,
183183
at: T,
184184
block: Option<BlockNumber>,
185-
) -> Result<Bytes, ProviderError>;
185+
) -> Result<Bytes, Self::Error>;
186186

187187
async fn get_storage_at<T: Into<NameOrAddress> + Send + Sync>(
188188
&self,
189189
from: T,
190190
location: H256,
191191
block: Option<BlockNumber>,
192-
) -> Result<H256, ProviderError>;
192+
) -> Result<H256, Self::Error>;
193193
}

Diff for: ethers-providers/src/provider.rs

+76-40
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,30 @@ pub struct Provider<P>(P, Option<Duration>);
4747
#[derive(Clone, Debug)]
4848
pub struct EnsProvider<M>(M, Option<Address>);
4949

50+
#[derive(Debug, Error)]
51+
pub enum EnsError<M: Middleware> {
52+
#[error("MiddlewareError: {0:?}")]
53+
MiddlewareError(M::Error),
54+
#[error(transparent)]
55+
ProviderError(#[from] ProviderError),
56+
}
57+
5058
// The ENS Middleware overrides functions involving from/to and forwards everything
5159
// else to the inner layer
5260
#[async_trait]
53-
impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
54-
type Error = ProviderError;
61+
impl<M: Middleware> Middleware for EnsProvider<M> {
62+
type Error = EnsError<M>;
5563

5664
/// Returns the address that the `ens_name` resolves to (or None if not configured).
5765
///
5866
/// # Panics
5967
///
6068
/// If the bytes returned from the ENS registrar/resolver cannot be interpreted as
6169
/// an address. This should theoretically never happen.
62-
async fn resolve_name(&self, ens_name: &str) -> Result<Address, ProviderError> {
63-
self.query_resolver(ParamType::Address, ens_name, ens::ADDR_SELECTOR)
64-
.await
70+
async fn resolve_name(&self, ens_name: &str) -> Result<Address, Self::Error> {
71+
Ok(self
72+
.query_resolver(ParamType::Address, ens_name, ens::ADDR_SELECTOR)
73+
.await?)
6574
}
6675

6776
////// Ethereum Naming Service
@@ -76,10 +85,11 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
7685
///
7786
/// If the bytes returned from the ENS registrar/resolver cannot be interpreted as
7887
/// a string. This should theoretically never happen.
79-
async fn lookup_address(&self, address: Address) -> Result<String, ProviderError> {
88+
async fn lookup_address(&self, address: Address) -> Result<String, Self::Error> {
8089
let ens_name = ens::reverse_address(address);
81-
self.query_resolver(ParamType::String, &ens_name, ens::NAME_SELECTOR)
82-
.await
90+
Ok(self
91+
.query_resolver(ParamType::String, &ens_name, ens::NAME_SELECTOR)
92+
.await?)
8393
}
8494

8595
/// Sends the transaction to the entire Ethereum network and returns the transaction's hash
@@ -88,7 +98,7 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
8898
&self,
8999
mut tx: TransactionRequest,
90100
block: Option<BlockNumber>,
91-
) -> Result<TxHash, ProviderError> {
101+
) -> Result<TxHash, Self::Error> {
92102
if let Some(ref to) = tx.to {
93103
if let NameOrAddress::Name(ens_name) = to {
94104
// resolve to an address
@@ -99,20 +109,26 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
99109
}
100110
}
101111

102-
self.0.send_transaction(tx, block).await
112+
self.0
113+
.send_transaction(tx, block)
114+
.await
115+
.map_err(EnsError::MiddlewareError)
103116
}
104117

105118
/// Returns the nonce of the address
106119
async fn get_transaction_count<T: Into<NameOrAddress> + Send + Sync>(
107120
&self,
108121
from: T,
109122
block: Option<BlockNumber>,
110-
) -> Result<U256, ProviderError> {
123+
) -> Result<U256, Self::Error> {
111124
let from = match from.into() {
112125
NameOrAddress::Name(ens_name) => self.resolve_name(&ens_name).await?,
113126
NameOrAddress::Address(addr) => addr,
114127
};
115-
self.0.get_transaction_count(from, block).await
128+
self.0
129+
.get_transaction_count(from, block)
130+
.await
131+
.map_err(EnsError::MiddlewareError)
116132
}
117133

118134
/// Get the storage of an address for a particular slot location
@@ -121,54 +137,72 @@ impl<M: Middleware<Error = ProviderError>> Middleware for EnsProvider<M> {
121137
from: T,
122138
location: H256,
123139
block: Option<BlockNumber>,
124-
) -> Result<H256, ProviderError> {
140+
) -> Result<H256, Self::Error> {
125141
let from = match from.into() {
126142
NameOrAddress::Name(ens_name) => self.resolve_name(&ens_name).await?,
127143
NameOrAddress::Address(addr) => addr,
128144
};
129-
self.0.get_storage_at(from, location, block).await
145+
self.0
146+
.get_storage_at(from, location, block)
147+
.await
148+
.map_err(EnsError::MiddlewareError)
130149
}
131150

132151
/// Returns the deployed code at a given address
133152
async fn get_code<T: Into<NameOrAddress> + Send + Sync>(
134153
&self,
135154
at: T,
136155
block: Option<BlockNumber>,
137-
) -> Result<Bytes, ProviderError> {
156+
) -> Result<Bytes, Self::Error> {
138157
let at = match at.into() {
139158
NameOrAddress::Name(ens_name) => self.resolve_name(&ens_name).await?,
140159
NameOrAddress::Address(addr) => addr,
141160
};
142-
self.0.get_code(at, block).await
161+
self.0
162+
.get_code(at, block)
163+
.await
164+
.map_err(EnsError::MiddlewareError)
143165
}
144166

145167
/// Gets the latest block number via the `eth_BlockNumber` API
146-
async fn get_block_number(&self) -> Result<U64, ProviderError> {
147-
self.0.get_block_number().await
168+
async fn get_block_number(&self) -> Result<U64, Self::Error> {
169+
self.0
170+
.get_block_number()
171+
.await
172+
.map_err(EnsError::MiddlewareError)
148173
}
149174

150175
/// Gets the block at `block_hash_or_number` (transaction hashes only)
151176
async fn get_block<T: Into<BlockId> + Send + Sync>(
152177
&self,
153178
block_hash_or_number: T,
154-
) -> Result<Option<Block<TxHash>>, ProviderError> {
155-
self.0.get_block(block_hash_or_number).await
179+
) -> Result<Option<Block<TxHash>>, Self::Error> {
180+
self.0
181+
.get_block(block_hash_or_number)
182+
.await
183+
.map_err(EnsError::MiddlewareError)
156184
}
157185

158186
/// Gets the block at `block_hash_or_number` (full transactions included)
159187
async fn get_block_with_txs<T: Into<BlockId> + Send + Sync>(
160188
&self,
161189
block_hash_or_number: T,
162-
) -> Result<Option<Block<Transaction>>, ProviderError> {
163-
self.0.get_block_with_txs(block_hash_or_number).await
190+
) -> Result<Option<Block<Transaction>>, Self::Error> {
191+
self.0
192+
.get_block_with_txs(block_hash_or_number)
193+
.await
194+
.map_err(EnsError::MiddlewareError)
164195
}
165196

166197
async fn call(
167198
&self,
168199
tx: &TransactionRequest,
169200
block: Option<BlockNumber>,
170-
) -> Result<Bytes, ProviderError> {
171-
self.0.call(tx, block).await
201+
) -> Result<Bytes, Self::Error> {
202+
self.0
203+
.call(tx, block)
204+
.await
205+
.map_err(EnsError::MiddlewareError)
172206
}
173207
}
174208

@@ -204,7 +238,7 @@ impl<M: Middleware> EnsProvider<M> {
204238
param: ParamType,
205239
ens_name: &str,
206240
selector: Selector,
207-
) -> Result<T, ProviderError> {
241+
) -> Result<T, EnsError<M>> {
208242
// Get the ENS address, prioritize the local override variable
209243
let ens_addr = self.1.unwrap_or(ens::ENS_ADDRESS);
210244

@@ -213,18 +247,20 @@ impl<M: Middleware> EnsProvider<M> {
213247
let data = self
214248
.0
215249
.call(&ens::get_resolver(ens_addr, ens_name), None)
216-
.await?;
250+
.await
251+
.map_err(EnsError::MiddlewareError)?;
217252

218253
let resolver_address: Address = decode_bytes(ParamType::Address, data);
219254
if resolver_address == Address::zero() {
220-
return Err(ProviderError::EnsError(ens_name.to_owned()));
255+
return Err(ProviderError::EnsError(ens_name.to_owned()).into());
221256
}
222257

223258
// resolve
224259
let data = self
225260
.0
226261
.call(&ens::resolve(resolver_address, selector, ens_name), None)
227-
.await?;
262+
.await
263+
.map_err(EnsError::MiddlewareError)?;
228264

229265
Ok(decode_bytes(param, data))
230266
}
@@ -239,9 +275,9 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
239275
/// This will consume gas from the account that signed the transaction.
240276
async fn send_transaction(
241277
&self,
242-
mut tx: TransactionRequest,
243-
block: Option<BlockNumber>,
244-
) -> Result<TxHash, ProviderError> {
278+
tx: TransactionRequest,
279+
_: Option<BlockNumber>,
280+
) -> Result<TxHash, Self::Error> {
245281
Ok(self
246282
.0
247283
.request("eth_sendTransaction", [tx])
@@ -254,7 +290,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
254290
// Functions for querying the state of the blockchain
255291

256292
/// Gets the latest block number via the `eth_BlockNumber` API
257-
async fn get_block_number(&self) -> Result<U64, ProviderError> {
293+
async fn get_block_number(&self) -> Result<U64, Self::Error> {
258294
Ok(self
259295
.0
260296
.request("eth_blockNumber", ())
@@ -266,7 +302,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
266302
async fn get_block<T: Into<BlockId> + Send + Sync>(
267303
&self,
268304
block_hash_or_number: T,
269-
) -> Result<Option<Block<TxHash>>, ProviderError> {
305+
) -> Result<Option<Block<TxHash>>, Self::Error> {
270306
Ok(self
271307
.get_block_gen(block_hash_or_number.into(), false)
272308
.await?)
@@ -276,7 +312,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
276312
async fn get_block_with_txs<T: Into<BlockId> + Send + Sync>(
277313
&self,
278314
block_hash_or_number: T,
279-
) -> Result<Option<Block<Transaction>>, ProviderError> {
315+
) -> Result<Option<Block<Transaction>>, Self::Error> {
280316
Ok(self
281317
.get_block_gen(block_hash_or_number.into(), true)
282318
.await?)
@@ -287,7 +323,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
287323
&self,
288324
from: T,
289325
block: Option<BlockNumber>,
290-
) -> Result<U256, ProviderError> {
326+
) -> Result<U256, Self::Error> {
291327
let from = utils::serialize(&from.into());
292328
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
293329
Ok(self
@@ -303,7 +339,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
303339
&self,
304340
tx: &TransactionRequest,
305341
block: Option<BlockNumber>,
306-
) -> Result<Bytes, ProviderError> {
342+
) -> Result<Bytes, Self::Error> {
307343
let tx = utils::serialize(tx);
308344
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
309345
Ok(self
@@ -319,7 +355,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
319355
from: T,
320356
location: H256,
321357
block: Option<BlockNumber>,
322-
) -> Result<H256, ProviderError> {
358+
) -> Result<H256, Self::Error> {
323359
let from = utils::serialize(&from.into());
324360
let location = utils::serialize(&location);
325361
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
@@ -335,7 +371,7 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
335371
&self,
336372
at: T,
337373
block: Option<BlockNumber>,
338-
) -> Result<Bytes, ProviderError> {
374+
) -> Result<Bytes, Self::Error> {
339375
let at = utils::serialize(&at.into());
340376
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
341377
Ok(self
@@ -345,11 +381,11 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
345381
.map_err(Into::into)?)
346382
}
347383

348-
async fn resolve_name(&self, ens_name: &str) -> Result<Address, Self::Error> {
384+
async fn resolve_name(&self, _: &str) -> Result<Address, Self::Error> {
349385
unimplemented!("No ENS at the inner layer of the onion");
350386
}
351387

352-
async fn lookup_address(&self, address: Address) -> Result<String, Self::Error> {
388+
async fn lookup_address(&self, _: Address) -> Result<String, Self::Error> {
353389
unimplemented!("No ENS at the inner layer of the onion");
354390
}
355391
}

Diff for: ethers-providers/src/transports/ws.rs

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ impl Provider<WebSocketStream<MaybeTlsStream>> {
107107
}
108108
}
109109

110+
impl<S> std::fmt::Debug for Provider<S> {
111+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112+
f.debug_struct("WebSockets Provider").finish()
113+
}
114+
}
115+
110116
impl<S> Provider<S>
111117
where
112118
S: Send

0 commit comments

Comments
 (0)