Skip to main content

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    // Edges Extension
120    /// Returned when an edge is missing the required `'isA'` assertion.
121    #[cfg(feature = "edge")]
122    #[error("edge missing 'isA' assertion")]
123    EdgeMissingIsA,
124
125    /// Returned when an edge is missing the required `'source'` assertion.
126    #[cfg(feature = "edge")]
127    #[error("edge missing 'source' assertion")]
128    EdgeMissingSource,
129
130    /// Returned when an edge is missing the required `'target'` assertion.
131    #[cfg(feature = "edge")]
132    #[error("edge missing 'target' assertion")]
133    EdgeMissingTarget,
134
135    /// Returned when an edge has duplicate `'isA'` assertions.
136    #[cfg(feature = "edge")]
137    #[error("edge has duplicate 'isA' assertions")]
138    EdgeDuplicateIsA,
139
140    /// Returned when an edge has duplicate `'source'` assertions.
141    #[cfg(feature = "edge")]
142    #[error("edge has duplicate 'source' assertions")]
143    EdgeDuplicateSource,
144
145    /// Returned when an edge has duplicate `'target'` assertions.
146    #[cfg(feature = "edge")]
147    #[error("edge has duplicate 'target' assertions")]
148    EdgeDuplicateTarget,
149
150    /// Returned when an edge is requested but does not exist.
151    #[cfg(feature = "edge")]
152    #[error("nonexistent edge")]
153    NonexistentEdge,
154
155    /// Returned when multiple edges match a single query.
156    #[cfg(feature = "edge")]
157    #[error("ambiguous edge")]
158    AmbiguousEdge,
159
160    //
161    // Compression Extension
162    /// Returned when attempting to compress an envelope that is already
163    /// compressed.
164    ///
165    /// This error occurs when calling compression functions on an envelope that
166    /// already has compressed content, as defined in BCR-2023-005.
167    #[cfg(feature = "compress")]
168    #[error("envelope was already compressed")]
169    AlreadyCompressed,
170
171    /// Returned when attempting to decompress an envelope that is not
172    /// compressed.
173    ///
174    /// This error occurs when calling decompression functions on an envelope
175    /// that doesn't contain compressed content.
176    #[cfg(feature = "compress")]
177    #[error("cannot decompress an envelope that was not compressed")]
178    NotCompressed,
179
180    //
181    // Symmetric Encryption Extension
182    /// Returned when attempting to encrypt an envelope that is already
183    /// encrypted or compressed.
184    ///
185    /// This error occurs to prevent multiple layers of encryption or encryption
186    /// of compressed data, which could reduce security, as defined in
187    /// BCR-2023-004.
188    #[cfg(feature = "encrypt")]
189    #[error(
190        "envelope was already encrypted or compressed, so it cannot be encrypted"
191    )]
192    AlreadyEncrypted,
193
194    /// Returned when attempting to decrypt an envelope that is not encrypted.
195    ///
196    /// This error occurs when calling decryption functions on an envelope that
197    /// doesn't contain encrypted content.
198    #[cfg(feature = "encrypt")]
199    #[error("cannot decrypt an envelope that was not encrypted")]
200    NotEncrypted,
201
202    //
203    // Known Values Extension
204    /// Returned when expecting an envelope's subject to be a known value, but
205    /// it isn't.
206    ///
207    /// This error occurs when calling methods that require a known value (as
208    /// defined in BCR-2023-003) but the envelope's subject is a different
209    /// type.
210    #[cfg(feature = "known_value")]
211    #[error("the envelope's subject is not a known value")]
212    NotKnownValue,
213
214    //
215    // Public Key Encryption Extension
216    /// Returned when attempting to decrypt an envelope with a recipient that
217    /// doesn't match.
218    ///
219    /// This error occurs when trying to use a private key to decrypt an
220    /// envelope that wasn't encrypted for the corresponding public key.
221    #[cfg(feature = "recipient")]
222    #[error("unknown recipient")]
223    UnknownRecipient,
224
225    //
226    // Encrypted Key Extension
227    /// Returned when attempting to decrypt an envelope with a secret that
228    /// doesn't match.
229    ///
230    /// This error occurs when trying to use a secret that does not correspond
231    /// to the expected recipient, preventing successful decryption.
232    #[cfg(feature = "secret")]
233    #[error("secret not found")]
234    UnknownSecret,
235
236    //
237    // Public Key Signing Extension
238    /// Returned when a signature verification fails.
239    ///
240    /// This error occurs when a signature does not validate against its
241    /// purported public key.
242    #[cfg(feature = "signature")]
243    #[error("could not verify a signature")]
244    UnverifiedSignature,
245
246    /// Returned when the outer signature object type is not `Signature`.
247    #[cfg(feature = "signature")]
248    #[error("unexpected outer signature object type")]
249    InvalidOuterSignatureType,
250
251    /// Returned when the inner signature object type is not `Signature`.
252    #[cfg(feature = "signature")]
253    #[error("unexpected inner signature object type")]
254    InvalidInnerSignatureType,
255
256    /// Returned when the inner signature is not made with the same key as the
257    /// outer signature.
258    #[cfg(feature = "signature")]
259    #[error("inner signature not made with same key as outer signature")]
260    UnverifiedInnerSignature,
261
262    /// Returned when the signature object is not a `Signature`.
263    #[cfg(feature = "signature")]
264    #[error("unexpected signature object type")]
265    InvalidSignatureType,
266
267    //
268    // SSKR Extension
269    /// Returned when SSKR shares are invalid or insufficient for
270    /// reconstruction.
271    ///
272    /// This error occurs when attempting to join SSKR shares that are
273    /// malformed, from different splits, or insufficient to meet the
274    /// recovery threshold.
275    #[cfg(feature = "sskr")]
276    #[error("invalid SSKR shares")]
277    InvalidShares,
278
279    //
280    // Types Extension
281    /// Returned when an envelope contains an invalid type.
282    ///
283    /// This error occurs when an envelope's type information doesn't match
284    /// the expected format or value.
285    #[cfg(feature = "types")]
286    #[error("invalid type")]
287    InvalidType,
288
289    /// Returned when an envelope contains ambiguous type information.
290    ///
291    /// This error occurs when multiple type assertions exist that conflict
292    /// with each other or create ambiguity about the envelope's type.
293    #[cfg(feature = "types")]
294    #[error("ambiguous type")]
295    AmbiguousType,
296
297    /// Returned when the subject of the envelope is not the unit value.
298    #[cfg(feature = "known_value")]
299    #[error("the subject of the envelope is not the unit value")]
300    SubjectNotUnit,
301
302    //
303    // Expressions Extension
304    /// Returned when a response envelope has an unexpected ID.
305    ///
306    /// This error occurs when processing a response envelope and the ID doesn't
307    /// match the expected request ID, as defined in BCR-2023-012.
308    #[cfg(feature = "expression")]
309    #[error("unexpected response ID")]
310    UnexpectedResponseID,
311
312    /// Returned when a response envelope is invalid.
313    #[cfg(feature = "expression")]
314    #[error("invalid response")]
315    InvalidResponse,
316
317    /// SSKR error
318    #[cfg(feature = "sskr")]
319    #[error("sskr error: {0}")]
320    SSKR(#[from] bc_components::SSKRError),
321
322    /// dcbor error
323    #[error("dcbor error: {0}")]
324    Cbor(#[from] dcbor::Error),
325
326    /// Components error
327    #[error("components error: {0}")]
328    Components(#[from] ComponentsError),
329
330    /// General error
331    #[error("general error: {0}")]
332    General(String),
333}
334
335impl Error {
336    pub fn msg(msg: impl Into<String>) -> Self { Error::General(msg.into()) }
337}
338
339impl From<Error> for dcbor::Error {
340    fn from(error: Error) -> dcbor::Error {
341        match error {
342            Error::Cbor(err) => err,
343            _ => dcbor::Error::Custom(error.to_string()),
344        }
345    }
346}
347
348pub type Result<T> = std::result::Result<T, Error>;