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

Allow custom RPC method errors #973

Closed
MOZGIII opened this issue Jan 18, 2023 · 0 comments · Fixed by #977
Closed

Allow custom RPC method errors #973

MOZGIII opened this issue Jan 18, 2023 · 0 comments · Fixed by #977

Comments

@MOZGIII
Copy link
Contributor

MOZGIII commented Jan 18, 2023

I'd like to do something like this:

#[rpc(server, client, namespace = "myrpc")]
pub trait MyRpc {
    // an explicit error for this method
    #[method(name = "myMethod1")]
    async fn my_method1(&self) -> Result<MyData1, MyError1>;

    // an explicit error for this method
    #[method(name = "myMethod2")]
    async fn my_method2(&self) -> Result<MyData2, MyError2>;

    // backward-compatible generic error for this method
    #[method(name = "myMethod3")]
    async fn my_method3(&self) -> Result<MyData3, jsonrpsee::core::Error>;
}

pub enum MyError1 {
  // some variants here
}

pub enum MyError2 {
  // some variants here
}

impl From<MyError1> for jsonrpsee::core::Error {
    fn from(val: MyError1) -> jsonrpsee::core::Error {
        // my custom implementation for this method's errors
        jsonrpsee::core::Error::Other(ErrorObjectOwned { /*  ... */ }}
    }
}

impl From<MyError2> for jsonrpsee::core::Error {
    fn from(val: MyError2) -> jsonrpsee::core::Error {
        // my custom implementation for this method's errors
        jsonrpsee::core::Error::Other(ErrorObjectOwned { /*  ... */ }}
    }
}

The idea here is to allow methods to return an error type that can be converted into jsonrpsee::core::Error instead of requiring it explicitly.

This would allow us to have type-safe and clearly expressed application-level errors for the RPC calls - something we have had trouble with for quite some time.

We want to supply the data for certain errors, and our current implementation is a mess; people tend to manually construct the jsonrpsee::core::Error instead of going through the helper functions (which are not really discoverable, at least not in a way the type-safe errors enable).

Our frontend guys are also complaining it's hard to understand all possible application-level error cases as the code evolves, and we figured having an explicit error enum for every method would work very well for us.


This can be implemented in a backward-compatible way if the error is made to be impl Into<jsonrpsee::core::Error> instead of an explicit jsonrpsee::core::Error.

Alternatively, Into<jsonrpsee::types:error::ErrorObjectOwned> could be used - this is more direct, but doesn't allow invoking the baked-in errors, which is kind of a limitation.


This does not make much sense from the client-side perspective, so I propose the client trait to rewrite the errors to still be jsonrpsee::core::Error.

@MOZGIII MOZGIII mentioned this issue Jan 19, 2023
1 task
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