diff --git a/starknet-providers/src/jsonrpc/transports/http.rs b/starknet-providers/src/jsonrpc/transports/http.rs index 565c6c18..f45af8c2 100644 --- a/starknet-providers/src/jsonrpc/transports/http.rs +++ b/starknet-providers/src/jsonrpc/transports/http.rs @@ -2,6 +2,7 @@ use async_trait::async_trait; use log::trace; use reqwest::{Client, Url}; use serde::{de::DeserializeOwned, Serialize}; +use serde_json::Value; use crate::jsonrpc::{transports::JsonRpcTransport, JsonRpcMethod, JsonRpcResponse}; @@ -75,7 +76,26 @@ impl JsonRpcTransport for HttpTransport { let response_body = response.text().await.map_err(Self::Error::Reqwest)?; trace!("Response from JSON-RPC: {}", response_body); - let parsed_response = serde_json::from_str(&response_body).map_err(Self::Error::Json)?; + let mut parsed_response = + serde_json::from_str(&response_body).map_err(Self::Error::Json)?; + + if let JsonRpcResponse::Error { ref mut error, .. } = parsed_response { + if error.code == 40 { + trace!("Extracting `data` from ContractError (40)"); + let json_raw: Value = + serde_json::from_str(&response_body).map_err(Self::Error::Json)?; + // "error" key is safe to unwrap here as we parsed the response correctly with + // the field "error". + if let Some(data) = json_raw.get("error").unwrap().get("data") { + if let Some(revert_error) = data.get("revert_error") { + error.message += &format!( + "\nrevert_error: {}", + revert_error.as_str().unwrap_or("Not available") + ); + } + } + } + } Ok(parsed_response) }