voa_openpgp/
error.rs

1//! Error handling.
2
3use std::path::PathBuf;
4
5use crate::OpenpgpSignature;
6
7/// The error that can occur when using OpenPGP verifiers in VOA.
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    /// An error occurred during import of an OpenPGP certificate.
11    #[error("Import error:\n{0}")]
12    Import(#[from] crate::import::error::Error),
13
14    /// An I/O error occurred at a path.
15    #[error("I/O error at path {path} while {context}:\n{source}")]
16    IoPath {
17        /// The path at which the error occurred.
18        path: PathBuf,
19
20        /// The context in which the error occurred.
21        ///
22        /// This is meant to complete the sentence "I/O error at path XXX while ...".
23        context: &'static str,
24
25        /// The source error.
26        source: std::io::Error,
27    },
28
29    /// An OpenPGP error occurred at a specific path.
30    #[error("OpenPGP error at path {path:?} while {context}:\n{source}")]
31    OpenPgpPath {
32        /// The path at which the error occurred.
33        path: PathBuf,
34
35        /// The context in which the error occurred.
36        ///
37        /// This is meant to complete the sentence "OpenPGP error at path {path} while ...".
38        context: &'static str,
39
40        /// The source error.
41        source: pgp::errors::Error,
42    },
43
44    /// An OpenPGP error occurred.
45    #[error("OpenPGP error while {context}:\n{source}")]
46    OpenPgp {
47        /// The context in which the error occurred.
48        ///
49        /// This is meant to complete the sentence "OpenPGP error while ...".
50        context: &'static str,
51
52        /// The source error.
53        source: pgp::errors::Error,
54    },
55
56    /// Encountered an OpenPGP fingerprint that is not supported in VOA.
57    ///
58    /// VOA currently supports v4 and v6 keys.
59    #[error("Unsupported OpenPGP fingerprint shape: '{fingerprint}'")]
60    UnsupportedOpenpgpFingerprint {
61        /// The string representation of the unsupported OpenPGP fingerprint.
62        fingerprint: String,
63    },
64
65    /// An VOA core error occurred.
66    #[error("VOA core error:\n{0}")]
67    VoaCore(#[from] voa_core::Error),
68
69    /// No applicable signature verification component key.
70    #[error("No applicable OpenPGP signature verification component key found in {fingerprint}")]
71    NoVerificationKey {
72        /// The fingerprint of the verifier in which the error occurred.
73        fingerprint: String,
74    },
75
76    /// The verification of a file failed.
77    #[error(
78        "Verification for file {path:?} failed with {num_successful_signatures}/{num_required_signatures} successful signature verifications and {} failed verifications for the signature(s): {}",
79        failed_signatures.len(),
80        failed_signatures
81            .iter()
82            .map(
83                |signature| {
84                let path = if let Some(path) = signature.source() {
85                    path.to_string_lossy().to_string()
86                } else {
87                    "-".to_string()
88                };
89                let issuer_fingerprints = signature
90                    .detached
91                    .signature
92                    .issuer_fingerprint()
93                    .iter()
94                    .map(|fingerprint| fingerprint.to_string())
95                    .collect::<Vec<_>>()
96                    .join(", ");
97                format!("{} {}", path, issuer_fingerprints)
98            })
99            .collect::<Vec<_>>()
100            .join("\n"),
101    )]
102    FileVerificationFailed {
103        /// The path to a file for which the verification failed.
104        path: PathBuf,
105        /// The number of required OpenPGP signatures.
106        num_required_signatures: usize,
107        /// The number of successful OpenPGP signatures.
108        num_successful_signatures: usize,
109        /// The list of failed OpenPGP signatures
110        failed_signatures: Vec<OpenpgpSignature>,
111    },
112
113    /// A verification method is not yet implemented.
114    #[error("The verification method {verification_method} is not yet implemented.")]
115    UnimplementedVerificationMethod {
116        /// The name of the verification method.
117        verification_method: String,
118    },
119}