Struct jrpc::Error
[−]
[src]
pub struct Error<T> { pub jsonrpc: V2_0, pub error: ErrorObject<T>, pub id: Id, }
The jsonrpc Error response, indicating an error.
Examples
Since the T
in the ErrorObject
will at least be based on the ErrorCode
it is
recommended that you deserialize this type as T=Value
first.
extern crate serde_json; use jrpc::{Id, Error, ErrorCode, ErrorObject, V2_0}; let data: Vec<u32> = vec![1, 2, 3]; let example = Error { jsonrpc: V2_0, error: ErrorObject { code: ErrorCode::from(-32000), message: "BadIndexes".into(), data: Some(data.clone()), }, id: Id::from(4), }; let json = r#" { "jsonrpc": "2.0", "error": { "code": -32000, "message": "BadIndexes", "data": [1,2,3] }, "id": 4 } "#; let json = json.replace("\n", "").replace(" ", ""); let result = serde_json::to_string(&example).unwrap(); assert_eq!(json, result); // This is how it is recommended you deserialize: let error: Error<serde_json::Value> = serde_json::from_str(&json).unwrap(); if error.error.code != ErrorCode::ServerError(-32000) { panic!("unexpected error"); } let result: Vec<u32> = serde_json::from_value( error.error.data.unwrap() ).unwrap(); assert_eq!(data, result);
Fields
jsonrpc: V2_0
Always "2.0"
error: ErrorObject<T>
The error object.
id: Id
The id of the request.
Methods
impl<T: Serialize + DeserializeOwned> Error<T>
[src]
pub fn new<C, S>(id: Id, code: C, message: S, data: Option<T>) -> Self where
C: Into<ErrorCode>,
S: Into<String>,
[src]
C: Into<ErrorCode>,
S: Into<String>,
Helper to create a new Error
object.
pub fn to_string(&self) -> String
[src]
Helper to serialize the Error as json.
pub fn from_str(s: &str) -> Result<T>
[src]
Helper to deserialize the Error from json.