Skip to content

Commit faf8d15

Browse files
committed
using enum to represent json rpc error codes
1 parent 7d48a1d commit faf8d15

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33
use std::{collections::HashMap, str::FromStr, time::Duration};
44

55
// Import RPC types from parent module
6-
use crate::wasi::rpc::{JsonRpcError, JsonRpcResponse, RPC_VERSION};
6+
use crate::wasi::rpc::{JsonRpcError, JsonRpcErrorCode, JsonRpcResponse, RPC_VERSION};
77

88
// HTTP request structures matching the SDK
99
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -93,7 +93,7 @@ pub async fn handle_http_request(params: Option<serde_json::Value>, id: u32) ->
9393
jsonrpc: RPC_VERSION.to_string(),
9494
result: None,
9595
error: Some(JsonRpcError {
96-
code: -32602,
96+
code: JsonRpcErrorCode::InvalidParams as i32,
9797
message: "Invalid params".to_string(),
9898
data: Some(serde_json::json!({
9999
"error": "Missing HTTP request parameters"
@@ -115,7 +115,7 @@ pub async fn handle_http_request(params: Option<serde_json::Value>, id: u32) ->
115115
jsonrpc: RPC_VERSION.to_string(),
116116
result: None,
117117
error: Some(JsonRpcError {
118-
code: -32603,
118+
code: JsonRpcErrorCode::InternalError as i32,
119119
message: "Internal error".to_string(),
120120
data: Some(serde_json::json!({
121121
"error": format!("Failed to serialize result: {}", e)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ pub struct JsonRpcError {
3939
pub data: Option<serde_json::Value>,
4040
}
4141

42+
// https://www.jsonrpc.org/specification#error_object
43+
#[derive(Debug, Clone, Copy)]
44+
#[repr(i32)]
45+
pub enum JsonRpcErrorCode {
46+
ParseError = -32700,
47+
InvalidRequest = -32600,
48+
MethodNotFound = -32601,
49+
InvalidParams = -32602,
50+
InternalError = -32603,
51+
}
52+
4253
impl types::UserErrorConversion for WasiCtx {
4354
fn blockless_rpc_error_from_blockless_rpc_error_kind(
4455
&mut self,
@@ -143,7 +154,7 @@ async fn handle_rpc_request(request: JsonRpcRequest) -> JsonRpcResponse {
143154
jsonrpc: RPC_VERSION.to_string(),
144155
result: None,
145156
error: Some(JsonRpcError {
146-
code: -32601,
157+
code: JsonRpcErrorCode::MethodNotFound as i32,
147158
message: "Method not found".to_string(),
148159
data: Some(serde_json::json!({
149160
"method": request.method

0 commit comments

Comments
 (0)