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

Refactor RPC methods to use HexData and an error constructor #5548

Closed
arya2 opened this issue Nov 3, 2022 · 1 comment
Closed

Refactor RPC methods to use HexData and an error constructor #5548

arya2 opened this issue Nov 3, 2022 · 1 comment
Labels
A-rpc Area: Remote Procedure Call interfaces A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement

Comments

@arya2
Copy link
Contributor

arya2 commented Nov 3, 2022

Motivation

Multiple RPC methods are currently accepting hex-encoded data and similarly constructing ServerErrors.

Using the HexData type and a server error constructor will reduce repetition and improve maintainability.

Potentially something like:

{
    ...

    ready().await.map_server_error()?;

    get_height().ok_or_server_error("No blocks in state")?;
}

impl<T, E> MapServerError for Result<T, E> where E: Debug {
    fn map_server_error(result: Self) -> Result<T, jsonrpc_core::Error> {
        result.map_err(|error| jsonrpc_core::Error {
            code: ErrorCode::ServerError(0),
            message: format!("{error:?}"),
            data: None,
        })
    }
}

impl<T> OkOrServerError for Option<T> {
    fn ok_or_server_error<S: ToString>(option: Self, message: S) -> Result<T, jsonrpc_core::Error> {
        option.ok_or(jsonrpc_core::Error {
            code: ErrorCode::ServerError(0),
            message: message.to_string(),
            data: None,
        })
    }
}

Originally posted by Teor

@arya2 arya2 added A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement S-needs-triage Status: A bug report needs triage P-Optional ✨ A-rpc Area: Remote Procedure Call interfaces labels Nov 3, 2022
@teor2345
Copy link
Contributor

teor2345 commented Dec 1, 2022

I've added these as TODOs to the code instead, so we do them when we are rewriting particular RPCs.

@teor2345 teor2345 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2022
@mpguerra mpguerra removed the S-needs-triage Status: A bug report needs triage label Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rpc Area: Remote Procedure Call interfaces A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement
Projects
None yet
Development

No branches or pull requests

3 participants