Skip to content

Commit

Permalink
add a non-static string variant for CustomError and InvalidParams
Browse files Browse the repository at this point in the history
This enables uses cases that require error message built with runtime data such as
```
RPCError::InvalidParamsString(format!("Method {method_name} requires {num_required} params, got {num_actual}"))
```

```
// Forward error message
RPCError::CustomErrorString(1, err.what().to_string())
```
  • Loading branch information
PiRK committed Dec 30, 2024
1 parent e42f231 commit 22eedd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jsonrpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ pub enum RPCError {
#[error("Custom Error: code: {0} msg: {1}")]
CustomError(i32, &'static str),

#[error("Custom Error: code: {0} msg: {1}")]
CustomErrorString(i32, String),

#[error("Invalid Params: {0}")]
InvalidParams(&'static str),

#[error("Invalid Params: {0}")]
InvalidParamsString(String),

#[error("Invalid Request: {0}")]
InvalidRequest(&'static str),

Expand Down
10 changes: 10 additions & 0 deletions jsonrpc/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ impl RPCError {
message: msg.to_string(),
data,
},
RPCError::InvalidParamsString(msg) => Error {
code: INVALID_PARAMS_ERROR_CODE,
message: msg.to_string(),
data,
},
RPCError::InvalidRequest(msg) => Error {
code: INVALID_REQUEST_ERROR_CODE,
message: msg.to_string(),
Expand All @@ -174,6 +179,11 @@ impl RPCError {
message: msg.to_string(),
data,
},
RPCError::CustomErrorString(code, msg) => Error {
code: *code,
message: msg.to_string(),
data,
},
RPCError::InternalError => Error {
code: INTERNAL_ERROR_CODE,
message: INTERNAL_ERROR_MSG.to_string(),
Expand Down

0 comments on commit 22eedd7

Please sign in to comment.