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
impl ErrorObject
Sourcepub fn new(code: ErrorCode, message: impl Into<Cow<'static, str>>) -> Self
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)");Sourcepub fn with_data<'a>(self, data: impl Into<Json<'a>>) -> Self
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"));Sourcepub fn parse_error(message: impl Into<Cow<'static, str>>) -> Self
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);Sourcepub fn invalid_request(message: impl Into<Cow<'static, str>>) -> Self
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);Sourcepub fn method_not_found(method: &str) -> Self
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`");Sourcepub fn invalid_params(message: impl Into<Cow<'static, str>>) -> Self
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);Sourcepub fn internal_error(message: impl Into<Cow<'static, str>>) -> Self
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);Trait Implementations§
Source§impl Clone for ErrorObject
impl Clone for ErrorObject
Source§impl Debug for ErrorObject
impl Debug for ErrorObject
Source§impl Display for ErrorObject
impl Display for ErrorObject
impl Eq for ErrorObject
Source§impl Error for ErrorObject
impl Error for ErrorObject
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl From<Error> for ErrorObject
impl From<Error> for ErrorObject
Source§impl From<Error> for ErrorObject
Available on crate feature std only.
impl From<Error> for ErrorObject
Available on crate feature
std only.Source§impl PartialEq for ErrorObject
impl PartialEq for ErrorObject
impl StructuralPartialEq for ErrorObject
Auto Trait Implementations§
impl Freeze for ErrorObject
impl RefUnwindSafe for ErrorObject
impl Send for ErrorObject
impl Sync for ErrorObject
impl Unpin for ErrorObject
impl UnsafeUnpin for ErrorObject
impl UnwindSafe for ErrorObject
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more