Skip to content

Commit

Permalink
Merge pull request #50 from luca992/patch-1
Browse files Browse the repository at this point in the history
fix(request_handler) to read error message from error json body
  • Loading branch information
0xIchigo authored Jun 21, 2024
2 parents dc1fd1a + 40a90b9 commit 2590773
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use reqwest::{Client, Method, RequestBuilder, Response, StatusCode, Url};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::sync::Arc;
use serde_json::Value;

/// Manages HTTP requests for the `Helius` client
///
Expand Down Expand Up @@ -100,10 +101,18 @@ impl RequestHandler {
}
}
} else {
let body_json: serde_json::Result<serde_json::Value> = serde_json::from_str(&body_text);
let body_json: serde_json::Result<Value> = serde_json::from_str(&body_text);
match body_json {
Ok(body) => {
let error_message: String = body["message"].as_str().unwrap_or("Unknown error").to_string();
let error_message = match body["error"].clone() {
Value::Object(error_value) => {
error_value.into_iter().map(|(k, v)| format!("{}: {}", k, v)).collect::<Vec<String>>().join(", ").to_string()
}
Value::String(error_value) => {
error_value
}
_ => "Unknown error".to_string(),
};
Err(HeliusError::from_response_status(status, path, error_message))
}
Err(_) => Err(HeliusError::from_response_status(status, path, body_text)),
Expand Down
41 changes: 40 additions & 1 deletion tests/test_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn test_bad_request_error() {
.mock("GET", "/")
.with_status(400)
.with_header("content-type", "application/json")
.with_body(r#"{"message": "bad request"}"#)
.with_body(r#"{"error": "bad request"}"#)
.create();

let client: Arc<Client> = Arc::new(Client::new());
Expand All @@ -63,3 +63,42 @@ async fn test_bad_request_error() {

server.reset();
}

#[tokio::test]
async fn test_bad_request_with_json_rpc_error() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = server.url();

server
.mock("GET", "/")
.with_status(400)
.with_header("content-type", "application/json")
.with_body(r#"
{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "internal error: please contact Helius support if this persists"
}
}"#)
.create();

let client: Arc<Client> = Arc::new(Client::new());
let handler: RequestHandler = RequestHandler::new(client).unwrap();

let response: Result<MockResponse> = handler
.send::<(), MockResponse>(Method::GET, url.parse().unwrap(), None)
.await;

assert!(response.is_err());
match response {
Err(HeliusError::BadRequest { text, .. }) =>
assert_eq!(
text,
"code: -32603, message: \"internal error: please contact Helius support if this persists\""
),
_ => panic!("Expected BadRequest error"),
}

server.reset();
}

0 comments on commit 2590773

Please sign in to comment.