Skip to content

Commit 7d48a1d

Browse files
committed
applied suggested copilot fixes
1 parent 552701c commit 7d48a1d

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

crates/blockless-drivers/src/handlers/http.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use reqwest::{Client, Method};
22
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
4-
use std::str::FromStr;
5-
use std::time::Duration;
3+
use std::{collections::HashMap, str::FromStr, time::Duration};
64

75
// Import RPC types from parent module
8-
use crate::wasi::rpc::{JsonRpcError, JsonRpcResponse};
6+
use crate::wasi::rpc::{JsonRpcError, JsonRpcResponse, RPC_VERSION};
97

108
// HTTP request structures matching the SDK
119
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -77,7 +75,7 @@ pub async fn handle_http_request(params: Option<serde_json::Value>, id: u32) ->
7775
Ok(req) => req,
7876
Err(e) => {
7977
return JsonRpcResponse {
80-
jsonrpc: "2.0".to_string(),
78+
jsonrpc: RPC_VERSION.to_string(),
8179
result: None,
8280
error: Some(JsonRpcError {
8381
code: -32602,
@@ -92,7 +90,7 @@ pub async fn handle_http_request(params: Option<serde_json::Value>, id: u32) ->
9290
},
9391
None => {
9492
return JsonRpcResponse {
95-
jsonrpc: "2.0".to_string(),
93+
jsonrpc: RPC_VERSION.to_string(),
9694
result: None,
9795
error: Some(JsonRpcError {
9896
code: -32602,
@@ -109,8 +107,24 @@ pub async fn handle_http_request(params: Option<serde_json::Value>, id: u32) ->
109107
// Execute the HTTP request using the http_v2 driver
110108
let result = execute_http_request(http_request).await;
111109
JsonRpcResponse {
112-
jsonrpc: "2.0".to_string(),
113-
result: Some(serde_json::to_value(result).unwrap()),
110+
jsonrpc: RPC_VERSION.to_string(),
111+
result: match serde_json::to_value(result) {
112+
Ok(value) => Some(value),
113+
Err(e) => {
114+
return JsonRpcResponse {
115+
jsonrpc: RPC_VERSION.to_string(),
116+
result: None,
117+
error: Some(JsonRpcError {
118+
code: -32603,
119+
message: "Internal error".to_string(),
120+
data: Some(serde_json::json!({
121+
"error": format!("Failed to serialize result: {}", e)
122+
})),
123+
}),
124+
id,
125+
};
126+
}
127+
},
114128
error: None,
115129
id,
116130
}

crates/blockless-drivers/src/wasi/rpc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ wiggle::from_witx!({
1010
async: *,
1111
});
1212

13+
pub const RPC_VERSION: &str = "2.0";
14+
1315
// JSON-RPC 2.0 structures
1416
#[derive(Serialize, Deserialize, Debug)]
1517
pub struct JsonRpcRequest {
@@ -112,22 +114,22 @@ async fn handle_rpc_request(request: JsonRpcRequest) -> JsonRpcResponse {
112114

113115
match request.method.as_str() {
114116
"ping" => JsonRpcResponse {
115-
jsonrpc: "2.0".to_string(),
117+
jsonrpc: RPC_VERSION.to_string(),
116118
result: Some(serde_json::json!("pong")),
117119
error: None,
118120
id,
119121
},
120122
"echo" => {
121123
let params = request.params.unwrap_or(serde_json::Value::Null);
122124
JsonRpcResponse {
123-
jsonrpc: "2.0".to_string(),
125+
jsonrpc: RPC_VERSION.to_string(),
124126
result: Some(params),
125127
error: None,
126128
id,
127129
}
128130
}
129131
"version" => JsonRpcResponse {
130-
jsonrpc: "2.0".to_string(),
132+
jsonrpc: RPC_VERSION.to_string(),
131133
result: Some(serde_json::json!({
132134
"runtime": "bls-runtime",
133135
"version": env!("CARGO_PKG_VERSION"),
@@ -138,7 +140,7 @@ async fn handle_rpc_request(request: JsonRpcRequest) -> JsonRpcResponse {
138140
},
139141
"http.request" => crate::handlers::http::handle_http_request(request.params, id).await,
140142
_ => JsonRpcResponse {
141-
jsonrpc: "2.0".to_string(),
143+
jsonrpc: RPC_VERSION.to_string(),
142144
result: None,
143145
error: Some(JsonRpcError {
144146
code: -32601,

0 commit comments

Comments
 (0)