rpc/interface: impl thiserror::Error

This commit is contained in:
hinto.janai 2024-09-05 18:06:22 -04:00
parent 7406afb910
commit d268c50246
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 35 additions and 12 deletions

2
Cargo.lock generated
View file

@ -800,6 +800,7 @@ 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",
@ -809,6 +810,7 @@ dependencies = [
"paste", "paste",
"serde", "serde",
"serde_json", "serde_json",
"thiserror",
"tokio", "tokio",
"tower", "tower",
"ureq", "ureq",

View file

@ -17,10 +17,12 @@ 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" }
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 }
thiserror = { workspace = true }
paste = { workspace = true } paste = { workspace = true }
futures = { workspace = true } futures = { workspace = true }

View file

@ -33,7 +33,7 @@ This crate simply handles:
- Defining handler function signatures - Defining handler function signatures
- (De)serialization of requests/responses (JSON-RPC, binary, JSON) - (De)serialization of requests/responses (JSON-RPC, binary, JSON)
The actual server details are all handled by the [`axum`] and [`tower`] ecosystem.a The actual server details are all handled by the [`axum`] and [`tower`] ecosystem.
The proper usage of this crate is to: The proper usage of this crate is to:
1. Implement a [`RpcHandler`] 1. Implement a [`RpcHandler`]
@ -45,8 +45,8 @@ 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 associated types are 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 from this crate: the 3 endpoint enums from [`cuprate_rpc_types`] and the error type is from this crate:
- [`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)

View file

@ -2,8 +2,8 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use axum::http::StatusCode; use axum::http::StatusCode;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use cuprate_database::RuntimeError;
//---------------------------------------------------------------------------------------------------- RpcError //---------------------------------------------------------------------------------------------------- RpcError
/// Possible errors during RPC operation. /// Possible errors during RPC operation.
@ -16,9 +16,16 @@ use serde::{Deserialize, Serialize};
/// ///
/// TODO: This is empty as possible errors will be /// TODO: This is empty as possible errors will be
/// enumerated when the handler functions are created. /// enumerated when the handler functions are created.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum RpcError {
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 { impl From<RpcError> for StatusCode {
fn from(_: RpcError) -> Self { fn from(_: RpcError) -> Self {
@ -27,6 +34,18 @@ impl From<RpcError> for StatusCode {
} }
} }
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 //---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View file

@ -32,7 +32,7 @@ pub trait RpcService<Request, Response>:
Request, Request,
Response = Response, Response = Response,
Error = RpcError, Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + Sync + 'static, Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
> >
{ {
} }
@ -46,7 +46,7 @@ impl<Request, Response, T> RpcService<Request, Response> for T where
Request, Request,
Response = Response, Response = Response,
Error = RpcError, Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + Sync + 'static, Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
> >
{ {
} }