summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-07-09 10:49:16 +0200
committerRobin Krahl <robin.krahl@ireas.org>2019-07-09 10:49:16 +0200
commit7ce751225f12c295c6e33dd46cfb5dcb88f8fbb3 (patch)
tree910e7fcae5b2b502900c67782b4db22d9c242f21 /src/error.rs
parent445e920986db276d0c5c39709aa76dd290773e8f (diff)
parent62e8ee8f5d02511d6eb5dc179b087b04e88c1b94 (diff)
downloadnitrokey-rs-7ce751225f12c295c6e33dd46cfb5dcb88f8fbb3.tar.gz
nitrokey-rs-7ce751225f12c295c6e33dd46cfb5dcb88f8fbb3.tar.bz2
Merge branch 'connection-manager-mut' into next
Refactor the connection management to prevent multiple device connections at the same time. RFC: https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C20190126174327.tbuyk2s535kfiqm4%40localhost%3E
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/error.rs b/src/error.rs
index 1730171..9e6adc0 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -5,6 +5,7 @@ use std::error;
use std::fmt;
use std::os::raw;
use std::str;
+use std::sync;
use crate::device;
@@ -13,11 +14,15 @@ use crate::device;
pub enum Error {
/// An error reported by the Nitrokey device in the response packet.
CommandError(CommandError),
- /// A device communication.
+ /// A device communication error.
CommunicationError(CommunicationError),
+ /// An error occurred due to concurrent access to the Nitrokey device.
+ ConcurrentAccessError,
/// A library usage error.
LibraryError(LibraryError),
- /// An error that occured during random number generation.
+ /// An error that occurred due to a poisoned lock.
+ PoisonError(sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>),
+ /// An error that occurred during random number generation.
RandError(Box<dyn error::Error>),
/// An error that is caused by an unexpected value returned by libnitrokey.
UnexpectedError,
@@ -65,7 +70,22 @@ impl From<str::Utf8Error> for Error {
}
}
-impl<T: device::Device> From<(T, Error)> for Error {
+impl From<sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>> for Error {
+ fn from(error: sync::PoisonError<sync::MutexGuard<'static, crate::Manager>>) -> Self {
+ Error::PoisonError(error)
+ }
+}
+
+impl From<sync::TryLockError<sync::MutexGuard<'static, crate::Manager>>> for Error {
+ fn from(error: sync::TryLockError<sync::MutexGuard<'static, crate::Manager>>) -> Self {
+ match error {
+ sync::TryLockError::Poisoned(err) => err.into(),
+ sync::TryLockError::WouldBlock => Error::ConcurrentAccessError,
+ }
+ }
+}
+
+impl<'a, T: device::Device<'a>> From<(T, Error)> for Error {
fn from((_, err): (T, Error)) -> Self {
err
}
@@ -76,7 +96,9 @@ impl error::Error for Error {
match *self {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(ref err) => Some(err),
+ Error::ConcurrentAccessError => None,
Error::LibraryError(ref err) => Some(err),
+ Error::PoisonError(ref err) => Some(err),
Error::RandError(ref err) => Some(err.as_ref()),
Error::UnexpectedError => None,
Error::UnknownError(_) => None,
@@ -90,7 +112,9 @@ impl fmt::Display for Error {
match *self {
Error::CommandError(ref err) => write!(f, "Command error: {}", err),
Error::CommunicationError(ref err) => write!(f, "Communication error: {}", err),
+ Error::ConcurrentAccessError => write!(f, "Internal error: concurrent access"),
Error::LibraryError(ref err) => write!(f, "Library error: {}", err),
+ Error::PoisonError(_) => write!(f, "Internal error: poisoned lock"),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
Error::UnexpectedError => write!(f, "An unexpected error occurred"),
Error::UnknownError(ref err) => write!(f, "Unknown error: {}", err),