Skip to content

Commit 22eedd7

Browse files
committed
add a non-static string variant for CustomError and InvalidParams
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()) ```
1 parent e42f231 commit 22eedd7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

jsonrpc/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ pub enum RPCError {
8181
#[error("Custom Error: code: {0} msg: {1}")]
8282
CustomError(i32, &'static str),
8383

84+
#[error("Custom Error: code: {0} msg: {1}")]
85+
CustomErrorString(i32, String),
86+
8487
#[error("Invalid Params: {0}")]
8588
InvalidParams(&'static str),
8689

90+
#[error("Invalid Params: {0}")]
91+
InvalidParamsString(String),
92+
8793
#[error("Invalid Request: {0}")]
8894
InvalidRequest(&'static str),
8995

jsonrpc/src/message.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ impl RPCError {
164164
message: msg.to_string(),
165165
data,
166166
},
167+
RPCError::InvalidParamsString(msg) => Error {
168+
code: INVALID_PARAMS_ERROR_CODE,
169+
message: msg.to_string(),
170+
data,
171+
},
167172
RPCError::InvalidRequest(msg) => Error {
168173
code: INVALID_REQUEST_ERROR_CODE,
169174
message: msg.to_string(),
@@ -174,6 +179,11 @@ impl RPCError {
174179
message: msg.to_string(),
175180
data,
176181
},
182+
RPCError::CustomErrorString(code, msg) => Error {
183+
code: *code,
184+
message: msg.to_string(),
185+
data,
186+
},
177187
RPCError::InternalError => Error {
178188
code: INTERNAL_ERROR_CODE,
179189
message: INTERNAL_ERROR_MSG.to_string(),

0 commit comments

Comments
 (0)