From 44eceec0cda2fa20a7584edad54043dc870eb982 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Tue, 9 Jun 2020 10:38:29 +0200 Subject: [PATCH 1/5] Improve rpc error display. --- client/rpc-api/src/author/error.rs | 11 +++++++---- client/transaction-pool/src/api.rs | 2 +- primitives/api/src/lib.rs | 4 ++-- primitives/runtime/src/transaction_validity.rs | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/client/rpc-api/src/author/error.rs b/client/rpc-api/src/author/error.rs index e6ee36cdce19a..0a0815c45d190 100644 --- a/client/rpc-api/src/author/error.rs +++ b/client/rpc-api/src/author/error.rs @@ -114,10 +114,13 @@ impl From for rpc::Error { message: format!("Verification Error: {}", e).into(), data: Some(format!("{:?}", e).into()), }, - Error::Pool(PoolError::InvalidTransaction(e)) => rpc::Error { - code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), - message: "Invalid Transaction".into(), - data: serde_json::to_value(e).ok(), + Error::Pool(PoolError::InvalidTransaction(e)) => { + let msg: &str = e.into(); + rpc::Error { + code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), + message: format!("Invalid Transaction: {}", msg).into(), + data: serde_json::to_value(e).ok(), + } }, Error::Pool(PoolError::UnknownTransaction(e)) => rpc::Error { code: rpc::ErrorCode::ServerError(POOL_UNKNOWN_VALIDITY), diff --git a/client/transaction-pool/src/api.rs b/client/transaction-pool/src/api.rs index 79315c2724b5b..b87fda5e97ecf 100644 --- a/client/transaction-pool/src/api.rs +++ b/client/transaction-pool/src/api.rs @@ -105,7 +105,7 @@ where #[allow(deprecated)] // old validate_transaction runtime_api.validate_transaction_before_version_2(&at, uxt) }; - let res = res.map_err(|e| Error::RuntimeApi(format!("{:?}", e))); + let res = res.map_err(|e| Error::RuntimeApi(format!("{}", e))); if let Err(e) = tx.send(res) { log::warn!("Unable to send a validate transaction result: {:?}", e); } diff --git a/primitives/api/src/lib.rs b/primitives/api/src/lib.rs index ec15c1eae71d0..8e57b660f61f2 100644 --- a/primitives/api/src/lib.rs +++ b/primitives/api/src/lib.rs @@ -344,7 +344,7 @@ pub trait ConstructRuntimeApi> { #[cfg(feature = "std")] pub trait ApiErrorExt { /// Error type used by the runtime apis. - type Error: std::fmt::Debug + From; + type Error: std::fmt::Debug + std::fmt::Display + From; } /// Extends the runtime api implementation with some common functionality. @@ -453,7 +453,7 @@ pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend { /// Error type used by the implementation. - type Error: std::fmt::Debug + From; + type Error: std::fmt::Debug + std::fmt::Display + From; /// The state backend that is used to store the block states. type StateBackend: StateBackend>; diff --git a/primitives/runtime/src/transaction_validity.rs b/primitives/runtime/src/transaction_validity.rs index fc2465a068fe3..1aad9e75aec34 100644 --- a/primitives/runtime/src/transaction_validity.rs +++ b/primitives/runtime/src/transaction_validity.rs @@ -104,7 +104,7 @@ impl From for &'static str { InvalidTransaction::BadMandatory => "A call was labelled as mandatory, but resulted in an Error.", InvalidTransaction::MandatoryDispatch => - "Tranaction dispatch is mandatory; transactions may not have mandatory dispatches.", + "Transaction dispatch is mandatory; transactions may not have mandatory dispatches.", InvalidTransaction::Custom(_) => "InvalidTransaction custom error", } } From cffb7eb1482d8b88214e9a749e528441f9afcf20 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Tue, 9 Jun 2020 13:38:54 +0200 Subject: [PATCH 2/5] Apply review suggestion. --- client/transaction-pool/src/api.rs | 4 ++-- primitives/api/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/transaction-pool/src/api.rs b/client/transaction-pool/src/api.rs index b87fda5e97ecf..c7665022a5623 100644 --- a/client/transaction-pool/src/api.rs +++ b/client/transaction-pool/src/api.rs @@ -64,7 +64,7 @@ where Client: ProvideRuntimeApi + BlockBackend + BlockIdTo, Client: Send + Sync + 'static, Client::Api: TaggedTransactionQueue, - sp_api::ApiErrorFor: Send, + sp_api::ApiErrorFor: Send + std::fmt::Display, { type Block = Block; type Error = error::Error; @@ -105,7 +105,7 @@ where #[allow(deprecated)] // old validate_transaction runtime_api.validate_transaction_before_version_2(&at, uxt) }; - let res = res.map_err(|e| Error::RuntimeApi(format!("{}", e))); + let res = res.map_err(|e| Error::RuntimeApi(e.to_string())); if let Err(e) = tx.send(res) { log::warn!("Unable to send a validate transaction result: {:?}", e); } diff --git a/primitives/api/src/lib.rs b/primitives/api/src/lib.rs index 8e57b660f61f2..ec15c1eae71d0 100644 --- a/primitives/api/src/lib.rs +++ b/primitives/api/src/lib.rs @@ -344,7 +344,7 @@ pub trait ConstructRuntimeApi> { #[cfg(feature = "std")] pub trait ApiErrorExt { /// Error type used by the runtime apis. - type Error: std::fmt::Debug + std::fmt::Display + From; + type Error: std::fmt::Debug + From; } /// Extends the runtime api implementation with some common functionality. @@ -453,7 +453,7 @@ pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend { /// Error type used by the implementation. - type Error: std::fmt::Debug + std::fmt::Display + From; + type Error: std::fmt::Debug + From; /// The state backend that is used to store the block states. type StateBackend: StateBackend>; From fc9f2e816fd06cbdc2bbd2b4c4bf5f88b2854040 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Tue, 9 Jun 2020 17:29:42 +0200 Subject: [PATCH 3/5] Apply review suggestion. --- client/rpc-api/src/author/error.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/client/rpc-api/src/author/error.rs b/client/rpc-api/src/author/error.rs index 0a0815c45d190..d741cbb8cd483 100644 --- a/client/rpc-api/src/author/error.rs +++ b/client/rpc-api/src/author/error.rs @@ -20,6 +20,7 @@ use crate::errors; use jsonrpc_core as rpc; +use sp_runtime::transaction_validity::InvalidTransaction; /// Author RPC Result type. pub type Result = std::result::Result; @@ -114,12 +115,17 @@ impl From for rpc::Error { message: format!("Verification Error: {}", e).into(), data: Some(format!("{:?}", e).into()), }, + Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => rpc::Error { + code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), + message: "Invalid Transaction".into(), + data: Some(e.into()), + }, Error::Pool(PoolError::InvalidTransaction(e)) => { let msg: &str = e.into(); rpc::Error { code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), - message: format!("Invalid Transaction: {}", msg).into(), - data: serde_json::to_value(e).ok(), + message: "Invalid Transaction".into(), + data: Some(msg.into()), } }, Error::Pool(PoolError::UnknownTransaction(e)) => rpc::Error { From 6f153c56adc9b683f2f49b88a08cc1d6e9e3ee09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 9 Jun 2020 17:35:24 +0200 Subject: [PATCH 4/5] Update client/rpc-api/src/author/error.rs --- client/rpc-api/src/author/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc-api/src/author/error.rs b/client/rpc-api/src/author/error.rs index d741cbb8cd483..d07ddbdcbf455 100644 --- a/client/rpc-api/src/author/error.rs +++ b/client/rpc-api/src/author/error.rs @@ -118,7 +118,7 @@ impl From for rpc::Error { Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => rpc::Error { code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), message: "Invalid Transaction".into(), - data: Some(e.into()), + data: Some(format!("Custom error: {}", e)), }, Error::Pool(PoolError::InvalidTransaction(e)) => { let msg: &str = e.into(); From 67674e08ec8e153923ca1538f218b4b2ef953920 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Tue, 9 Jun 2020 18:37:14 +0200 Subject: [PATCH 5/5] Fix custom. --- client/rpc-api/src/author/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc-api/src/author/error.rs b/client/rpc-api/src/author/error.rs index d07ddbdcbf455..69c036be95fe0 100644 --- a/client/rpc-api/src/author/error.rs +++ b/client/rpc-api/src/author/error.rs @@ -118,7 +118,7 @@ impl From for rpc::Error { Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => rpc::Error { code: rpc::ErrorCode::ServerError(POOL_INVALID_TX), message: "Invalid Transaction".into(), - data: Some(format!("Custom error: {}", e)), + data: Some(format!("Custom error: {}", e).into()), }, Error::Pool(PoolError::InvalidTransaction(e)) => { let msg: &str = e.into();