[][src]Struct jrpc::Error

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]

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.

Trait Implementations

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

impl<T> Serialize for Error<T> where
    T: Serialize
[src]

impl<'de, T> Deserialize<'de> for Error<T> where
    T: Deserialize<'de>, 
[src]

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]