Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc macros: IntoResponse doesn't work for parameters that isn't Result #1093

Closed
niklasad1 opened this issue Apr 23, 2023 · 0 comments · Fixed by #1094
Closed

proc macros: IntoResponse doesn't work for parameters that isn't Result #1093

niklasad1 opened this issue Apr 23, 2023 · 0 comments · Fixed by #1094

Comments

@niklasad1
Copy link
Member

#1057 introduced new a trait for method call responses however there is a an edge-case where the parameter parsing in the proc macro code assumes it can't return Err(e.into) this doesn't work for a type which isn't a Result and the removal of CallError/jsonrpsee::core::Error needs an extra From impl.

These needs to be fixed before releasing in v0.18 and ideally the params parsing could just return PartialResponse directly as handled for subscriptions as well.

Example with the custom error type

pub struct MyError;

impl From<MyError> for ErrorObjectOwned {
	fn from(_: MyError) -> Self {
		ErrorObject::owned(1, "my_error", None::<()>)
	}
}

// This is annoying and shouldn't be needed..
impl From<ErrorObjectOwned> for MyError {
	fn from(_: ErrorObjectOwned) -> MyError {
		MyError
	}
}

#[rpc(server, client, namespace = "t")]
pub trait Api {
	/// Async method call example.
	#[method(name = "getKeys")]
	async fn f(&self, x: usize) -> Result<usize, MyError>;
}

Example with the custom return type

// Serialize impl is not used as the responses are sent out as error.
#[derive(Serialize, Clone)]
pub enum CustomError {
	One,
	Two { custom_data: u32 },
}

impl IntoResponse for CustomError {
	type Output = Self;

	fn into_response(self) -> ResponsePayload<'static, Self::Output> {
           todo!()
        }
	
}

#[rpc(server)]
pub trait Rpc {
        // This won't compile as it would return `Result<usize, ErrorObject>` when parsing `x` in the call
	#[method(name = "method1")]
	async fn method1(&self, x: usize) -> CustomError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant