Skip to main content

tss_esapi/error/
wrapper.rs

1// Copyright 2022 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3/// List of error types that might occur in the wrapper.
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum WrapperErrorKind {
7    /// Returned when a size or length-defined parameter does not conform with the size
8    /// restrictions for it.
9    WrongParamSize,
10    /// Returned when a required parameter was not passed, usually to a builder.
11    ParamsMissing,
12    /// Returned when two or more parameters have inconsistent values or variants.
13    InconsistentParams,
14    /// Returned when the value of a parameter is not yet supported.
15    UnsupportedParam,
16    /// Returned when the value of a parameter is invalid for that type.
17    InvalidParam,
18    /// Returned when the TPM returns an invalid value from a call.
19    WrongValueFromTpm,
20    /// Returned when a session for authentication has not been set
21    /// before the call is made.
22    MissingAuthSession,
23    /// Returned when a handle is required to be in a specific state
24    /// (i.g. Open, Flushed, Closed) but it is not.
25    InvalidHandleState,
26    /// An unexpected internal error occurred.
27    InternalError,
28}
29
30impl std::fmt::Display for WrapperErrorKind {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            WrapperErrorKind::WrongParamSize => {
34                write!(f, "Parameter provided is of the wrong size.")
35            }
36            WrapperErrorKind::ParamsMissing => {
37                write!(f, "Some of the required parameters were not provided.")
38            }
39            WrapperErrorKind::InconsistentParams => write!(
40                f,
41                "The provided parameters have inconsistent values or variants."
42            ),
43            WrapperErrorKind::UnsupportedParam => write!(
44                f,
45                "The provided parameter is not yet supported by the library."
46            ),
47            WrapperErrorKind::InvalidParam => {
48                write!(f, "The provided parameter is invalid for that type.")
49            }
50            WrapperErrorKind::WrongValueFromTpm => write!(f, "The TPM returned an invalid value."),
51            WrapperErrorKind::MissingAuthSession => write!(f, "Missing authorization session."),
52            WrapperErrorKind::InvalidHandleState => write!(f, "Invalid handle state."),
53            WrapperErrorKind::InternalError => {
54                write!(f, "An unexpected error occurred within the crate.")
55            }
56        }
57    }
58}
59
60impl std::error::Error for WrapperErrorKind {}