-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: erase implementation-specific provider error (#484)
Changes the `Other` variant of `ProviderError` to use a trait object instead of a generic parameter. This makes the `ProviderError` type much easier to work with, as errors from different `Provider` types are now compatible, and any type that wraps `ProviderError` will have one fewer generic parameter to deal with. At the same time, it's still possible, though harder, to access the erased error type by downcasting, which requires the caller to know the concrete type at compile time, as showcased in the new example. This is a breaking change, but it shouldn't affect downstream applications which already erase the whole `ProviderError` type with something like `anyhow`.
- Loading branch information
1 parent
33e200d
commit c166e93
Showing
13 changed files
with
443 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use starknet_providers::{ | ||
jsonrpc::{HttpTransport, HttpTransportError, JsonRpcClient, JsonRpcClientError}, | ||
Provider, ProviderError, | ||
}; | ||
use url::Url; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let rpc_client = | ||
JsonRpcClient::new(HttpTransport::new(Url::parse("https://fake.url/").unwrap())); | ||
|
||
let error = match rpc_client.block_number().await.unwrap_err() { | ||
ProviderError::Other(inner) => inner, | ||
_ => panic!("unexpected error variant"), | ||
}; | ||
|
||
// The implementation-specific error type is erased to make the `ProviderError` type easier to | ||
// work with. Here, we showcase how to recover the inner type. | ||
let impl_specific_error: &JsonRpcClientError<HttpTransportError> = | ||
match error.as_any().downcast_ref() { | ||
Some(inner) => inner, | ||
None => panic!("unexpected downcast failure"), | ||
}; | ||
|
||
dbg!(impl_specific_error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.