in_toto/
error.rs

1//! Error types and converters.
2
3use data_encoding::DecodeError;
4use std::io;
5use std::path::Path;
6use std::str;
7use thiserror::Error;
8
9/// Error type for all in-toto related errors.
10#[derive(Error, Debug, PartialEq, Eq)]
11pub enum Error {
12    /// The metadata had a bad signature.
13    #[error("bad signature")]
14    BadSignature,
15
16    /// There was a problem encoding or decoding.
17    #[error("encoding: {0}")]
18    Encoding(String),
19
20    /// An illegal argument was passed into a function.
21    #[error("illegal argument: {0}")]
22    IllegalArgument(String),
23
24    /// There were no available hash algorithms.
25    #[error("no supported hash algorithm")]
26    NoSupportedHashAlgorithm,
27
28    /// The metadata or target was not found.
29    #[error("not found")]
30    NotFound,
31
32    /// Opaque error type, to be interpreted similar to HTTP 500. Something went wrong, and you may
33    /// or may not be able to do anything about it.
34    #[error("opaque: {0}")]
35    Opaque(String),
36
37    /// There was a library internal error. These errors are *ALWAYS* bugs and should be reported.
38    #[error("programming: {0}")]
39    Programming(String),
40
41    /// The target is unavailable. This may mean it is either not in the metadata or the metadata
42    /// chain to the target cannot be fully verified.
43    #[error("target unavailable")]
44    TargetUnavailable,
45
46    /// There is no known or available hash algorithm.
47    #[error("unknown hash algorithm: {0}")]
48    UnknownHashAlgorithm(String),
49
50    /// There is no known or available key type.
51    #[error("unknown key type: {0}")]
52    UnknownKeyType(String),
53
54    /// The metadata or target failed to verify.
55    #[error("verification failure: {0}")]
56    VerificationFailure(String),
57
58    #[error("prefix selection failure: {0}")]
59    LinkGatheringError(String),
60
61    #[error("do Pre-Authentication Encoding failed: {0}")]
62    PAEParseFailed(String),
63
64    #[error("runlib failed: {0}")]
65    RunLibError(String),
66
67    #[error("attestation state and predicate version dismatch: {0} and {1}")]
68    AttestationFormatDismatch(String, String),
69
70    #[error("convertion from string failed: {0}")]
71    StringConvertFailed(String),
72
73    #[error("artifact rule error: {0}")]
74    ArtifactRuleError(String),
75}
76
77impl From<serde_json::error::Error> for Error {
78    fn from(err: serde_json::error::Error) -> Error {
79        Error::Encoding(format!("JSON: {:?}", err))
80    }
81}
82
83impl Error {
84    /// Helper to include the path that causd the error for FS I/O errors.
85    pub fn from_io(err: &io::Error, path: &Path) -> Error {
86        Error::Opaque(format!("Path {:?} : {:?}", path, err))
87    }
88}
89
90impl From<io::Error> for Error {
91    fn from(err: io::Error) -> Error {
92        match err.kind() {
93            std::io::ErrorKind::NotFound => Error::NotFound,
94            _ => Error::Opaque(format!("IO: {:?}", err)),
95        }
96    }
97}
98
99impl From<DecodeError> for Error {
100    fn from(err: DecodeError) -> Error {
101        Error::Encoding(format!("{:?}", err))
102    }
103}
104
105impl From<derp::Error> for Error {
106    fn from(err: derp::Error) -> Error {
107        Error::Encoding(format!("DER: {:?}", err))
108    }
109}
110
111impl From<str::Utf8Error> for Error {
112    fn from(err: str::Utf8Error) -> Error {
113        Error::Opaque(format!("Parse utf8: {:?}", err))
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn verify_io_error_display_string() {
123        let err = Error::from(io::Error::from(std::io::ErrorKind::NotFound));
124        assert_eq!(err.to_string(), "not found");
125        assert_eq!(Error::NotFound.to_string(), "not found");
126
127        let err =
128            Error::from(io::Error::from(std::io::ErrorKind::PermissionDenied));
129        assert_eq!(err.to_string(), "opaque: IO: Kind(PermissionDenied)");
130    }
131}