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

Make error types serializable #2596

Closed
wants to merge 0 commits into from

Conversation

bddap
Copy link
Contributor

@bddap bddap commented Feb 18, 2019

This pr derives serialize and deserialize for public error types. This is needed for structured errors in networked apis.

Note 1

core::ser::Error required some extra attention as it includes an io::ErrorKind in it's definition. Removing io::ErrorKind would make obviate the need for manual serialization, but io::ErrorKind may be needed elsewhere.

The current solution works fine, but adds some boilerplate.

Note 2

This pr is blocked until secp is updated to 0.7.5. The code in this pr will not compile until 0.7.5 is published.

Once secp is updated, this pr will be ready to merge.

@antiochp
Copy link
Member

What would one of these errors look like serialized into json? Can you paste an example here (even a representative example).

I'm wondering how "rust specific" this is going to feel in terms of error handling on the other end.

@bddap
Copy link
Contributor Author

bddap commented Feb 19, 2019

Here is an example of a serialized grin_keychain::extkey_bip32::Error

{"InvalidChildNumber":{"Normal":{"index":10}}}

and here's a libwallet::ErrorKind

{"Keychain":{"KeyDerivation":{"InvalidChildNumber":{"Normal":{"index":10}}}}}
code
use grin_keychain;
use grin_libwallet;
use serde_json;
use std::io::stdout;

let example_err = grin_libwallet::ErrorKind::Keychain(grin_keychain::Error::KeyDerivation(
    grin_keychain::extkey_bip32::Error::InvalidChildNumber(
        grin_keychain::extkey_bip32::ChildNumber::Normal { index: 10 },
    ),
));
serde_json::to_writer(stdout(), &example_err).unwrap();

The serialized error is rusty, but definitely human readable, also parsable by non-rust clients.

Some nice things about sending serialized errors:

  • Network apis become isomorphic to native ones. For every method API::m(args..) -> R, a method NetworkAPI::m(args..) -> Future<Item=R, Error=NetworkError> can be generated.
  • Rust (and possibly non-rust) clients can be generated automatically.
  • Implementors of non-rust clients can use just the type definitions from rustdoc to parse structured errors.

@bddap
Copy link
Contributor Author

bddap commented Feb 19, 2019

Another thought: non-rust clients may simply ignore as much error structure as they want. They can even store errors as generic json blobs.

@bddap bddap force-pushed the milestone/1.1.0 branch 2 times, most recently from 97cf364 to 46bedc0 Compare February 25, 2019 22:16
@bddap
Copy link
Contributor Author

bddap commented Feb 25, 2019

Update:

@bddap bddap changed the title WIP make error types serializable Make error types serializable Feb 25, 2019
@bddap bddap mentioned this pull request Feb 26, 2019
9 tasks
@bddap bddap force-pushed the milestone/1.1.0 branch 3 times, most recently from dbd016e to d25f845 Compare March 5, 2019 00:12
@bddap bddap closed this Mar 7, 2019
@bddap bddap force-pushed the milestone/1.1.0 branch from d25f845 to fd077a4 Compare March 7, 2019 17:41
@bddap bddap deleted the milestone/1.1.0 branch March 7, 2019 17:44
@bddap bddap restored the milestone/1.1.0 branch March 7, 2019 17:45
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.

2 participants