Skip to content

Commit 7b22511

Browse files
TomasArracheaSantiagoPittella
authored andcommitted
feat: remove ApiError (#784)
* feat: remove `ApiError` * review: update CHANGELOG * review: use anyhow context on url conversion * chore: lint
1 parent 7833392 commit 7b22511

14 files changed

Lines changed: 51 additions & 84 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/faucet/src/server/get_tokens.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use axum::{
55
};
66
use http::header;
77
use http_body_util::Full;
8-
use miden_node_utils::errors::ErrorReport;
8+
use miden_node_utils::ErrorReport;
99
use miden_objects::{
1010
AccountIdError,
1111
account::AccountId,

bin/faucet/src/stub_rpc_api.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use miden_node_proto::generated::{
23
block::BlockHeader,
34
digest::Digest,
@@ -15,7 +16,6 @@ use miden_node_proto::generated::{
1516
},
1617
rpc::api_server,
1718
};
18-
use miden_node_utils::errors::ApiError;
1919
use tokio::net::TcpListener;
2020
use tokio_stream::wrappers::TcpListenerStream;
2121
use tonic::{Request, Response, Status};
@@ -146,10 +146,10 @@ impl api_server::Api for StubRpcApi {
146146
}
147147
}
148148

149-
pub async fn serve_stub(endpoint: &Url) -> Result<(), ApiError> {
149+
pub async fn serve_stub(endpoint: &Url) -> anyhow::Result<()> {
150150
let addr = endpoint
151151
.socket_addrs(|| None)
152-
.map_err(ApiError::EndpointToSocketFailed)?
152+
.context("failed to convert endpoint to socket address")?
153153
.into_iter()
154154
.next()
155155
.unwrap();
@@ -162,5 +162,5 @@ pub async fn serve_stub(endpoint: &Url) -> Result<(), ApiError> {
162162
.add_service(api_service)
163163
.serve_with_incoming(TcpListenerStream::new(listener))
164164
.await
165-
.map_err(ApiError::ApiServeFailed)
165+
.context("failed to serve stub RPC API")
166166
}

crates/block-producer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ testing = []
1919
tracing-forest = ["miden-node-utils/tracing-forest"]
2020

2121
[dependencies]
22+
anyhow = { workspace = true }
2223
async-trait = { version = "0.1" }
2324
futures = { workspace = true }
2425
itertools = { workspace = true }

crates/block-producer/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use miden_block_prover::ProvenBlockError;
22
use miden_node_proto::errors::ConversionError;
3-
use miden_node_utils::{errors::ErrorReport, formatting::format_opt};
3+
use miden_node_utils::{ErrorReport, formatting::format_opt};
44
use miden_objects::{
55
Digest, ProposedBatchError, ProposedBlockError,
66
block::BlockNumber,

crates/block-producer/src/server.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::{collections::HashMap, net::SocketAddr, time::Duration};
22

3+
use anyhow::{Context, Result};
34
use miden_node_proto::generated::{
45
block_producer::api_server, requests::SubmitProvenTransactionRequest,
56
responses::SubmitProvenTransactionResponse, store::api_client as store_client,
67
};
78
use miden_node_utils::{
8-
errors::ApiError,
99
formatting::{format_input_notes, format_output_notes},
1010
tracing::grpc::{OtelInterceptor, block_producer_trace_fn},
1111
};
@@ -59,23 +59,20 @@ impl BlockProducer {
5959
block_prover: Option<Url>,
6060
batch_interval: Duration,
6161
block_interval: Duration,
62-
) -> Result<Self, ApiError> {
62+
) -> Result<Self> {
6363
info!(target: COMPONENT, endpoint=?listener, store=%store_address, "Initializing server");
6464

6565
let store_url = format!("http://{store_address}");
66-
let channel = tonic::transport::Endpoint::try_from(store_url)
67-
.map_err(|err| ApiError::InvalidStoreUrl(err.to_string()))?
66+
let channel = tonic::transport::Endpoint::try_from(store_url.clone())
67+
.with_context(|| format!("failed to create store endpoint for {store_url}"))?
6868
.connect()
6969
.await
70-
.map_err(|err| ApiError::DatabaseConnectionFailed(err.to_string()))?;
70+
.with_context(|| format!("failed to connect to store on {store_url}"))?;
7171

7272
let store = store_client::ApiClient::with_interceptor(channel, OtelInterceptor);
7373
let store = StoreClient::new(store);
7474

75-
let latest_header = store
76-
.latest_header()
77-
.await
78-
.map_err(|err| ApiError::DatabaseConnectionFailed(err.to_string()))?;
75+
let latest_header = store.latest_header().await.context("failed to get latest header")?;
7976
let chain_tip = latest_header.block_num();
8077

8178
info!(target: COMPONENT, "Server initialized");

crates/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ version.workspace = true
1515
workspace = true
1616

1717
[dependencies]
18+
anyhow = { workspace = true }
1819
miden-node-proto = { workspace = true }
1920
miden-node-utils = { workspace = true }
2021
miden-objects = { workspace = true }

crates/rpc/src/server/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::net::SocketAddr;
22

3+
use anyhow::Context;
34
use api::RpcService;
45
use miden_node_proto::generated::rpc::api_server;
5-
use miden_node_utils::errors::ApiError;
66
use tokio::net::TcpListener;
77
use tokio_stream::wrappers::TcpListenerStream;
88
use tracing::info;
@@ -27,12 +27,12 @@ impl Rpc {
2727
listener: TcpListener,
2828
store: SocketAddr,
2929
block_producer: SocketAddr,
30-
) -> Result<Self, ApiError> {
30+
) -> anyhow::Result<Self> {
3131
info!(target: COMPONENT, endpoint=?listener, %store, %block_producer, "Initializing server");
3232

3333
let api = api::RpcService::new(store, block_producer)
3434
.await
35-
.map_err(|err| ApiError::ApiInitialisationFailed(err.to_string()))?;
35+
.context("failed to initialize RPC service")?;
3636
let api_service = api_server::ApiServer::new(api);
3737

3838
info!(target: COMPONENT, "Server initialized");
@@ -43,12 +43,12 @@ impl Rpc {
4343
/// Serves the RPC API.
4444
///
4545
/// Note: this blocks until the server dies.
46-
pub async fn serve(self) -> Result<(), ApiError> {
46+
pub async fn serve(self) -> anyhow::Result<()> {
4747
tonic::transport::Server::builder()
4848
.accept_http1(true)
4949
.add_service(self.api_service)
5050
.serve_with_incoming(TcpListenerStream::new(self.listener))
5151
.await
52-
.map_err(ApiError::ApiServeFailed)
52+
.context("failed to serve RPC API")
5353
}
5454
}

crates/store/src/server/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use anyhow::Context;
88
use miden_node_proto::generated::store::api_server;
9-
use miden_node_utils::{errors::ApiError, tracing::grpc::store_trace_fn};
9+
use miden_node_utils::tracing::grpc::store_trace_fn;
1010
use tokio::net::TcpListener;
1111
use tokio_stream::wrappers::TcpListenerStream;
1212
use tower_http::trace::TraceLayer;
@@ -70,22 +70,19 @@ impl Store {
7070
}
7171

7272
/// Performs initialization tasks required before [`serve`](Self::serve) can be called.
73-
pub async fn init(listener: TcpListener, data_directory: PathBuf) -> Result<Self, ApiError> {
73+
pub async fn init(listener: TcpListener, data_directory: PathBuf) -> anyhow::Result<Self> {
7474
info!(target: COMPONENT, endpoint=?listener, ?data_directory, "Loading database");
7575

7676
let data_directory = DataDirectory::load(data_directory)?;
7777

7878
let block_store = Arc::new(BlockStore::load(data_directory.block_store_dir())?);
7979

80-
let db = Db::load(data_directory.database_path())
81-
.await
82-
.map_err(|err| ApiError::ApiInitialisationFailed(err.to_string()))?;
80+
let database_filepath = data_directory.database_path();
81+
let db = Db::load(database_filepath.clone()).await.with_context(|| {
82+
format!("failed to load database at {}", database_filepath.display())
83+
})?;
8384

84-
let state = Arc::new(
85-
State::load(db, block_store)
86-
.await
87-
.map_err(|err| ApiError::DatabaseConnectionFailed(err.to_string()))?,
88-
);
85+
let state = Arc::new(State::load(db, block_store).await.context("failed to load state")?);
8986

9087
let db_maintenance_service =
9188
DbMaintenance::new(Arc::clone(&state), DATABASE_MAINTENANCE_INTERVAL);
@@ -103,15 +100,15 @@ impl Store {
103100
/// Serves the store's RPC API and DB maintenance background task.
104101
///
105102
/// Note: this blocks until the server dies.
106-
pub async fn serve(self) -> Result<(), ApiError> {
103+
pub async fn serve(self) -> anyhow::Result<()> {
107104
tokio::spawn(self.db_maintenance_service.run());
108105
// Build the gRPC server with the API service and trace layer.
109106
tonic::transport::Server::builder()
110107
.layer(TraceLayer::new_for_grpc().make_span_with(store_trace_fn))
111108
.add_service(self.api_service)
112109
.serve_with_incoming(TcpListenerStream::new(self.listener))
113110
.await
114-
.map_err(ApiError::ApiServeFailed)
111+
.context("failed to serve store API")
115112
}
116113
}
117114

crates/utils/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ opentelemetry-otlp = { version = "0.29", default-features = false, features =
2929
opentelemetry_sdk = { version = "0.29", features = ["rt-tokio", "testing"] }
3030
rand = { workspace = true }
3131
serde = { version = "1.0", features = ["derive"] }
32-
thiserror = { workspace = true }
3332
tonic = { workspace = true }
3433
tracing = { workspace = true }
3534
tracing-forest = { version = "0.1", optional = true, features = ["chrono"] }

0 commit comments

Comments
 (0)