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

No easy way to create error without CallError #1115

Open
xlc opened this issue May 11, 2023 · 4 comments
Open

No easy way to create error without CallError #1115

xlc opened this issue May 11, 2023 · 4 comments

Comments

@xlc
Copy link
Contributor

xlc commented May 11, 2023

I am upgrading jsonrpsee and without CallError I just need to write lot more code to do the same.

For example, what's the best way for me to upgrade this code without writing extra helpers? I am expecting jsonrpsee providing helpers to generate standard error objects

https://github.com/AcalaNetwork/subway/blob/a6bbc19c3db67e21edde0fdf2837804928a81d22/src/middleware/inject_params.rs#L106-L113

@xlc
Copy link
Contributor Author

xlc commented May 11, 2023

I ended up with this helper: https://github.com/AcalaNetwork/subway/blob/2fc9a52f0404796d763f533ab802f3fea89fb35f/src/helper.rs

those should be provided by jsonrpsee

@niklasad1
Copy link
Member

The reason is that the old jsonrpsee CallError did too much magic (convert it back and forth from a jsonrpsee::Error) and we decided it's better to require folks explicitly decide what to return.

Because now it's possible to return custom error types that implements Into<ErrorObject>.
Thus, callbacks for methods and subscriptions shouldn't return jsonrpsee::core::Error anymore and we will most likely split that big error type.

Sure, I agree that we can expose invalid_params, failed and internal error helpers

@xlc
Copy link
Contributor Author

xlc commented May 11, 2023

I am fine with refactoring, just don't take away features. I don't want to manually construct ErrorObejct.

@mycognosist
Copy link

In case it's helpful to others making the upgrade to jsonrpsee, here's my error conversion implementation:

use jsonrpsee::types::error::ErrorObjectOwned as JsonRpcErrorOwned;
use jsonrpsee::types::error::SERVER_ERROR_MSG;

// Conversions for errors which occur in the context of a JSON-RPC method call.
// Crate-local error variants are converted to JSON-RPC errors which are
// then return to the caller.
impl From<Error> for JsonRpcErrorOwned {
    fn from(err: Error) -> Self {
        match &err {
            Error::SerdeJson(err_msg) => {
                JsonRpcErrorOwned::owned(-32000, SERVER_ERROR_MSG, Some(err_msg.to_string()))
            }
            Error::UrlParse(err_msg) => {
                JsonRpcErrorOwned::owned(-32001, SERVER_ERROR_MSG, Some(err_msg.to_string()))
            }
            Error::Validation(err_msg) => {
                JsonRpcErrorOwned::owned(-32002, SERVER_ERROR_MSG, Some(err_msg.to_string()))
            }
            _ => todo!(),
        }
    }
}

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

No branches or pull requests

3 participants