pub enum ErrorCode {
ParseError,
InvalidRequest,
MethodNotFound,
InvalidParams,
InternalError,
ServerError(i32),
}
Expand description
This enum
encapsulates JSON-RPC 2.0’s error codes
found in ErrorObject
.
It associates the code integer (i32
) with its defined message.
§Application defined errors
The custom error codes past -32099
(-31000, -31001
, …)
defined in JSON-RPC 2.0 are not supported by this enum because:
- The
(i32, &'static str)
required makes the enum more than 3x larger - It is not used by Cuprate/Monero1
§Display
use cuprate_json_rpc::error::ErrorCode;
use serde_json::{to_value, from_value, Value};
for e in [
ErrorCode::ParseError,
ErrorCode::InvalidRequest,
ErrorCode::MethodNotFound,
ErrorCode::InvalidParams,
ErrorCode::InternalError,
ErrorCode::ServerError(0),
] {
// The formatting is `$CODE: $MSG`.
let expected_fmt = format!("{}: {}", e.code(), e.msg());
assert_eq!(expected_fmt, format!("{e}"));
}
§(De)serialization
This type gets (de)serialized as the associated i32
, for example:
use cuprate_json_rpc::error::ErrorCode;
use serde_json::{to_value, from_value, Value};
for e in [
ErrorCode::ParseError,
ErrorCode::InvalidRequest,
ErrorCode::MethodNotFound,
ErrorCode::InvalidParams,
ErrorCode::InternalError,
ErrorCode::ServerError(0),
ErrorCode::ServerError(1),
ErrorCode::ServerError(2),
] {
// Gets serialized into a JSON integer.
let value = to_value(&e).unwrap();
assert_eq!(value, Value::Number(e.code().into()));
// Expects a JSON integer when deserializing.
assert_eq!(e, from_value(value).unwrap());
}
// A JSON string that contains an integer won't work.
from_value::<ErrorCode>("-32700".into()).unwrap();
Defined errors used by Monero (also excludes the last defined error
-32000 to -32099 Server error
): https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/contrib/epee/include/net/http_server_handlers_map2.h#L150 ↩
Variants§
ParseError
Invalid JSON was received by the server.
An error occurred on the server while parsing the JSON text.
InvalidRequest
The JSON sent is not a valid Request object.
MethodNotFound
The method does not exist / is not available.
InvalidParams
Invalid method parameters.
InternalError
Internal JSON-RPC error.
ServerError(i32)
Reserved for implementation-defined server-errors.
Implementations§
Source§impl ErrorCode
impl ErrorCode
Sourcepub const fn from_code(code: i32) -> Self
pub const fn from_code(code: i32) -> Self
From<i32>
is the same as this function.
use cuprate_json_rpc::error::{
ErrorCode,
INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, METHOD_NOT_FOUND, PARSE_ERROR,
};
assert_eq!(ErrorCode::from_code(PARSE_ERROR.0), ErrorCode::ParseError);
assert_eq!(ErrorCode::from_code(INVALID_REQUEST.0), ErrorCode::InvalidRequest);
assert_eq!(ErrorCode::from_code(METHOD_NOT_FOUND.0), ErrorCode::MethodNotFound);
assert_eq!(ErrorCode::from_code(INVALID_PARAMS.0), ErrorCode::InvalidParams);
assert_eq!(ErrorCode::from_code(INTERNAL_ERROR.0), ErrorCode::InternalError);
// Non-defined code inputs will default to a custom `ServerError`.
assert_eq!(ErrorCode::from_code(0), ErrorCode::ServerError(0));
assert_eq!(ErrorCode::from_code(1), ErrorCode::ServerError(1));
assert_eq!(ErrorCode::from_code(2), ErrorCode::ServerError(2));
Sourcepub const fn code(&self) -> i32
pub const fn code(&self) -> i32
Returns self
’s i32
code representation.
use cuprate_json_rpc::error::{
ErrorCode,
INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, METHOD_NOT_FOUND, PARSE_ERROR,
};
assert_eq!(ErrorCode::ParseError.code(), PARSE_ERROR.0);
assert_eq!(ErrorCode::InvalidRequest.code(), INVALID_REQUEST.0);
assert_eq!(ErrorCode::MethodNotFound.code(), METHOD_NOT_FOUND.0);
assert_eq!(ErrorCode::InvalidParams.code(), INVALID_PARAMS.0);
assert_eq!(ErrorCode::InternalError.code(), INTERNAL_ERROR.0);
assert_eq!(ErrorCode::ServerError(0).code(), 0);
assert_eq!(ErrorCode::ServerError(1).code(), 1);
Sourcepub const fn msg(&self) -> &'static str
pub const fn msg(&self) -> &'static str
Returns self
’s human readable str
message.
use cuprate_json_rpc::error::{
ErrorCode,
INTERNAL_ERROR, INVALID_PARAMS, INVALID_REQUEST, METHOD_NOT_FOUND, PARSE_ERROR, SERVER_ERROR,
};
assert_eq!(ErrorCode::ParseError.msg(), PARSE_ERROR.1);
assert_eq!(ErrorCode::InvalidRequest.msg(), INVALID_REQUEST.1);
assert_eq!(ErrorCode::MethodNotFound.msg(), METHOD_NOT_FOUND.1);
assert_eq!(ErrorCode::InvalidParams.msg(), INVALID_PARAMS.1);
assert_eq!(ErrorCode::InternalError.msg(), INTERNAL_ERROR.1);
assert_eq!(ErrorCode::ServerError(0).msg(), SERVER_ERROR);
Trait Implementations§
Source§impl<'a> Deserialize<'a> for ErrorCode
impl<'a> Deserialize<'a> for ErrorCode
Source§fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Error for ErrorCode
impl Error for ErrorCode
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<ErrorCode> for ErrorObject
impl From<ErrorCode> for ErrorObject
Source§impl Ord for ErrorCode
impl Ord for ErrorCode
Source§impl PartialOrd for ErrorCode
impl PartialOrd for ErrorCode
impl Copy for ErrorCode
impl Eq for ErrorCode
impl StructuralPartialEq for ErrorCode
Auto Trait Implementations§
impl Freeze for ErrorCode
impl RefUnwindSafe for ErrorCode
impl Send for ErrorCode
impl Sync for ErrorCode
impl Unpin for ErrorCode
impl UnwindSafe for ErrorCode
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: 8 bytes
Size for each variant:
ParseError
: 0 bytesInvalidRequest
: 0 bytesMethodNotFound
: 0 bytesInvalidParams
: 0 bytesInternalError
: 0 bytesServerError
: 4 bytes