Enum cuprate_json_rpc::error::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 T)

🔬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