ledger_lib/
error.rs

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
//! Ledger interface [Error] type and conversions

use ledger_proto::{ApduError, StatusCode};

/// Ledger interface error type
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[cfg(feature = "transport_usb")]
    #[error(transparent)]
    Hid(#[from] hidapi::HidError),

    #[cfg(feature = "transport_tcp")]
    #[error(transparent)]
    Tcp(#[from] tokio::io::Error),

    #[cfg(feature = "transport_ble")]
    #[error(transparent)]
    Ble(#[from] btleplug::Error),

    #[error("Unknown ledger model: {0}")]
    UnknownModel(u16),

    #[error("Unknown error")]
    Unknown,

    #[error("No devices found")]
    NoDevices,

    #[error("Invalid device index: {0}")]
    InvalidDeviceIndex(usize),

    #[error("Apdu encode/decode error: {0}")]
    Apdu(#[from] ApduError),

    /// Recognised status codes (see [StatusCode])
    #[error("Status: {0}")]
    Status(StatusCode),

    /// Unrecognised status codes
    #[error("Status: 0x{0:02x}{1:02x} (unrecognised)")]
    UnknownStatus(u8, u8),

    #[error("Request timeout")]
    Timeout,

    #[error("Device or transport closed")]
    Closed,

    #[error("Empty response payload")]
    EmptyResponse,

    #[error("Unexpected response payload")]
    UnexpectedResponse,

    #[error("Device in use")]
    DeviceInUse,

    #[error("Already running application ({0})")]
    ApplicationLoaded(String),
}

impl From<tokio::time::error::Elapsed> for Error {
    fn from(_e: tokio::time::error::Elapsed) -> Self {
        Self::Timeout
    }
}