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

Replace most panicking behaviours with Result #92

Merged
merged 1 commit into from
Jan 28, 2020

Conversation

hug-dev
Copy link
Member

@hug-dev hug-dev commented Jan 27, 2020

This commit looks at:

  • unwrap
  • expect
  • panic
  • unreachable
  • unimplemented
    functions/macros and tries to replace most of them with returning errors
    instead.

Exceptions are:

  • inside tests
  • when using with read/write locks or mutexes
  • env::var method
  • converting from u32 to usize
  • parsing Uuid

I took the liberty to align all the non-ResponseStatus errors to std::io::Result errors. I had to choose one of the ErrorKind variant and in some cases this choice is maybe bad. Feel free to propose changes. Using std::io::Result makes it easy to integrate with other libraries that we use that also return std::io::Result but we could also define our own.

@hug-dev hug-dev added the enhancement New feature or request label Jan 27, 2020
@hug-dev hug-dev added this to the Parsec production ready milestone Jan 27, 2020
@hug-dev hug-dev self-assigned this Jan 27, 2020
Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

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

Only the comment in mbed_provider/utils.rs is more pressing, the rest are observations

pub fn new(timeout: Duration) -> Self {
// If this PARSEC instance was socket activated (see the `parsec.socket`
pub fn new(timeout: Duration) -> Result<Self> {
// If this Parsec instance was socket activated (see the `parsec.socket`
Copy link
Member

Choose a reason for hiding this comment

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

👍

pub fn build(self) -> DomainSocketListener {
let timeout = self.timeout.expect("The listener timeout was not set");
pub fn build(self) -> Result<DomainSocketListener> {
let timeout = match self.timeout {
Copy link
Member

Choose a reason for hiding this comment

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

ok_or_else instead of match?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes good point, will change.

Ok(BackEndHandler {
provider: self
.provider
.ok_or_else(|| Error::new(ErrorKind::InvalidData, "provider is missing"))?,
Copy link
Member

Choose a reason for hiding this comment

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

If you don't also log something in the lambda, ok_or(Error::new(ErrorKind::InvalidData, "provider is missing"))? is simpler

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried that but clippy complains:

error: use of `ok_or` followed by a function call
   --> src/front/domain_socket.rs:148:48
    |
148 |           DomainSocketListener::new(self.timeout.ok_or(
    |  ________________________________________________^
149 | |             Error::new(
150 | |                 ErrorKind::InvalidInput,
151 | |                 "listener timeout missing",
152 | |                 )
153 | |         )?)
    | |_________^
    |
    = note: `-D clippy::or-fun-call` implied by `-D clippy::all`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
help: try this
    |
148 |         DomainSocketListener::new(self.timeout.ok_or_else(|| Error::new(
149 |                 ErrorKind::InvalidInput,
150 |                 "listener timeout missing",
151 |                 ))?)
    |

It also makes it easier to maintain and add a log in teh future :)

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, this makes sense! Thanks for the explanation!

}
KeyType::RsaKeypair => Ok(PSA_KEY_TYPE_RSA_KEYPAIR),
KeyType::RsaPublicKey => Ok(PSA_KEY_TYPE_RSA_PUBLIC_KEY),
_ => Err(ResponseStatus::PsaErrorInvalidArgument),
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this be UnsupportedParameters as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

True, will change.

This commit looks at:
* unwrap
* expect
* panic
* unreachable
* unimplemented
functions/macros and tries to replace most of them with returning errors
instead. Exceptions are:
* inside tests
* when using with read/write locks or mutexes
* env::var method
* converting from u32 to usize
* parsing Uuid

Signed-off-by: Hugues de Valon <[email protected]>
Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

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

Thanks for putting in the effort, Parsec'll be much more stable now! 👷

@hug-dev hug-dev merged commit 6a1cd93 into parallaxsecond:master Jan 28, 2020
@hug-dev hug-dev deleted the unsafe-unwrap branch January 28, 2020 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants