[−][src]Struct jrpc::Error
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.
Trait Implementations
impl<T: Debug> Debug for Error<T>
[src]
impl<T> Serialize for Error<T> where
T: Serialize,
[src]
T: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de, T> Deserialize<'de> for Error<T> where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
impl<T> Send for Error<T> where
T: Send,
T: Send,
impl<T> Sync for Error<T> where
T: Sync,
T: Sync,
impl<T> Unpin for Error<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Error<T> where
T: UnwindSafe,
T: UnwindSafe,
impl<T> RefUnwindSafe for Error<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,