Skip to main content

appcore_dnt/
error.rs

1//! DNT error contracts.
2
3/// DNT-local result type.
4pub type DntResult<T> = Result<T, DntError>;
5
6/// Controlled DNT failures.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum DntError {
9    /// The bytes do not contain a structurally valid DNT envelope.
10    #[error("invalid DNT container")]
11    InvalidFormat,
12    /// The envelope version is newer or otherwise unsupported.
13    #[error("unsupported DNT envelope version")]
14    UnsupportedVersion,
15    /// The configured maximum payload size would be exceeded.
16    #[error("DNT payload exceeds configured limit")]
17    PayloadTooLarge,
18    /// The envelope contains an impossible or unsupported flag combination.
19    #[error("DNT flags are invalid")]
20    InvalidFlags,
21    /// The envelope is not bound to the expected context.
22    #[error("DNT context mismatch")]
23    ContextMismatch,
24    /// The key provider could not return usable material.
25    #[error("DNT key is unavailable")]
26    KeyUnavailable,
27    /// AEAD authentication failed.
28    #[error("DNT authentication failed")]
29    AuthenticationFailed,
30    /// The codec identifier is unknown or incompatible.
31    #[error("DNT codec is unavailable")]
32    CodecUnavailable,
33    /// Encoding or decoding failed without exposing payload material.
34    #[error("DNT codec failed")]
35    CodecFailed,
36    /// Filesystem persistence failed.
37    #[error("DNT I/O failed")]
38    Io,
39}
40
41/// Controlled key-provider failures.
42#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
43pub enum DntKeyError {
44    /// The requested key identity is unknown.
45    #[error("DNT key was not found")]
46    NotFound,
47    /// The key provider is intentionally unavailable.
48    #[error("DNT key provider is unavailable")]
49    Unavailable,
50    /// The resolved key material is malformed.
51    #[error("DNT key material is invalid")]
52    InvalidKey,
53    /// The key is not valid for the supplied context.
54    #[error("DNT key is not valid for the requested context")]
55    Forbidden,
56}
57
58/// Controlled codec failures.
59#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
60pub enum CodecError {
61    /// The codec cannot encode the supplied bytes.
62    #[error("DNT codec encode failed")]
63    EncodeFailed,
64    /// The codec cannot decode the supplied bytes.
65    #[error("DNT codec decode failed")]
66    DecodeFailed,
67    /// The payload violates a codec-specific bound.
68    #[error("DNT codec payload is too large")]
69    PayloadTooLarge,
70}
71
72impl From<DntKeyError> for DntError {
73    fn from(value: DntKeyError) -> Self {
74        match value {
75            DntKeyError::NotFound | DntKeyError::Unavailable => Self::KeyUnavailable,
76            DntKeyError::InvalidKey => Self::KeyUnavailable,
77            DntKeyError::Forbidden => Self::ContextMismatch,
78        }
79    }
80}
81
82impl From<CodecError> for DntError {
83    fn from(_: CodecError) -> Self {
84        Self::CodecFailed
85    }
86}