|
| 1 | +// Copyright 2019-2020 ChainX Project Authors. Licensed under GPL-3.0. |
| 2 | + |
| 3 | +use std::fmt::Debug; |
| 4 | + |
| 5 | +pub use jsonrpc_core::{Error, ErrorCode, Result}; |
| 6 | + |
| 7 | +/// The call to runtime failed. |
| 8 | +pub const RUNTIME_ERROR: i64 = 1; |
| 9 | + |
| 10 | +/// The call related to trustee to runtime failed. |
| 11 | +const RUNTIME_TRUSTEE_ERROR: i64 = RUNTIME_ERROR + 100; |
| 12 | + |
| 13 | +/// Decode the generic trustee info failed. |
| 14 | +/// |
| 15 | +/// TODO: these pallet-specific errors should be moved to its own rpc module |
| 16 | +/// when there are many of them. |
| 17 | +pub const RUNTIME_TRUSTEE_DECODE_ERROR: i64 = RUNTIME_TRUSTEE_ERROR + 1; |
| 18 | + |
| 19 | +/// The trustees are inexistent. |
| 20 | +pub const RUNTIME_TRUSTEE_INEXISTENT_ERROR: i64 = RUNTIME_TRUSTEE_ERROR + 2; |
| 21 | + |
| 22 | +/// The transaction was not decodable. |
| 23 | +pub const DECODE_ERROR: i64 = 10000; |
| 24 | + |
| 25 | +/// The bytes failed to be decoded as hex. |
| 26 | +pub const DECODE_HEX_ERROR: i64 = DECODE_ERROR + 1; |
| 27 | + |
| 28 | +/// Converts a runtime trap into an RPC error. |
| 29 | +pub fn runtime_error_into_rpc_err(err: impl Debug) -> Error { |
| 30 | + Error { |
| 31 | + code: ErrorCode::ServerError(RUNTIME_ERROR), |
| 32 | + message: "Runtime trapped".into(), |
| 33 | + data: Some(format!("{:?}", err).into()), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Converts a trustee runtime trap into an RPC error. |
| 38 | +pub fn trustee_decode_error_into_rpc_err(err: impl Debug) -> Error { |
| 39 | + Error { |
| 40 | + code: ErrorCode::ServerError(RUNTIME_TRUSTEE_DECODE_ERROR), |
| 41 | + message: "Can not decode generic trustee session info".into(), |
| 42 | + data: Some(format!("{:?}", err).into()), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Converts a trustee runtime trap into an RPC error. |
| 47 | +pub fn trustee_inexistent_rpc_err() -> Error { |
| 48 | + Error { |
| 49 | + code: ErrorCode::ServerError(RUNTIME_TRUSTEE_INEXISTENT_ERROR), |
| 50 | + message: "Trustee does not exist".into(), |
| 51 | + data: None, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Converts a hex decode error into an RPC error. |
| 56 | +pub fn hex_decode_error_into_rpc_err(err: impl Debug) -> Error { |
| 57 | + Error { |
| 58 | + code: ErrorCode::ServerError(DECODE_HEX_ERROR), |
| 59 | + message: "Failed to decode hex".into(), |
| 60 | + data: Some(format!("{:?}", err).into()), |
| 61 | + } |
| 62 | +} |
0 commit comments