Skip to main content

ErrorObject

Struct ErrorObject 

Source
pub struct ErrorObject { /* private fields */ }
Expand description

The error member of a response: a code, a message, and optional data.

Handlers return one to fail a request; the crate builds one for every protocol error it answers itself; and a peer’s error response arrives as one. The message is kept owned (it is short, and an error usually outlives the message it was read from); data is any JSON value, kept as text.

§Examples

use jsonx_lib::json;
use rpc_io::{ErrorCode, ErrorObject};

let error = ErrorObject::new(ErrorCode(1001), "stale version")
    .with_data(json!({ "expected": 4 }));
assert_eq!(error.code(), ErrorCode(1001));
assert_eq!(error.message(), "stale version");
assert_eq!(error.data().map(|d| d.as_str()), Some(r#"{"expected":4}"#));

Implementations§

Source§

impl ErrorObject

Source

pub fn new(code: ErrorCode, message: impl Into<Cow<'static, str>>) -> Self

An error with code and message and no data.

§Examples
let e = rpc_io::ErrorObject::new(rpc_io::ErrorCode(7), "nope");
assert_eq!(e.to_string(), "nope (code 7)");
Source

pub fn with_data<'a>(self, data: impl Into<Json<'a>>) -> Self

Attaches data, any JSON value, replacing data set before.

§Examples
let e = rpc_io::ErrorObject::invalid_params("bad line").with_data(12);
assert_eq!(e.data().map(|d| d.as_str()), Some("12"));
Source

pub fn parse_error(message: impl Into<Cow<'static, str>>) -> Self

-32700 parse error with message.

§Examples
use rpc_io::{ErrorCode, ErrorObject};

assert_eq!(ErrorObject::parse_error("bad").code(), ErrorCode::PARSE_ERROR);
Source

pub fn invalid_request(message: impl Into<Cow<'static, str>>) -> Self

-32600 invalid request with message.

§Examples
use rpc_io::{ErrorCode, ErrorObject};

assert_eq!(ErrorObject::invalid_request("bad").code(), ErrorCode::INVALID_REQUEST);
Source

pub fn method_not_found(method: &str) -> Self

-32601 method not found, naming method.

§Examples
let e = rpc_io::ErrorObject::method_not_found("frob");
assert_eq!(e.message(), "method not found: `frob`");
Source

pub fn invalid_params(message: impl Into<Cow<'static, str>>) -> Self

-32602 invalid params with message.

§Examples
use rpc_io::{ErrorCode, ErrorObject};

let e = ErrorObject::invalid_params("`line` must be an integer");
assert_eq!(e.code(), ErrorCode::INVALID_PARAMS);
Source

pub fn internal_error(message: impl Into<Cow<'static, str>>) -> Self

-32603 internal error with message.

§Examples
use rpc_io::{ErrorCode, ErrorObject};

assert_eq!(ErrorObject::internal_error("x").code(), ErrorCode::INTERNAL_ERROR);
Source

pub const fn code(&self) -> ErrorCode

The code.

§Examples
assert_eq!(rpc_io::ErrorObject::new(rpc_io::ErrorCode(3), "m").code().0, 3);
Source

pub fn message(&self) -> &str

The message.

§Examples
assert_eq!(rpc_io::ErrorObject::internal_error("boom").message(), "boom");
Source

pub fn data(&self) -> Option<&Raw<'static>>

The data, if any, as JSON text.

§Examples
assert!(rpc_io::ErrorObject::internal_error("boom").data().is_none());

Trait Implementations§

Source§

impl Clone for ErrorObject

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ErrorObject

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ErrorObject

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

"<message> (code <code>)".

Source§

impl Eq for ErrorObject

Source§

impl Error for ErrorObject

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Error> for ErrorObject

Source§

fn from(error: Error) -> Self

An -32602 invalid-params error describing why the params did not parse, so a handler can apply ? to params.parse().

Source§

impl From<Error> for ErrorObject

Available on crate feature std only.
Source§

fn from(error: Error) -> Self

An -32603 internal error saying what failed, so a handler can apply ? to a send through its Context.

§Examples
use rpc_io::{Error, ErrorCode, ErrorObject};

let e = ErrorObject::from(Error::Closed);
assert_eq!(e.code(), ErrorCode::INTERNAL_ERROR);
assert_eq!(e.message(), "the connection is closed");
Source§

impl PartialEq for ErrorObject

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ErrorObject

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.