pub struct ErrorObject {
pub code: ErrorCode,
pub message: Cow<'static, str>,
pub data: Option<Value>,
}
Expand description
This is the object sent back in a Response
if the method call errored.
§Display
use cuprate_json_rpc::error::ErrorObject;
// The format is `$CODE: $MESSAGE`.
// If a message was not passed during construction,
// the error code's message will be used.
assert_eq!(format!("{}", ErrorObject::parse_error()), "-32700: Parse error");
assert_eq!(format!("{}", ErrorObject::invalid_request()), "-32600: Invalid Request");
assert_eq!(format!("{}", ErrorObject::method_not_found()), "-32601: Method not found");
assert_eq!(format!("{}", ErrorObject::invalid_params()), "-32602: Invalid params");
assert_eq!(format!("{}", ErrorObject::internal_error()), "-32603: Internal error");
assert_eq!(format!("{}", ErrorObject::server_error(0)), "0: Server error");
// Set a custom message.
let mut e = ErrorObject::server_error(1);
e.message = "hello".into();
assert_eq!(format!("{e}"), "1: hello");
Fields§
§code: ErrorCode
The error code.
message: Cow<'static, str>
A custom message for this error, distinct from ErrorCode::msg
.
A JSON string
value.
This is a Cow<'static, str>
to support both 0-allocation for
const
string ID’s commonly found in programs, as well as support
for runtime String
’s.
data: Option<Value>
Optional data associated with the error.
§None
vs Some(Value::Null)
This field will be completely omitted during serialization if None
,
however if it is Some(Value::Null)
, it will be serialized as "data": null
.
Implementations§
Source§impl ErrorObject
impl ErrorObject
Sourcepub const fn from_code(code: ErrorCode) -> Self
pub const fn from_code(code: ErrorCode) -> Self
Creates a new error, deriving the message from the code.
Same as ErrorObject::from(ErrorCode)
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
for code in [
ErrorCode::ParseError,
ErrorCode::InvalidRequest,
ErrorCode::MethodNotFound,
ErrorCode::InvalidParams,
ErrorCode::InternalError,
ErrorCode::ServerError(0),
] {
let object = ErrorObject::from_code(code);
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
}
Sourcepub const fn parse_error() -> Self
pub const fn parse_error() -> Self
Creates a new error using PARSE_ERROR
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::ParseError;
let object = ErrorObject::parse_error();
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Sourcepub const fn invalid_request() -> Self
pub const fn invalid_request() -> Self
Creates a new error using INVALID_REQUEST
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::InvalidRequest;
let object = ErrorObject::invalid_request();
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Sourcepub const fn method_not_found() -> Self
pub const fn method_not_found() -> Self
Creates a new error using METHOD_NOT_FOUND
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::MethodNotFound;
let object = ErrorObject::method_not_found();
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Sourcepub const fn invalid_params() -> Self
pub const fn invalid_params() -> Self
Creates a new error using INVALID_PARAMS
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::InvalidParams;
let object = ErrorObject::invalid_params();
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Sourcepub const fn internal_error() -> Self
pub const fn internal_error() -> Self
Creates a new error using INTERNAL_ERROR
.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::InternalError;
let object = ErrorObject::internal_error();
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Sourcepub const fn server_error(error_code: i32) -> Self
pub const fn server_error(error_code: i32) -> Self
Creates a new error using SERVER_ERROR
.
You must provide the custom i32
error code.
use std::borrow::Cow;
use cuprate_json_rpc::error::{ErrorCode, ErrorObject};
let code = ErrorCode::ServerError(0);
let object = ErrorObject::server_error(0);
assert_eq!(object, ErrorObject {
code,
message: Cow::Borrowed(code.msg()),
data: None,
});
Trait Implementations§
Source§impl Clone for ErrorObject
impl Clone for ErrorObject
Source§fn clone(&self) -> ErrorObject
fn clone(&self) -> ErrorObject
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ErrorObject
impl Debug for ErrorObject
Source§impl<'de> Deserialize<'de> for ErrorObject
impl<'de> Deserialize<'de> for ErrorObject
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for ErrorObject
impl Display for ErrorObject
Source§impl Error for ErrorObject
impl Error for ErrorObject
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<ErrorCode> for ErrorObject
impl From<ErrorCode> for ErrorObject
Source§impl PartialEq for ErrorObject
impl PartialEq for ErrorObject
Source§impl Serialize for ErrorObject
impl Serialize for ErrorObject
impl Eq 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 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 64 bytes