1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
//! Error types for `yubihsm-connector`

#[cfg(feature = "http")]
use gaunt;
#[cfg(feature = "usb")]
use libusb;
use std::num::ParseIntError;
use std::str::Utf8Error;
use std::{fmt, io};

use crate::error::Error;

/// `yubihsm-connector` related errors
pub type ConnectionError = Error<ConnectionErrorKind>;

/// `yubihsm-connector` related error kinds
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ConnectionErrorKind {
    /// Address provided was not valid
    #[fail(display = "invalid address")]
    AddrInvalid,

    /// Access denied
    #[fail(display = "access denied")]
    AccessDenied,

    /// YubiHSM2 is busy (in use by another client / process)
    #[fail(display = "device already in use")]
    DeviceBusyError,

    /// Couldn't connect to the YubiHSM2
    #[fail(display = "connection failed")]
    ConnectionFailed,

    /// Input/output error
    #[fail(display = "I/O error")]
    IoError,

    /// Error making request
    #[fail(display = "invalid request")]
    RequestError,

    /// `yubihsm-connector` sent bad response
    #[fail(display = "bad connector response")]
    ResponseError,

    /// USB operation failed
    #[cfg(feature = "usb")]
    #[fail(display = "USB error")]
    UsbError,
}

impl From<fmt::Error> for ConnectionError {
    fn from(err: fmt::Error) -> Self {
        err!(ConnectionErrorKind::IoError, err.to_string())
    }
}

impl From<io::Error> for ConnectionError {
    fn from(err: io::Error) -> Self {
        err!(ConnectionErrorKind::IoError, err.to_string())
    }
}

#[cfg(feature = "http")]
impl From<gaunt::Error> for ConnectionError {
    fn from(err: gaunt::Error) -> ConnectionError {
        let kind = match err.kind() {
            gaunt::ErrorKind::AddrInvalid => ConnectionErrorKind::AddrInvalid,
            gaunt::ErrorKind::IoError => ConnectionErrorKind::IoError,
            gaunt::ErrorKind::ParseError | gaunt::ErrorKind::ResponseError => {
                ConnectionErrorKind::ResponseError
            }
            gaunt::ErrorKind::RequestError => ConnectionErrorKind::RequestError,
        };

        err!(kind, err)
    }
}

#[cfg(feature = "usb")]
impl From<libusb::Error> for ConnectionError {
    fn from(err: libusb::Error) -> ConnectionError {
        match err {
            libusb::Error::Access => err!(ConnectionErrorKind::AccessDenied, "{}", err),
            libusb::Error::Io => err!(ConnectionErrorKind::IoError, "{}", err),
            libusb::Error::Pipe => err!(
                ConnectionErrorKind::UsbError,
                "lost connection to USB device"
            ),
            _ => err!(ConnectionErrorKind::UsbError, "{}", err),
        }
    }
}

impl From<ParseIntError> for ConnectionError {
    fn from(err: ParseIntError) -> Self {
        err!(ConnectionErrorKind::ResponseError, err.to_string())
    }
}

impl From<Utf8Error> for ConnectionError {
    fn from(err: Utf8Error) -> Self {
        err!(ConnectionErrorKind::ResponseError, err.to_string())
    }
}