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

Introduce dedicated error types to the lapin-futures crate #145

Merged
merged 5 commits into from
Nov 17, 2018

Conversation

kureuil
Copy link
Contributor

@kureuil kureuil commented Nov 4, 2018

This change replaces all occurrences of io::Error by the newly created
Error type, the only exception being for AMQPCodec. This change allows
future-proofing the error that can be returned by the crate. For example
by changing the internals of the Error type, it is now possible to
provide users with a way of knowing what kind of error occurred without
having to resort to parsing error messages.

The only exception to this change concerns AMQPCodec: a new CodecError type
was created just for it because tokio-codec's Encoder and Decoder traits
impose a From<std::io::Error> bound of the error type. Because I didn't
want to introduce a way to create an Error instance from outside the
crate, I isolated the possible errors from this section into its own
type.

}).and_then(|(client, heartbeat)| {
tokio::spawn(heartbeat.map_err(|e| eprintln!("heartbeat error: {:?}", e)))
.into_future().map(|_| client).map_err(|_| io::Error::new(io::ErrorKind::Other, "spawn error"))
.into_future().map(|_| client).map_err(|_| err_msg("spawn error"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the difference between err_msg and Error::new? I see the two being used in this patch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lapin_futures::Error can only be created from within the lapin_futures crate, by using the lapin_futures::Error::new function. This means that it can't be called in the examples.

err_msg is a function provided by the failure crate, creating a new failure::Error from a Displayable item.

futures/examples/topic.rs Show resolved Hide resolved
futures/src/channel.rs Show resolved Hide resolved
@@ -82,7 +84,7 @@ fn main() {
c.basic_ack(message.delivery_tag, false)
})
})
})
}).map_err(Error::from)
}).map_err(|err| eprintln!("error: {:?}", err))
Copy link
Collaborator

Choose a reason for hiding this comment

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

From what I understand, we could/should drop the :? in places like that, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According the the failure::Fail trait documentation, the Debug format is supposed to be "a verbose, developer-focused representation of the error", whereas the Display format is supposed to be "a user-friendly representation of the error", so it would make sense to switch from {:?} to {}, especially in the examples.

I'll change these in my next batch of commits.

let (consumed, f) = match parse_frame(buf) {
Err(e) => {
if e.is_incomplete() {
return Ok(None);
} else {
return Err(io::Error::new(io::ErrorKind::Other, format!("parse error: {:?}", e)));
return Err(CodecError::ParseError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we lose the error there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I'll add proper causes to the ParseError and GenerationError variants.

This change replaces all occurrences of io::Error by the newly created
Error type, the only exception being for AMQPCodec. This change allows
future-proofing the error that can be returned by the crate. For example
by changing the internals of the Error type, it is now possible to
provide users with a way of knowing what kind of error occurred without
having to resort to parsing error messages.

The only exception to this change concerns AMQPCodec: a new Error type
was created just for it because tokio-codec's Encoder and Decoder traits
impose a From<std::io::Error> bound of the error type. Because I didn't
want to introduce a way to create an Error instance from outside the
crate, I isolated the possible errors from this section into its own
type.
Errors are now printed using the `Display` formatter instead of the
`Debug` formatter.
@kureuil
Copy link
Contributor Author

kureuil commented Nov 17, 2018

@Keruspe rebased onto current master, awaiting for your review :)

@Keruspe
Copy link
Collaborator

Keruspe commented Nov 17, 2018

Thanks a lot for this. Always wanted to give failure a look and never had the occasion before

@Keruspe Keruspe merged commit b213113 into amqp-rs:master Nov 17, 2018
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.

None yet

2 participants