Skip to main content

ndn_protocol/
error.rs

1//! Error types returned by this crate's fallible operations.
2
3use ndn_tlv::TlvError;
4use thiserror::Error;
5
6/// The `Result` type used throughout this crate, with the error type fixed to [`NdnError`].
7pub type Result<T> = std::result::Result<T, NdnError>;
8
9/// The general-purpose error type for this crate.
10#[derive(Error, Debug)]
11pub enum NdnError {
12    /// A [`Name`](crate::Name) URI could not be parsed.
13    #[error("Parse error")]
14    ParseError,
15    /// Signature or parameter digest verification failed.
16    #[error("Failed to verify")]
17    VerifyError(VerifyError),
18    /// The underlying TLV data was malformed.
19    #[error("TLV Error: {0}")]
20    TlvError(TlvError),
21    /// A catch-all for errors that don't fit the other variants.
22    #[error("{0}")]
23    GenericError(String),
24    /// An I/O operation failed, e.g. while reading a certificate file.
25    #[error("IO Error: {0}")]
26    IOError(std::io::Error),
27}
28
29impl From<url::ParseError> for NdnError {
30    fn from(_value: url::ParseError) -> Self {
31        NdnError::ParseError
32    }
33}
34
35impl From<TlvError> for NdnError {
36    fn from(value: TlvError) -> Self {
37        NdnError::TlvError(value)
38    }
39}
40
41impl From<std::io::Error> for NdnError {
42    fn from(value: std::io::Error) -> Self {
43        NdnError::IOError(value)
44    }
45}
46
47/// Errors that can occur while signing an [`Interest`](crate::Interest).
48#[derive(Error, Debug)]
49pub enum SignError {
50    /// The interest has no application parameters to sign; set some before
51    /// calling `sign` (see
52    /// [`Interest::set_application_parameters`](crate::Interest::set_application_parameters)).
53    #[error("No application parameters present")]
54    MissingApplicationParameters,
55}
56
57/// Errors that can occur while verifying a signed
58/// [`Interest`](crate::Interest) or [`Data`](crate::Data) packet.
59#[derive(Error, Debug)]
60pub enum VerifyError {
61    /// The name's `ParametersSha256DigestComponent` doesn't match the
62    /// signed application parameters.
63    #[error("The parameter digest is invalid")]
64    InvalidParameterDigest,
65    /// The signature doesn't match the signed content.
66    #[error("The signature is invalid")]
67    InvalidSignature,
68    /// The packet is signed but carries no signature info.
69    #[error("The interest has no signature info")]
70    MissingSignatureInfo,
71    /// The packet is signed but has no application parameters to include
72    /// in the signature.
73    #[error("The signed interest has no application parameters")]
74    MissingApplicationParameters,
75    /// The signature's type isn't recognized by the verifier being used.
76    #[error("Signed with an unknown sign method")]
77    UnknownSignMethod,
78}