diff --git a/Cargo.lock b/Cargo.lock index 426fced1..3ca5d6af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -800,7 +800,6 @@ name = "cuprate-rpc-interface" version = "0.0.0" dependencies = [ "axum", - "cuprate-database", "cuprate-epee-encoding", "cuprate-helper", "cuprate-json-rpc", diff --git a/Cargo.toml b/Cargo.toml index 8704cb78..2d718936 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ opt-level = 1 opt-level = 3 [workspace.dependencies] +anyhow = { version = "1.0.87", default-features = false } async-trait = { version = "0.1.74", default-features = false } bitflags = { version = "2.4.2", default-features = false } borsh = { version = "1.2.1", default-features = false } diff --git a/rpc/interface/Cargo.toml b/rpc/interface/Cargo.toml index 56f3fd49..feb1affb 100644 --- a/rpc/interface/Cargo.toml +++ b/rpc/interface/Cargo.toml @@ -17,8 +17,8 @@ cuprate-epee-encoding = { path = "../../net/epee-encoding", default-features = f cuprate-json-rpc = { path = "../json-rpc", default-features = false } cuprate-rpc-types = { path = "../types", features = ["serde", "epee"], default-features = false } cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false } -cuprate-database = { path = "../../storage/database" } +anyhow = { workspace = true } axum = { version = "0.7.5", features = ["json"], default-features = false } serde = { workspace = true, optional = true } tower = { workspace = true } diff --git a/rpc/interface/README.md b/rpc/interface/README.md index eb878643..fa5496c1 100644 --- a/rpc/interface/README.md +++ b/rpc/interface/README.md @@ -45,15 +45,16 @@ The proper usage of this crate is to: This is your [`tower::Service`] that converts `Request`s into `Response`s, i.e. the "inner handler". -Said concretely, `RpcHandler` is 3 `tower::Service`s where the request/response types are -the 3 endpoint enums from [`cuprate_rpc_types`] and the error type is from this crate: +Said concretely, `RpcHandler` is 3 `tower::Service`s where the +request/response types are the 3 endpoint enums from [`cuprate_rpc_types`]: - [`JsonRpcRequest`](cuprate_rpc_types::json::JsonRpcRequest) & [`JsonRpcResponse`](cuprate_rpc_types::json::JsonRpcResponse) - [`BinRequest`](cuprate_rpc_types::bin::BinRequest) & [`BinResponse`](cuprate_rpc_types::bin::BinRequest) - [`OtherRequest`](cuprate_rpc_types::other::OtherRequest) & [`OtherResponse`](cuprate_rpc_types::other::OtherRequest) -- [`RpcError`] `RpcHandler`'s [`Future`](std::future::Future) is generic, _although_, -it must output `Result<$RESPONSE, RpcError>`. +it must output `Result<$RESPONSE, anyhow::Error>`. + +The error type must always be [`anyhow::Error`]. The `RpcHandler` must also hold some state that is required for RPC server operation. diff --git a/rpc/interface/src/lib.rs b/rpc/interface/src/lib.rs index ebea4939..1f84738e 100644 --- a/rpc/interface/src/lib.rs +++ b/rpc/interface/src/lib.rs @@ -3,14 +3,12 @@ mod route; mod router_builder; -mod rpc_error; mod rpc_handler; #[cfg(feature = "dummy")] mod rpc_handler_dummy; mod rpc_service; pub use router_builder::RouterBuilder; -pub use rpc_error::RpcError; pub use rpc_handler::RpcHandler; #[cfg(feature = "dummy")] pub use rpc_handler_dummy::RpcHandlerDummy; diff --git a/rpc/interface/src/rpc_error.rs b/rpc/interface/src/rpc_error.rs deleted file mode 100644 index 59f371ab..00000000 --- a/rpc/interface/src/rpc_error.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! RPC errors. - -//---------------------------------------------------------------------------------------------------- Import -use axum::http::StatusCode; - -use cuprate_database::RuntimeError; - -//---------------------------------------------------------------------------------------------------- RpcError -/// Possible errors during RPC operation. -/// -/// These are any errors that can happen _during_ a handler function. -/// I.e. if this error surfaces, it happened _after_ the request was -/// deserialized. -/// -/// This is the `Error` type required to be used in an [`RpcHandler`](crate::RpcHandler). -/// -/// TODO: This is empty as possible errors will be -/// enumerated when the handler functions are created. -#[derive(Debug, thiserror::Error)] -pub enum RpcError { - /// A [`std::io::Error`] from the database. - #[error("database I/O error: {0}")] - DatabaseIo(#[from] std::io::Error), - - /// A (non-I/O related) database error. - #[error("database error: {0}")] - DatabaseError(RuntimeError), -} - -impl From for StatusCode { - fn from(_: RpcError) -> Self { - // TODO - Self::INTERNAL_SERVER_ERROR - } -} - -impl From for RpcError { - fn from(error: RuntimeError) -> Self { - match error { - RuntimeError::Io(io) => Self::DatabaseIo(io), - RuntimeError::KeyExists - | RuntimeError::KeyNotFound - | RuntimeError::ResizeNeeded - | RuntimeError::TableNotFound => Self::DatabaseError(error), - } - } -} - -//---------------------------------------------------------------------------------------------------- Tests -#[cfg(test)] -mod test { - // use super::*; -} diff --git a/rpc/interface/src/rpc_handler.rs b/rpc/interface/src/rpc_handler.rs index 1299ec48..1d2676c7 100644 --- a/rpc/interface/src/rpc_handler.rs +++ b/rpc/interface/src/rpc_handler.rs @@ -22,7 +22,7 @@ use crate::RpcService; /// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times, /// one for each request/response enum type found in [`cuprate_rpc_types`]. /// -/// The error type must always be [`RpcError`](crate::RpcError). +/// The error type must always be [`anyhow::Error`]. /// /// See this crate's `RpcHandlerDummy` for an implementation example of this trait. /// diff --git a/rpc/interface/src/rpc_service.rs b/rpc/interface/src/rpc_service.rs index b85ac047..285d60ba 100644 --- a/rpc/interface/src/rpc_service.rs +++ b/rpc/interface/src/rpc_service.rs @@ -5,8 +5,6 @@ use std::future::Future; use tower::Service; -use crate::rpc_error::RpcError; - //---------------------------------------------------------------------------------------------------- RpcService /// An RPC [`tower::Service`]. /// @@ -17,7 +15,7 @@ use crate::rpc_error::RpcError; /// The `Request` and `Response` are generic and /// are used in the [`tower::Service`] bounds. /// -/// The error type is always [`RpcError`]. +/// The error type is always [`anyhow::Error`]. /// /// There is a blanket implementation that implements this /// trait on types that implement `tower::Service` correctly. @@ -31,8 +29,8 @@ pub trait RpcService: + Service< Request, Response = Response, - Error = RpcError, - Future: Future> + Send + 'static, + Error = anyhow::Error, + Future: Future> + Send + 'static, > { } @@ -45,8 +43,8 @@ impl RpcService for T where + Service< Request, Response = Response, - Error = RpcError, - Future: Future> + Send + 'static, + Error = anyhow::Error, + Future: Future> + Send + 'static, > { }