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

[servers] return error if context or params fails #295

Merged
merged 9 commits into from
May 4, 2021

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Apr 29, 2021

This PR changes the servers behavior when they get a response with params or user provided context that fails.
Instead of just terminating the connection we now return back a JSON-RPC error.

I changed the RpcMethod trait to take an generic Error instead such that we can configure it statically instead of matching of the entire enum when the is just one or two possible errors.

http-server/src/module.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 force-pushed the na-parse-rpc-params-fails-return-err branch from 0c88db6 to 0bfa1ff Compare April 29, 2021 15:25
ws-server/Cargo.toml Outdated Show resolved Hide resolved
http-server/src/module.rs Outdated Show resolved Hide resolved
http-server/src/tests.rs Show resolved Hide resolved
http-server/src/module.rs Outdated Show resolved Hide resolved
http-server/src/module.rs Outdated Show resolved Hide resolved
types/src/error.rs Outdated Show resolved Hide resolved
types/src/error.rs Outdated Show resolved Hide resolved
ws-server/src/tests.rs Show resolved Hide resolved
@@ -35,16 +38,17 @@ impl RpcModule {
pub fn register_method<F, R>(&mut self, method_name: &'static str, callback: F) -> Result<(), Error>
where
R: Serialize,
F: RpcMethod<R>,
F: RpcMethod<R, InvalidParams>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do something like dyn Error here instead? Or implement our own error trait that gives us the JSON-RPC error code along with the message?

Copy link
Member Author

@niklasad1 niklasad1 May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or implement our own error trait that gives us the JSON-RPC error code along with the message?

That sounds neat and I like, but it might be annoying users to explicitly implement that trait for the error types that they "plug-in" in Error::ContextFailed(user err)? However, it would be a way for the users to explicitly add there own error codes for this

For methods without context this would work very nicely.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create an issue for this and address it another PR

types/src/error.rs Show resolved Hide resolved
@@ -16,12 +16,36 @@ impl<T: fmt::Display> fmt::Display for Mismatch<T> {
}
}

/// Invalid params.
#[derive(Debug)]
pub struct InvalidParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we need this zst, i.e. why the ServerCallError::InvalidParams variant needs an unnamed field. I'll read more and perhaps I'll figure it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just that register_method_without_context can only fail when the params couldn't be parsed so it was introduced to avoid matching on unreachable enum variants

types/src/error.rs Show resolved Hide resolved
types/src/v2/error.rs Outdated Show resolved Hide resolved
types/src/error.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this in, just fix up the naming. Still a bit unsure about how the errors will work out in practice but this is better than what we had so let's merge it.

types/src/error.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 merged commit b51abec into master May 4, 2021
@niklasad1 niklasad1 deleted the na-parse-rpc-params-fails-return-err branch May 4, 2021 13:27
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 this pull request may close these issues.

4 participants