-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrpc.rs
More file actions
40 lines (35 loc) · 994 Bytes
/
rpc.rs
File metadata and controls
40 lines (35 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use core::result::Result as CoreResult;
use jsonrpsee::{
core::RpcResult,
types::error::{ErrorCode, ErrorObject},
};
use sp_api::ApiError;
/// Converts CoreResult to Result for RPC calls
pub fn map_rpc_result<T>(response: CoreResult<T, ApiError>) -> RpcResult<T> {
match response {
Ok(res) => Ok(res),
Err(e) => Err(ErrorObject::owned(
ErrorCode::ServerError(300).code(), // No real reason for this value
"Api Error",
Some(format!("{:?}", e)),
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
// It is enough to test that we are getting into the correct match statement.
#[test]
fn maps_ok_to_ok() {
let result = map_rpc_result(Ok(0));
assert!(result.is_ok());
assert_eq!(0, result.unwrap());
}
#[test]
fn maps_err_to_api_err() {
let result = map_rpc_result::<u64>(Err(ApiError::StateBackendIsNotTrie));
assert!(result.is_err());
let str = format!("{:?}", result.err().unwrap());
assert!(str.contains("Api Error"), "Did not find in: {:?}", str);
}
}