replace RpcError with anyhow::Error
Some checks are pending
Audit / audit (push) Waiting to run
Deny / audit (push) Waiting to run

This commit is contained in:
hinto.janai 2024-09-06 18:06:50 -04:00
parent 1b94435952
commit 7e4015a381
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
8 changed files with 13 additions and 69 deletions

1
Cargo.lock generated
View file

@ -800,7 +800,6 @@ name = "cuprate-rpc-interface"
version = "0.0.0"
dependencies = [
"axum",
"cuprate-database",
"cuprate-epee-encoding",
"cuprate-helper",
"cuprate-json-rpc",

View file

@ -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 }

View file

@ -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 }

View file

@ -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.

View file

@ -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;

View file

@ -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<RpcError> for StatusCode {
fn from(_: RpcError) -> Self {
// TODO
Self::INTERNAL_SERVER_ERROR
}
}
impl From<RuntimeError> 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::*;
}

View file

@ -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.
///

View file

@ -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<Request, Response>:
+ Service<
Request,
Response = Response,
Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
Error = anyhow::Error,
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
>
{
}
@ -45,8 +43,8 @@ impl<Request, Response, T> RpcService<Request, Response> for T where
+ Service<
Request,
Response = Response,
Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
Error = anyhow::Error,
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
>
{
}