cuprate_json_rpc::error

Enum ErrorCode

Source
pub enum ErrorCode {
    ParseError,
    InvalidRequest,
    MethodNotFound,
    InvalidParams,
    InternalError,
    ServerError(i32),
}
Expand description

Error object code.

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:

  1. The (i32, &'static str) required makes the enum more than 3x larger
  2. 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();

  1. 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

Source

pub const fn from_code(code: i32) -> Self

Creates Self from a i32 code.

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));
Source

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);
Source

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 Clone for ErrorCode

Source§

fn clone(&self) -> ErrorCode

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ErrorCode

Source§

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

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

impl<'a> Deserialize<'a> for ErrorCode

Source§

fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for ErrorCode

Source§

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

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

impl Error for ErrorCode

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<ErrorCode> for ErrorObject

Source§

fn from(code: ErrorCode) -> Self

Converts to this type from the input type.
Source§

impl<N: Into<i32>> From<N> for ErrorCode

Source§

fn from(code: N) -> Self

Converts to this type from the input type.
Source§

impl Hash for ErrorCode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for ErrorCode

Source§

fn cmp(&self, other: &ErrorCode) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ErrorCode

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ErrorCode

Source§

fn partial_cmp(&self, other: &ErrorCode) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for ErrorCode

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for ErrorCode

Source§

impl Eq for ErrorCode

Source§

impl StructuralPartialEq for ErrorCode

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> DeserializeOwned for T
where 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 bytes
  • InvalidRequest: 0 bytes
  • MethodNotFound: 0 bytes
  • InvalidParams: 0 bytes
  • InternalError: 0 bytes
  • ServerError: 4 bytes