Skip to content

Commit

Permalink
return parameter deserialisation errors (close #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Jun 24, 2024
1 parent e72078a commit 1fb61ac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/return-param-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"qubit": patch:feat
"qubit-macros": patch:feat
---

return parameter deserialising errors if they are encountered (close #69)
8 changes: 6 additions & 2 deletions crates/qubit-macros/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ impl From<Handler> for TokenStream {
// Generate the parameter parsing implementation
let parse_params = (!inputs.is_empty()).then(|| {
quote! {
let (#(#param_names,)*) = #params_ident.parse::<(#(#param_tys,)*)>().unwrap();
let (#(#param_names,)*) = match #params_ident.parse::<(#(#param_tys,)*)>() {
::std::result::Result::Ok(params) => params,
::std::result::Result::Err(e) => return ::std::result::Result::Err(e),
};
}
});

Expand All @@ -251,7 +254,8 @@ impl From<Handler> for TokenStream {
let register_inner = quote! {
#parse_params

#handler_call
let result = #handler_call;
::std::result::Result::Ok::<_, ::qubit::ErrorObject>(result)
};

let register_method = match kind {
Expand Down
3 changes: 3 additions & 0 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pub use handler::Handler;
pub(crate) use handler::HandlerCallbacks;
pub use rpc_builder::RpcBuilder;
pub use ty::*;

pub use jsonrpsee::types::ErrorObject;
pub use jsonrpsee::IntoResponse;
17 changes: 10 additions & 7 deletions src/builder/rpc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::ops::Deref;

use futures::{Future, Stream, StreamExt};
use jsonrpsee::{
types::{ErrorCode, Params, ResponsePayload},
RpcModule, SubscriptionCloseResponse, SubscriptionMessage,
types::{ErrorCode, ErrorObject, Params, ResponsePayload},
IntoResponse, RpcModule, SubscriptionCloseResponse, SubscriptionMessage,
};
use serde::Serialize;
use serde::{de::Error, Serialize};
use serde_json::json;

use crate::{FromRequestExtensions, RequestKind, RpcError};
Expand Down Expand Up @@ -50,7 +50,7 @@ where
T: Serialize + Clone + 'static,
C: FromRequestExtensions<Ctx>,
F: Fn(C, Params<'static>) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = T> + Send + 'static,
Fut: Future<Output = Result<T, ErrorObject<'static>>> + Send + 'static,
{
self.register_handler(name, handler, RequestKind::Query)
}
Expand All @@ -61,7 +61,7 @@ where
T: Serialize + Clone + 'static,
C: FromRequestExtensions<Ctx>,
F: Fn(C, Params<'static>) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = T> + Send + 'static,
Fut: Future<Output = Result<T, ErrorObject<'static>>> + Send + 'static,
{
self.register_handler(name, handler, RequestKind::Mutation)
}
Expand All @@ -78,7 +78,7 @@ where
T: Serialize + Clone + 'static,
C: FromRequestExtensions<Ctx>,
F: Fn(C, Params<'static>) -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = T> + Send + 'static,
Fut: Future<Output = Result<T, jsonrpsee::types::ErrorObject<'static>>> + Send + 'static,
{
self.module
.register_async_method(self.namespace_str(name), move |params, ctx, extensions| {
Expand Down Expand Up @@ -114,7 +114,10 @@ where
};

// Run the actual handler
ResponsePayload::success(handler(ctx, params).await)
match handler(ctx, params).await {
Ok(result) => ResponsePayload::success(result),
Err(e) => ResponsePayload::error(e),
}
}
})
.unwrap();
Expand Down

0 comments on commit 1fb61ac

Please sign in to comment.