Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ethexe/rpc/src/apis/injected/promise_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ethexe_common::{
},
};
use ethexe_db::Database;
use serde::{Deserialize, Serialize};
use std::{sync::Arc, time::Duration};
use tokio::sync::oneshot;
use tracing::trace;
Expand All @@ -45,7 +46,7 @@ pub struct PromiseSubscriptionManager {
waiting_for_compute: PromisesComputationWaiting,
}

#[derive(Debug, Clone, thiserror::Error)]
#[derive(Debug, Clone, thiserror::Error, Serialize, Deserialize)]
pub enum RegisterSubscriberError {
#[error("Subscriber for this transaction already exists, tx_hash={0}")]
AlreadyRegistered(HashOf<InjectedTransaction>),
Expand Down
2 changes: 1 addition & 1 deletion ethexe/rpc/src/apis/injected/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl TransactionsRelayer {
value = transaction.tx.data().value,
"Injected transaction with non-zero value is not supported"
);
return Err(errors::bad_request(
return Err(errors::invalid_params_with(
"Injected transactions with non-zero value are not supported",
));
}
Expand Down
4 changes: 2 additions & 2 deletions ethexe/rpc/src/apis/injected/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl InjectedApi {
let pending_subscriber = match self.manager.try_register_subscriber(tx_hash) {
Ok(subscriber) => subscriber,
Err(err) => {
return Err(errors::bad_request(err).into());
return Err(errors::bad_request(Some(err)).into());
}
};

Expand Down Expand Up @@ -181,7 +181,7 @@ impl InjectedApi {
tracing::trace!(?transaction_ids, "Called injected_getTransactions");

if transaction_ids.len() > MAX_TRANSACTION_IDS {
return Err(errors::invalid_params(format!(
return Err(errors::invalid_params_with(format!(
"Too many transaction ids requested. Maximum is {MAX_TRANSACTION_IDS}.",
)));
}
Expand Down
33 changes: 24 additions & 9 deletions ethexe/rpc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,41 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use jsonrpsee::types::{ErrorObject, error::INVALID_PARAMS_CODE};

// TODO #4364: https://github.com/gear-tech/gear/issues/4364
use jsonrpsee::types::{
ErrorObject,
error::{
CALL_EXECUTION_FAILED_CODE, INTERNAL_ERROR_CODE, INTERNAL_ERROR_MSG, INVALID_PARAMS_CODE,
INVALID_PARAMS_MSG, INVALID_REQUEST_CODE, INVALID_REQUEST_MSG,
},
};
use serde::Serialize;

// TODO: db errors are cause when we do not found some data in data, so maybe rename it to `not_found`.
pub fn db(err: &'static str) -> ErrorObject<'static> {
ErrorObject::owned(8000, "Database error", Some(err))
}

pub fn runtime(err: impl ToString) -> ErrorObject<'static> {
ErrorObject::owned(8000, "Runtime error", Some(err.to_string()))
ErrorObject::owned(
CALL_EXECUTION_FAILED_CODE,
"Runtime error",
Some(err.to_string()),
)
}

pub fn bad_request(err: impl ToString) -> ErrorObject<'static> {
ErrorObject::owned(8000, "Bad request", Some(err.to_string()))
pub fn bad_request<D: Serialize>(data: Option<D>) -> ErrorObject<'static> {
ErrorObject::owned(INVALID_REQUEST_CODE, INVALID_REQUEST_MSG, data)
}

pub fn internal() -> ErrorObject<'static> {
ErrorObject::owned(8000, "Internal error", None::<&str>)
ErrorObject::owned(INTERNAL_ERROR_CODE, INTERNAL_ERROR_MSG, None::<&str>)
}

#[allow(unused)]
pub fn invalid_params() -> ErrorObject<'static> {
ErrorObject::owned(INVALID_PARAMS_CODE, INVALID_PARAMS_MSG, None::<&str>)
}

pub fn invalid_params(err: impl ToString) -> ErrorObject<'static> {
ErrorObject::owned(INVALID_PARAMS_CODE, "Invalid params", Some(err.to_string()))
pub fn invalid_params_with<D: Serialize>(data: D) -> ErrorObject<'static> {
ErrorObject::owned(INVALID_PARAMS_CODE, INVALID_PARAMS_MSG, Some(data))
}