mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-31 14:55:55 +00:00
replace RpcError
with anyhow::Error
This commit is contained in:
parent
1b94435952
commit
7e4015a381
8 changed files with 13 additions and 69 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -800,7 +800,6 @@ name = "cuprate-rpc-interface"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"cuprate-database",
|
|
||||||
"cuprate-epee-encoding",
|
"cuprate-epee-encoding",
|
||||||
"cuprate-helper",
|
"cuprate-helper",
|
||||||
"cuprate-json-rpc",
|
"cuprate-json-rpc",
|
||||||
|
|
|
@ -48,6 +48,7 @@ opt-level = 1
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
anyhow = { version = "1.0.87", default-features = false }
|
||||||
async-trait = { version = "0.1.74", default-features = false }
|
async-trait = { version = "0.1.74", default-features = false }
|
||||||
bitflags = { version = "2.4.2", default-features = false }
|
bitflags = { version = "2.4.2", default-features = false }
|
||||||
borsh = { version = "1.2.1", default-features = false }
|
borsh = { version = "1.2.1", default-features = false }
|
||||||
|
|
|
@ -17,8 +17,8 @@ cuprate-epee-encoding = { path = "../../net/epee-encoding", default-features = f
|
||||||
cuprate-json-rpc = { path = "../json-rpc", default-features = false }
|
cuprate-json-rpc = { path = "../json-rpc", default-features = false }
|
||||||
cuprate-rpc-types = { path = "../types", features = ["serde", "epee"], default-features = false }
|
cuprate-rpc-types = { path = "../types", features = ["serde", "epee"], default-features = false }
|
||||||
cuprate-helper = { path = "../../helper", features = ["asynch"], 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 }
|
axum = { version = "0.7.5", features = ["json"], default-features = false }
|
||||||
serde = { workspace = true, optional = true }
|
serde = { workspace = true, optional = true }
|
||||||
tower = { workspace = true }
|
tower = { workspace = true }
|
||||||
|
|
|
@ -45,15 +45,16 @@ The proper usage of this crate is to:
|
||||||
This is your [`tower::Service`] that converts `Request`s into `Response`s,
|
This is your [`tower::Service`] that converts `Request`s into `Response`s,
|
||||||
i.e. the "inner handler".
|
i.e. the "inner handler".
|
||||||
|
|
||||||
Said concretely, `RpcHandler` is 3 `tower::Service`s where the request/response types are
|
Said concretely, `RpcHandler` is 3 `tower::Service`s where the
|
||||||
the 3 endpoint enums from [`cuprate_rpc_types`] and the error type is from this crate:
|
request/response types are the 3 endpoint enums from [`cuprate_rpc_types`]:
|
||||||
- [`JsonRpcRequest`](cuprate_rpc_types::json::JsonRpcRequest) & [`JsonRpcResponse`](cuprate_rpc_types::json::JsonRpcResponse)
|
- [`JsonRpcRequest`](cuprate_rpc_types::json::JsonRpcRequest) & [`JsonRpcResponse`](cuprate_rpc_types::json::JsonRpcResponse)
|
||||||
- [`BinRequest`](cuprate_rpc_types::bin::BinRequest) & [`BinResponse`](cuprate_rpc_types::bin::BinRequest)
|
- [`BinRequest`](cuprate_rpc_types::bin::BinRequest) & [`BinResponse`](cuprate_rpc_types::bin::BinRequest)
|
||||||
- [`OtherRequest`](cuprate_rpc_types::other::OtherRequest) & [`OtherResponse`](cuprate_rpc_types::other::OtherRequest)
|
- [`OtherRequest`](cuprate_rpc_types::other::OtherRequest) & [`OtherResponse`](cuprate_rpc_types::other::OtherRequest)
|
||||||
- [`RpcError`]
|
|
||||||
|
|
||||||
`RpcHandler`'s [`Future`](std::future::Future) is generic, _although_,
|
`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
|
The `RpcHandler` must also hold some state that is required
|
||||||
for RPC server operation.
|
for RPC server operation.
|
||||||
|
|
|
@ -3,14 +3,12 @@
|
||||||
|
|
||||||
mod route;
|
mod route;
|
||||||
mod router_builder;
|
mod router_builder;
|
||||||
mod rpc_error;
|
|
||||||
mod rpc_handler;
|
mod rpc_handler;
|
||||||
#[cfg(feature = "dummy")]
|
#[cfg(feature = "dummy")]
|
||||||
mod rpc_handler_dummy;
|
mod rpc_handler_dummy;
|
||||||
mod rpc_service;
|
mod rpc_service;
|
||||||
|
|
||||||
pub use router_builder::RouterBuilder;
|
pub use router_builder::RouterBuilder;
|
||||||
pub use rpc_error::RpcError;
|
|
||||||
pub use rpc_handler::RpcHandler;
|
pub use rpc_handler::RpcHandler;
|
||||||
#[cfg(feature = "dummy")]
|
#[cfg(feature = "dummy")]
|
||||||
pub use rpc_handler_dummy::RpcHandlerDummy;
|
pub use rpc_handler_dummy::RpcHandlerDummy;
|
||||||
|
|
|
@ -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::*;
|
|
||||||
}
|
|
|
@ -22,7 +22,7 @@ use crate::RpcService;
|
||||||
/// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times,
|
/// 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`].
|
/// 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.
|
/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
|
||||||
///
|
///
|
||||||
|
|
|
@ -5,8 +5,6 @@ use std::future::Future;
|
||||||
|
|
||||||
use tower::Service;
|
use tower::Service;
|
||||||
|
|
||||||
use crate::rpc_error::RpcError;
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- RpcService
|
//---------------------------------------------------------------------------------------------------- RpcService
|
||||||
/// An RPC [`tower::Service`].
|
/// An RPC [`tower::Service`].
|
||||||
///
|
///
|
||||||
|
@ -17,7 +15,7 @@ use crate::rpc_error::RpcError;
|
||||||
/// The `Request` and `Response` are generic and
|
/// The `Request` and `Response` are generic and
|
||||||
/// are used in the [`tower::Service`] bounds.
|
/// 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
|
/// There is a blanket implementation that implements this
|
||||||
/// trait on types that implement `tower::Service` correctly.
|
/// trait on types that implement `tower::Service` correctly.
|
||||||
|
@ -31,8 +29,8 @@ pub trait RpcService<Request, Response>:
|
||||||
+ Service<
|
+ Service<
|
||||||
Request,
|
Request,
|
||||||
Response = Response,
|
Response = Response,
|
||||||
Error = RpcError,
|
Error = anyhow::Error,
|
||||||
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
|
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -45,8 +43,8 @@ impl<Request, Response, T> RpcService<Request, Response> for T where
|
||||||
+ Service<
|
+ Service<
|
||||||
Request,
|
Request,
|
||||||
Response = Response,
|
Response = Response,
|
||||||
Error = RpcError,
|
Error = anyhow::Error,
|
||||||
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
|
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue