bc_envelope/base/
error.rs

1use bc_components::Error as ComponentsError;
2use thiserror::Error;
3
4/// Error types returned when operating on Gordian Envelopes.
5///
6/// These errors capture various conditions that can occur when working with
7/// envelopes, including structure validation, operation constraints, and
8/// extension-specific errors.
9///
10/// The errors are organized by category, reflecting the base envelope
11/// specification and various extensions defined in the Gordian Envelope
12/// Internet Draft and Blockchain Commons Research (BCR) documents.
13#[derive(Debug, Error)]
14pub enum Error {
15    //
16    // Base Specification
17    /// Returned when attempting to compress or encrypt an envelope that has
18    /// already been elided.
19    ///
20    /// This error occurs because an elided envelope only contains a digest
21    /// reference and no longer has a subject that can be compressed or
22    /// encrypted.
23    #[error("envelope was elided, so it cannot be compressed or encrypted")]
24    AlreadyElided,
25
26    /// Returned when attempting to retrieve an assertion by predicate, but
27    /// multiple matching assertions exist.
28    ///
29    /// For queries that expect a single result (like `object_for_predicate`),
30    /// having multiple matching assertions is ambiguous and requires more
31    /// specific targeting.
32    #[error("more than one assertion matches the predicate")]
33    AmbiguousPredicate,
34
35    /// Returned when a digest validation fails.
36    ///
37    /// This can occur when unwrapping an envelope, verifying signatures, or
38    /// other operations that rely on the integrity of envelope digests.
39    #[error("digest did not match")]
40    InvalidDigest,
41
42    /// Returned when an envelope's format is invalid.
43    ///
44    /// This typically occurs during parsing or decoding of an envelope from
45    /// CBOR.
46    #[error("invalid format")]
47    InvalidFormat,
48
49    /// Returned when a digest is expected but not found.
50    ///
51    /// This can occur when working with envelope structures that require digest
52    /// information, such as when working with elided envelopes.
53    #[error("a digest was expected but not found")]
54    MissingDigest,
55
56    /// Returned when attempting to retrieve an assertion by predicate, but no
57    /// matching assertion exists.
58    ///
59    /// This error occurs with functions like `object_for_predicate` when the
60    /// specified predicate doesn't match any assertion in the envelope.
61    #[error("no assertion matches the predicate")]
62    NonexistentPredicate,
63
64    /// Returned when attempting to unwrap an envelope that wasn't wrapped.
65    ///
66    /// This error occurs when calling `Envelope::try_unwrap` on an
67    /// envelope that doesn't have the wrapped format.
68    #[error("cannot unwrap an envelope that was not wrapped")]
69    NotWrapped,
70
71    /// Returned when expecting an envelope's subject to be a leaf, but it
72    /// isn't.
73    ///
74    /// This error occurs when calling methods that require access to a leaf
75    /// value but the envelope's subject is an assertion, node, or elided.
76    #[error("the envelope's subject is not a leaf")]
77    NotLeaf,
78
79    /// Returned when expecting an envelope's subject to be an assertion, but it
80    /// isn't.
81    ///
82    /// This error occurs when calling methods that require an assertion
83    /// structure but the envelope's subject has a different format.
84    #[error("the envelope's subject is not an assertion")]
85    NotAssertion,
86
87    /// Returned
88    #[error("assertion must be a map with exactly one element")]
89    InvalidAssertion,
90
91    //
92    // Attachments Extension
93    /// Returned when an attachment's format is invalid.
94    ///
95    /// This error occurs when an envelope contains an attachment with an
96    /// invalid structure according to the Envelope Attachment specification
97    /// (BCR-2023-006).
98    #[cfg(feature = "attachment")]
99    #[error("invalid attachment")]
100    InvalidAttachment,
101
102    /// Returned when an attachment is requested but does not exist.
103    ///
104    /// This error occurs when attempting to retrieve an attachment by ID that
105    /// doesn't exist in the envelope.
106    #[cfg(feature = "attachment")]
107    #[error("nonexistent attachment")]
108    NonexistentAttachment,
109
110    /// Returned when multiple attachments match a single query.
111    ///
112    /// This error occurs when multiple attachments have the same ID, making
113    /// it ambiguous which attachment should be returned.
114    #[cfg(feature = "attachment")]
115    #[error("abiguous attachment")]
116    AmbiguousAttachment,
117
118    //
119    // Compression Extension
120    /// Returned when attempting to compress an envelope that is already
121    /// compressed.
122    ///
123    /// This error occurs when calling compression functions on an envelope that
124    /// already has compressed content, as defined in BCR-2023-005.
125    #[cfg(feature = "compress")]
126    #[error("envelope was already compressed")]
127    AlreadyCompressed,
128
129    /// Returned when attempting to decompress an envelope that is not
130    /// compressed.
131    ///
132    /// This error occurs when calling decompression functions on an envelope
133    /// that doesn't contain compressed content.
134    #[cfg(feature = "compress")]
135    #[error("cannot decompress an envelope that was not compressed")]
136    NotCompressed,
137
138    //
139    // Symmetric Encryption Extension
140    /// Returned when attempting to encrypt an envelope that is already
141    /// encrypted or compressed.
142    ///
143    /// This error occurs to prevent multiple layers of encryption or encryption
144    /// of compressed data, which could reduce security, as defined in
145    /// BCR-2023-004.
146    #[cfg(feature = "encrypt")]
147    #[error(
148        "envelope was already encrypted or compressed, so it cannot be encrypted"
149    )]
150    AlreadyEncrypted,
151
152    /// Returned when attempting to decrypt an envelope that is not encrypted.
153    ///
154    /// This error occurs when calling decryption functions on an envelope that
155    /// doesn't contain encrypted content.
156    #[cfg(feature = "encrypt")]
157    #[error("cannot decrypt an envelope that was not encrypted")]
158    NotEncrypted,
159
160    //
161    // Known Values Extension
162    /// Returned when expecting an envelope's subject to be a known value, but
163    /// it isn't.
164    ///
165    /// This error occurs when calling methods that require a known value (as
166    /// defined in BCR-2023-003) but the envelope's subject is a different
167    /// type.
168    #[cfg(feature = "known_value")]
169    #[error("the envelope's subject is not a known value")]
170    NotKnownValue,
171
172    //
173    // Public Key Encryption Extension
174    /// Returned when attempting to decrypt an envelope with a recipient that
175    /// doesn't match.
176    ///
177    /// This error occurs when trying to use a private key to decrypt an
178    /// envelope that wasn't encrypted for the corresponding public key.
179    #[cfg(feature = "recipient")]
180    #[error("unknown recipient")]
181    UnknownRecipient,
182
183    //
184    // Encrypted Key Extension
185    /// Returned when attempting to decrypt an envelope with a secret that
186    /// doesn't match.
187    ///
188    /// This error occurs when trying to use a secret that does not correspond
189    /// to the expected recipient, preventing successful decryption.
190    #[cfg(feature = "secret")]
191    #[error("secret not found")]
192    UnknownSecret,
193
194    //
195    // Public Key Signing Extension
196    /// Returned when a signature verification fails.
197    ///
198    /// This error occurs when a signature does not validate against its
199    /// purported public key.
200    #[cfg(feature = "signature")]
201    #[error("could not verify a signature")]
202    UnverifiedSignature,
203
204    /// Returned when the outer signature object type is not `Signature`.
205    #[cfg(feature = "signature")]
206    #[error("unexpected outer signature object type")]
207    InvalidOuterSignatureType,
208
209    /// Returned when the inner signature object type is not `Signature`.
210    #[cfg(feature = "signature")]
211    #[error("unexpected inner signature object type")]
212    InvalidInnerSignatureType,
213
214    /// Returned when the inner signature is not made with the same key as the
215    /// outer signature.
216    #[cfg(feature = "signature")]
217    #[error("inner signature not made with same key as outer signature")]
218    UnverifiedInnerSignature,
219
220    /// Returned when the signature object is not a `Signature`.
221    #[cfg(feature = "signature")]
222    #[error("unexpected signature object type")]
223    InvalidSignatureType,
224
225    //
226    // SSKR Extension
227    /// Returned when SSKR shares are invalid or insufficient for
228    /// reconstruction.
229    ///
230    /// This error occurs when attempting to join SSKR shares that are
231    /// malformed, from different splits, or insufficient to meet the
232    /// recovery threshold.
233    #[cfg(feature = "sskr")]
234    #[error("invalid SSKR shares")]
235    InvalidShares,
236
237    //
238    // Types Extension
239    /// Returned when an envelope contains an invalid type.
240    ///
241    /// This error occurs when an envelope's type information doesn't match
242    /// the expected format or value.
243    #[cfg(feature = "types")]
244    #[error("invalid type")]
245    InvalidType,
246
247    /// Returned when an envelope contains ambiguous type information.
248    ///
249    /// This error occurs when multiple type assertions exist that conflict
250    /// with each other or create ambiguity about the envelope's type.
251    #[cfg(feature = "types")]
252    #[error("ambiguous type")]
253    AmbiguousType,
254
255    /// Returned when the subject of the envelope is not the unit value.
256    #[cfg(feature = "known_value")]
257    #[error("the subject of the envelope is not the unit value")]
258    SubjectNotUnit,
259
260    //
261    // Expressions Extension
262    /// Returned when a response envelope has an unexpected ID.
263    ///
264    /// This error occurs when processing a response envelope and the ID doesn't
265    /// match the expected request ID, as defined in BCR-2023-012.
266    #[cfg(feature = "expression")]
267    #[error("unexpected response ID")]
268    UnexpectedResponseID,
269
270    /// Returned when a response envelope is invalid.
271    #[cfg(feature = "expression")]
272    #[error("invalid response")]
273    InvalidResponse,
274
275    /// SSKR error
276    #[cfg(feature = "sskr")]
277    #[error("sskr error: {0}")]
278    SSKR(#[from] bc_components::SSKRError),
279
280    /// dcbor error
281    #[error("dcbor error: {0}")]
282    Cbor(#[from] dcbor::Error),
283
284    /// Components error
285    #[error("components error: {0}")]
286    Components(#[from] ComponentsError),
287
288    /// General error
289    #[error("general error: {0}")]
290    General(String),
291}
292
293impl Error {
294    pub fn msg(msg: impl Into<String>) -> Self { Error::General(msg.into()) }
295}
296
297impl From<Error> for dcbor::Error {
298    fn from(error: Error) -> dcbor::Error {
299        match error {
300            Error::Cbor(err) => err,
301            _ => dcbor::Error::Custom(error.to_string()),
302        }
303    }
304}
305
306pub type Result<T> = std::result::Result<T, Error>;