11use reqwest:: { Client , Method } ;
22use 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 }
0 commit comments