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

Refactoring: Use let-else for errors #14

Open
AlfioEmanueleFresta opened this issue Feb 27, 2022 · 0 comments
Open

Refactoring: Use let-else for errors #14

AlfioEmanueleFresta opened this issue Feb 27, 2022 · 0 comments
Labels
good first issue Good for newcomers

Comments

@AlfioEmanueleFresta
Copy link
Owner

AlfioEmanueleFresta commented Feb 27, 2022

Currently a lot of errors are handled in a manner similar to:

let dev = device.open_device(&hidapi)
                .or(Err(Error::Transport(TransportError::ConnectionFailed)))?;

These errors are tricky to track down. Now that we're using the tracing crate fully, it would be best to rewrite these using let-else syntax, ie.:

let Ok(dev) = device.open_device(&hidapi) else {
    error!(?hidapi, "Failed to open device");
    return Err(Error::Transport(TransportError::ConnectionFailed));
}

If the error is relevant to the message, first consider if the method being called should be instrumented to automatically log errors in their context:

use tracing::instrument;

#[instrument(skipall, err)]
pub fn open_device(&self, dev: &HidDevice) {
    ...

If this is not possible, we can instead use match statements at the point of invocation:

let dev = match device.open_device(&hidapi) {
    Ok(dev) => dev,
    Err(err) => {
        error!({ %err, ?hidapi }, "Failed to open device");
        return Err(Error::Transport(TransportError::ConnectionFailed));
    }
};
@AlfioEmanueleFresta AlfioEmanueleFresta added the good first issue Good for newcomers label Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant