Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor improve from_config error #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/extensions/api/eth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use std::{sync::Arc, time::Duration};

use async_trait::async_trait;
Expand Down Expand Up @@ -33,7 +34,7 @@ impl Extension for EthApi {
type Config = EthApiConfig;

async fn from_config(config: &Self::Config, registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> {
let client = registry.get::<Client>().await.expect("Client not found");
let client = registry.get::<Client>().await.context("Client not found")?;

Ok(Self::new(client, Duration::from_secs(config.stale_timeout_seconds)))
}
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/api/substrate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use std::{sync::Arc, time::Duration};

use async_trait::async_trait;
Expand Down Expand Up @@ -34,7 +35,7 @@ impl Extension for SubstrateApi {
type Config = SubstrateApiConfig;

async fn from_config(config: &Self::Config, registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> {
let client = registry.get::<Client>().await.expect("Client not found");
let client = registry.get::<Client>().await.context("Client not found")?;

Ok(Self::new(client, Duration::from_secs(config.stale_timeout_seconds)))
}
Expand Down
12 changes: 8 additions & 4 deletions src/extensions/validator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::extensions::client::Client;
use crate::middlewares::{CallRequest, CallResult};
use anyhow::Context;
use async_trait::async_trait;
use serde::Deserialize;
use std::sync::Arc;
Expand All @@ -21,15 +22,18 @@ impl Extension for Validator {
type Config = ValidateConfig;

async fn from_config(config: &Self::Config, registry: &ExtensionRegistry) -> Result<Self, anyhow::Error> {
let client = registry.get::<Client>().await.expect("Client extension not found");
let client = registry.get::<Client>().await.context("Client extension not found")?;

let clients = client
.endpoints()
.iter()
.map(|e| Arc::new(Client::with_endpoints([e]).expect("Unable to create client")))
.collect();
.map(|e| {
let client = Client::with_endpoints([e]).context("Failed to connect endpoint")?;
Ok(Arc::new(client))
})
.collect::<Result<Vec<Arc<Client>>, anyhow::Error>>();

Ok(Self::new(config.clone(), clients))
Ok(Self::new(config.clone(), clients?))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/block_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for BlockTagMiddlewar
.get::<EthApi>()
.expect("EthApi extension not found");

Some(Box::new(BlockTagMiddleware::new(eth_api, index)))
Some(Box::new(Self::new(eth_api, index)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for DelayMiddleware {
) -> Option<Box<dyn Middleware<CallRequest, CallResult>>> {
method
.delay_ms
.map(|delay| Box::new(DelayMiddleware::new(delay)) as Box<dyn Middleware<CallRequest, CallResult>>)
.map(|delay| Box::new(Self::new(delay)) as Box<dyn Middleware<CallRequest, CallResult>>)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for ResponseMiddlewar
method
.response
.as_ref()
.map(|resp| Box::new(ResponseMiddleware::new(resp.clone())) as Box<dyn Middleware<CallRequest, CallResult>>)
.map(|resp| Box::new(Self::new(resp.clone())) as Box<dyn Middleware<CallRequest, CallResult>>)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for CrazyMiddleware {
method: &RpcMethod,
_extensions: &TypeRegistryRef,
) -> Option<Box<dyn Middleware<CallRequest, CallResult>>> {
Some(Box::new(CrazyMiddleware::new(method.method == "go_crazy")))
Some(Box::new(Self::new(method.method == "go_crazy")))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for UpstreamMiddlewar
.await
.get::<Client>()
.expect("Client extension not found");
Some(Box::new(UpstreamMiddleware::new(client)))
Some(Box::new(Self::new(client)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/methods/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl MiddlewareBuilder<RpcMethod, CallRequest, CallResult> for ValidateMiddlewar
.get::<Validator>()
.expect("Validator extension not found");

Some(Box::new(ValidateMiddleware::new(validate)))
Some(Box::new(Self::new(validate)))
}
}

Expand Down
Loading