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

Always "2.0"

The error object.

The id of the request.

Methods

impl<T: Serialize + DeserializeOwned> Error<T>
[src]

[src]

Helper to create a new Error object.

[src]

Helper to serialize the Error as json.

[src]

Helper to deserialize the Error from json.

Trait Implementations

impl<T: Debug> Debug for Error<T>
[src]

[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<T> Send for Error<T> where
    T: Send

impl<T> Sync for Error<T> where
    T: Sync