openpgp_card/
errors.rs

1// SPDX-FileCopyrightText: 2021 Heiko Schaefer <heiko@schaefer.name>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Error types used by this crate.
5//!
6//! [`Error`] is a wrapper enum for all error types that are used.
7//!
8//! The two main classes of errors are:
9//! - [`SmartcardError`], for problems on the reader/smartcard layer
10//! - [`StatusBytes`], which models error statuses reported by the OpenPGP
11//!   card application
12
13use card_backend::SmartcardError;
14
15use crate::ocard::StatusBytes;
16
17/// Enum wrapper for the error types of this crate
18#[derive(thiserror::Error, Debug)]
19#[non_exhaustive]
20pub enum Error {
21    #[error("Error interacting with smartcard: {0}")]
22    Smartcard(SmartcardError),
23
24    #[error("OpenPGP card error status: {0}")]
25    CardStatus(StatusBytes),
26
27    #[error("Command too long ({0} bytes)")]
28    CommandTooLong(usize),
29
30    #[error("Unexpected response length: {0}")]
31    ResponseLength(usize),
32
33    #[error("Data not found: {0}")]
34    NotFound(String),
35
36    #[error("Couldn't parse data: {0}")]
37    ParseError(String),
38
39    #[error("Unsupported algorithm: {0}")]
40    UnsupportedAlgo(String),
41
42    #[error("Unsupported feature: {0}")]
43    UnsupportedFeature(String),
44
45    // FIXME: placeholder, remove again later?
46    #[error("Internal error: {0}")]
47    InternalError(String),
48}
49
50impl From<StatusBytes> for Error {
51    fn from(oce: StatusBytes) -> Self {
52        Error::CardStatus(oce)
53    }
54}
55
56impl From<SmartcardError> for Error {
57    fn from(sce: SmartcardError) -> Self {
58        Error::Smartcard(sce)
59    }
60}