Skip to content

Commit

Permalink
chore: extract panic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jun 11, 2024
1 parent 98e2e33 commit 6ff1cc7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 45 deletions.
21 changes: 17 additions & 4 deletions core/src/proc_macros_support.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use jsonrpsee_types::ErrorObjectOwned;

#[cold]
pub fn log_fail_parse(arg_pat: &str, ty: &str, e: &dyn std::fmt::Debug, optional: bool) {
pub fn log_fail_parse(arg_pat: &str, ty: &str, err: &ErrorObjectOwned, optional: bool) {
let optional = if optional { "optional " } else { "" };
tracing::debug!("Error parsing {optional}\"{arg_pat}\" as \"{ty}\": {e:?}");
tracing::debug!("Error parsing {optional}\"{arg_pat}\" as \"{ty}\": {err}");
}

#[cold]
pub fn log_fail_parse_as_object(err: &ErrorObjectOwned) {
tracing::debug!("Failed to parse JSON-RPC params as object: {err}");
}

#[cold]
pub fn panic_fail_serialize(param: &str, err: serde_json::Error) -> ! {
panic!("Parameter `{param}` cannot be serialized: {err}");
}

#[cfg(debug_assertions)]
#[cold]
pub fn log_fail_parse_as_object(e: &dyn std::fmt::Display) {
tracing::debug!("Failed to parse JSON-RPC params as object: {e}");
pub fn panic_fail_register() -> ! {
panic!("RPC macro method names should never conflict. This is a bug, please report it.");
}
10 changes: 6 additions & 4 deletions proc-macros/src/render_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ impl RpcDescription {
let jsonrpsee = self.jsonrpsee_client_path.as_ref().unwrap();
let p = Ident::new(ILLEGAL_PARAM_NAME, proc_macro2::Span::call_site());

let reexports = self.jrps_client_item(quote! { core::__reexports });

if params.is_empty() {
return quote!({
#jsonrpsee::core::params::ArrayParams::new()
Expand Down Expand Up @@ -249,8 +251,8 @@ impl RpcDescription {
quote!({
let mut #p = #jsonrpsee::core::params::ObjectParams::new();
#(
if let Err(err) = #p.insert( #params_insert ) {
panic!("Parameter `{}` cannot be serialized: {:?}", stringify!( #params_insert ), err);
if let Err(err) = #p.insert(#params_insert) {
#reexports::panic_fail_serialize(stringify!(#params_insert), err);
}
)*
#p
Expand All @@ -263,8 +265,8 @@ impl RpcDescription {
quote!({
let mut #p = #jsonrpsee::core::params::ArrayParams::new();
#(
if let Err(err) = #p.insert( #params ) {
panic!("Parameter `{}` cannot be serialized: {:?}", stringify!( #params ), err);
if let Err(err) = #p.insert(#params) {
#reexports::panic_fail_serialize(stringify!(#params), err);
}
)*
#p
Expand Down
78 changes: 41 additions & 37 deletions proc-macros/src/render_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ impl RpcDescription {
})
}

/// Helper that will ignore results of `register_*` method calls, and panic if there have been
/// any errors in debug builds.
///
/// The debug assert is a safeguard should the contract that guarantees the method names to
/// never conflict in the macro be broken in the future.
fn handle_register_result(&self, tokens: TokenStream2) -> TokenStream2 {
let reexports = self.jrps_server_item(quote! { core::__reexports });
quote! {{
let _res = #tokens;
#[cfg(debug_assertions)]
if _res.is_err() {
#reexports::panic_fail_register();
}
}}
}

fn render_into_rpc(&self) -> Result<TokenStream2, syn::Error> {
let rpc_module = self.jrps_server_item(quote! { RpcModule });

Expand All @@ -121,18 +137,6 @@ impl RpcDescription {
}
};

/// Helper that will ignore results of `register_*` method calls, and panic
/// if there have been any errors in debug builds.
///
/// The debug assert is a safeguard should the contract that guarantees the method
/// names to never conflict in the macro be broken in the future.
fn handle_register_result(tokens: TokenStream2) -> TokenStream2 {
quote! {{
let res = #tokens;
debug_assert!(res.is_ok(), "RPC macro method names should never conflict, this is a bug, please report it.");
}}
}

let methods = self
.methods
.iter()
Expand All @@ -153,14 +157,14 @@ impl RpcDescription {

if method.signature.sig.asyncness.is_some() {
if method.with_extensions {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_async_method(#rpc_method_name, |params, context, ext| async move {
#parsing
#into_response::into_response(context.as_ref().#rust_method_name(&ext, #params_seq).await)
})
})
} else {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_async_method(#rpc_method_name, |params, context, _| async move {
#parsing
#into_response::into_response(context.as_ref().#rust_method_name(#params_seq).await)
Expand All @@ -172,14 +176,14 @@ impl RpcDescription {
if method.blocking { quote!(register_blocking_method) } else { quote!(register_method) };

if method.with_extensions {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.#register_kind(#rpc_method_name, |params, context, ext| {
#parsing
#into_response::into_response(context.#rust_method_name(&ext, #params_seq))
})
})
} else {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.#register_kind(#rpc_method_name, |params, context, _| {
#parsing
#into_response::into_response(context.#rust_method_name(#params_seq))
Expand Down Expand Up @@ -223,37 +227,37 @@ impl RpcDescription {

if sub.signature.sig.asyncness.is_some() {
if sub.with_extensions {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_subscription(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, ext| async move {
#parsing
#into_sub_response::into_response(context.as_ref().#rust_method_name(pending, &ext, #params_seq).await)
})
})
} else {
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_subscription(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, _| async move {
#parsing
#into_sub_response::into_response(context.as_ref().#rust_method_name(pending, #params_seq).await)
})
})
}
} else if sub.with_extensions {
handle_register_result(quote! {
rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, ext| {
#parsing
let _ = context.as_ref().#rust_method_name(pending, &ext, #params_seq);
#sub_err::None
})
self.handle_register_result(quote! {
rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, ext| {
#parsing
let _ = context.as_ref().#rust_method_name(pending, &ext, #params_seq);
#sub_err::None
})
})
} else {
handle_register_result(quote! {
rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, _| {
#parsing
let _ = context.as_ref().#rust_method_name(pending, #params_seq);
#sub_err::None
})
self.handle_register_result(quote! {
rpc.register_subscription_raw(#rpc_sub_name, #rpc_notif_name, #rpc_unsub_name, |params, mut pending, context, _| {
#parsing
let _ = context.as_ref().#rust_method_name(pending, #params_seq);
#sub_err::None
})
}
})
}
})
.collect::<Vec<_>>();

Expand All @@ -270,7 +274,7 @@ impl RpcDescription {
.iter()
.map(|alias| {
check_name(alias, rust_method_name.span());
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_alias(#alias, #rpc_name)
})
})
Expand All @@ -293,7 +297,7 @@ impl RpcDescription {
.iter()
.map(|alias| {
check_name(alias, rust_method_name.span());
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_alias(#alias, #sub_name)
})
})
Expand All @@ -303,7 +307,7 @@ impl RpcDescription {
.iter()
.map(|alias| {
check_name(alias, rust_method_name.span());
handle_register_result(quote! {
self.handle_register_result(quote! {
rpc.register_alias(#alias, #unsub_name)
})
})
Expand Down Expand Up @@ -353,15 +357,15 @@ impl RpcDescription {

let reexports = self.jrps_server_item(quote! { core::__reexports });

let sub_err = self.jrps_server_item(quote! { SubscriptionCloseResponse });
let response_payload = self.jrps_server_item(quote! { ResponsePayload });
let tokio = quote! { #reexports::tokio };
let error_ret = if let Some(pending) = &sub {
let tokio = quote! { #reexports::tokio };
let sub_err = self.jrps_server_item(quote! { SubscriptionCloseResponse });
quote! {
#tokio::spawn(#pending.reject(e));
return #sub_err::None;
}
} else {
let response_payload = self.jrps_server_item(quote! { ResponsePayload });
quote! {
return #response_payload::error(e);
}
Expand Down

0 comments on commit 6ff1cc7

Please sign in to comment.