-
Notifications
You must be signed in to change notification settings - Fork 68
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
Conversation
There was a problem hiding this 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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/front/domain_socket.rs
Outdated
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 { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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"))?, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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!
src/providers/mbed_provider/utils.rs
Outdated
} | ||
KeyType::RsaKeypair => Ok(PSA_KEY_TYPE_RSA_KEYPAIR), | ||
KeyType::RsaPublicKey => Ok(PSA_KEY_TYPE_RSA_PUBLIC_KEY), | ||
_ => Err(ResponseStatus::PsaErrorInvalidArgument), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]>
58c8e26
to
1309a17
Compare
There was a problem hiding this 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! 👷
This commit looks at:
functions/macros and tries to replace most of them with returning errors
instead.
Exceptions are:
I took the liberty to align all the non-
ResponseStatus
errors tostd::io::Result
errors. I had to choose one of theErrorKind
variant and in some cases this choice is maybe bad. Feel free to propose changes. Usingstd::io::Result
makes it easy to integrate with other libraries that we use that also returnstd::io::Result
but we could also define our own.