Skip to content

Commit 03b0b7e

Browse files
feat(starknet_gateway_types,starknet_http_server): move spec error conversion to the http server
1 parent a422bb3 commit 03b0b7e

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_gateway_types/src/errors.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use axum::http::StatusCode;
2-
use axum::response::{IntoResponse, Response};
31
use enum_assoc::Assoc;
42
use papyrus_network_types::network_types::BroadcastedMessageMetadata;
53
use papyrus_rpc::error::{
@@ -59,25 +57,6 @@ pub enum GatewaySpecError {
5957
ValidationFailure { data: String },
6058
}
6159

62-
impl IntoResponse for GatewaySpecError {
63-
fn into_response(self) -> Response {
64-
let as_rpc = self.into_rpc();
65-
// TODO(Arni): Fix the status code. The status code should be a HTTP status code - not a
66-
// Json RPC error code. status code.
67-
let status =
68-
StatusCode::from_u16(u16::try_from(as_rpc.code).expect("Expecting a valid u16"))
69-
.expect("Expecting a valid error code");
70-
71-
let resp = Response::builder()
72-
.status(status)
73-
.body((as_rpc.message, as_rpc.data))
74-
.expect("Expecting valid response");
75-
let status = resp.status();
76-
let body = serde_json::to_string(resp.body()).expect("Expecting valid body");
77-
(status, body).into_response()
78-
}
79-
}
80-
8160
impl std::fmt::Display for GatewaySpecError {
8261
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8362
let as_rpc = self.clone().into_rpc();

crates/starknet_http_server/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ workspace = true
1515
axum.workspace = true
1616
hyper.workspace = true
1717
infra_utils.workspace = true
18+
jsonrpsee = { workspace = true, features = ["full"] }
1819
papyrus_config.workspace = true
1920
reqwest = { workspace = true, optional = true }
2021
serde.workspace = true
22+
serde_json.workspace = true
2123
starknet_api.workspace = true
2224
starknet_gateway_types.workspace = true
2325
starknet_sequencer_infra.workspace = true
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
use axum::response::{IntoResponse, Response};
2+
use jsonrpsee::types::error::ErrorCode;
3+
use starknet_gateway_types::communication::GatewayClientError;
4+
use starknet_gateway_types::errors::GatewayError;
15
use thiserror::Error;
6+
use tracing::error;
27

38
/// Errors originating from `[`HttpServer::run`]` command.
49
#[derive(Debug, Error)]
510
pub enum HttpServerRunError {
611
#[error(transparent)]
712
ServerStartupError(#[from] hyper::Error),
813
}
14+
15+
/// Wraps the `GatewayClientError` in order to implement Axum's `IntoResponse` trait.
16+
#[derive(Error, Debug)]
17+
#[error(transparent)]
18+
pub struct GatewayClientErrorWrapper(#[from] GatewayClientError);
19+
20+
impl IntoResponse for GatewayClientErrorWrapper {
21+
fn into_response(self) -> Response {
22+
let general_rpc_error = match self.0 {
23+
GatewayClientError::ClientError(e) => {
24+
error!("Got a gateway client: {}", e);
25+
jsonrpsee::types::ErrorObject::owned(
26+
ErrorCode::InternalError.code(),
27+
"Internal error",
28+
None::<()>,
29+
)
30+
}
31+
GatewayClientError::GatewayError(GatewayError::GatewaySpecError {
32+
source,
33+
p2p_message_metadata: _,
34+
}) => {
35+
let rpc_spec_error = source.into_rpc();
36+
jsonrpsee::types::ErrorObject::owned(
37+
ErrorCode::ServerError(rpc_spec_error.code).code(),
38+
rpc_spec_error.message,
39+
rpc_spec_error.data,
40+
)
41+
}
42+
};
43+
44+
serde_json::to_vec(&general_rpc_error)
45+
.expect("Expecting a serializable error.")
46+
.into_response()
47+
}
48+
}

crates/starknet_http_server/src/http_server.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ use infra_utils::type_name::short_type_name;
88
use starknet_api::rpc_transaction::RpcTransaction;
99
use starknet_api::transaction::TransactionHash;
1010
use starknet_gateway_types::communication::SharedGatewayClient;
11-
use starknet_gateway_types::errors::GatewaySpecError;
1211
use starknet_gateway_types::gateway_types::GatewayInput;
1312
use starknet_sequencer_infra::component_definitions::ComponentStarter;
1413
use starknet_sequencer_infra::errors::ComponentError;
15-
use tracing::{error, info, instrument};
14+
use tracing::{debug, info, instrument};
1615

1716
use crate::config::HttpServerConfig;
18-
use crate::errors::HttpServerRunError;
17+
use crate::errors::{GatewayClientErrorWrapper, HttpServerRunError};
1918

2019
#[cfg(test)]
2120
#[path = "http_server_test.rs"]
2221
pub mod http_server_test;
2322

24-
pub type HttpServerResult<T> = Result<T, GatewaySpecError>;
23+
pub type HttpServerResult<T> = Result<T, GatewayClientErrorWrapper>;
2524

2625
pub struct HttpServer {
2726
pub config: HttpServerConfig,
@@ -64,16 +63,16 @@ async fn add_tx(
6463
) -> HttpServerResult<Json<TransactionHash>> {
6564
let gateway_input: GatewayInput = GatewayInput { rpc_tx: tx.clone(), message_metadata: None };
6665

67-
let add_tx_result = app_state.gateway_client.add_tx(gateway_input).await.map_err(|join_err| {
68-
error!("Failed to process tx: {}", join_err);
69-
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
66+
let add_tx_result = app_state.gateway_client.add_tx(gateway_input).await.map_err(|e| {
67+
debug!("Error while adding transaction: {}", e);
68+
GatewayClientErrorWrapper::from(e)
7069
});
7170

7271
add_tx_result_as_json(add_tx_result)
7372
}
7473

7574
pub(crate) fn add_tx_result_as_json(
76-
result: Result<TransactionHash, GatewaySpecError>,
75+
result: Result<TransactionHash, GatewayClientErrorWrapper>,
7776
) -> HttpServerResult<Json<TransactionHash>> {
7877
let tx_hash = result?;
7978
Ok(Json(tx_hash))

0 commit comments

Comments
 (0)