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

libserialize: add new error() to Decoder for Decodable objects to signal parse errors #16130

Merged
merged 2 commits into from
Aug 1, 2014

Conversation

apoelstra
Copy link
Contributor

A quick and dirty fix for #15036 until we get serious decoder reform.

Right now it is impossible for a Decodable to signal a decode error, for example if it has only finitely many allowed values, is a string which must be encoded a certain way, needs a valid checksum, etc. For example in the libuuid implementation of Decodable an Option is unwrapped, meaning that a decode of a malformed UUID will cause the task to fail.

@brson
Copy link
Contributor

brson commented Jul 31, 2014

cc @erickt

@apoelstra
Copy link
Contributor Author

I will push a second commit which fixes libuuid and adds a unit test, just testing now..success.

@@ -163,6 +163,9 @@ pub trait Decoder<E> {
fn read_map<T>(&mut self, f: |&mut Self, uint| -> Result<T, E>) -> Result<T, E>;
fn read_map_elt_key<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;
fn read_map_elt_val<T>(&mut self, idx: uint, f: |&mut Self| -> Result<T, E>) -> Result<T, E>;

// Failure
fn fail_here<T>(&mut self, err: &str) -> Result<T, E>;
Copy link
Member

Choose a reason for hiding this comment

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

I would have thought that this should be something along the lines of:

fn error(&mut self, err: &str) -> E;

Generating a Result isn't necessarily what this is doing, and fail may mislead some into thinking that it invokes fail!()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about read_error? And agreed that it should return an E, Result is a confusing signature.

Edit: On second thought I agree error is good. I'll update the PR with that.

@alexcrichton
Copy link
Member

Could you also amend the first commit message in accordance with our breaking changes policy? Adding a new trait method will break all existing implementations.

cc @aturon, @huonw, this seems like a reasonable strategy for #15036 for now.

@apoelstra apoelstra changed the title libserialize: add fail_here() to Decoder libserialize: add new error() to Decoder for Decodable objects to signal parse errors Jul 31, 2014
@aturon
Copy link
Member

aturon commented Jul 31, 2014

@alexcrichton I'm fine with this solution for now.

@erickt
Copy link
Contributor

erickt commented Jul 31, 2014

I'm already using this pattern in rust-serde, so this appoach looks fine to me. Can you change https://github.com/rust-lang/rust/blob/master/src/libserialize/json.rs#L296 to use this error as well? It's not perfect, but I guess we could use the OtherIoError error if we can't convert the buffer into a string.

A word a warning though, you're going to hit a merge conflict once #16107 lands.

@apoelstra
Copy link
Contributor Author

Hi @erickt, right now the new error function is only in Decoder as an effort to minimize changes. Should I add it to Encoder as well so that I can change that Json encoder line?

@erickt
Copy link
Contributor

erickt commented Jul 31, 2014

@apoelstra: you are right, let's save that for a future pr.

A quick and dirty fix for rust-lang#15036 until we get serious decoder reform.

Right now it is impossible for a Decodable to signal a decode error,
for example if it has only finitely many allowed values, is a string
which must be encoded a certain way, needs a valid checksum, etc. For
example in the libuuid implementation of Decodable an Option is
unwrapped, meaning that a decode of a malformed UUID will cause the
task to fail.

Since this adds a method to the `Decoder` trait, all users will need
to update their implementations to add it. The strategy used for the
current implementations for JSON and EBML is to add a new entry to
the error enum `ApplicationError(String)` which stores the string
provided to `.error()`.

[breaking-change]
@apoelstra
Copy link
Contributor Author

Rebase for EBML's move to librbml

bors added a commit that referenced this pull request Aug 1, 2014
A quick and dirty fix for #15036 until we get serious decoder reform.

Right now it is impossible for a `Decodable` to signal a decode error, for example if it has only finitely many allowed values, is a string which must be encoded a certain way, needs a valid checksum, etc. For example in the `libuuid` implementation of `Decodable` an `Option` is unwrapped, meaning that a decode of a malformed UUID will cause the task to fail.
@bors bors closed this Aug 1, 2014
@bors bors merged commit dac9a1c into rust-lang:master Aug 1, 2014
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.

6 participants