Skip to main content

proof_core/
error.rs

1// Copyright 2026 Proof Engine Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3//! Stable machine-readable error codes. Human messages may change; codes must not.
4use thiserror::Error;
5
6/// Stable error codes (V0.1). Codes are append-only once V0.1 ships; the three
7/// graph codes below were added in Phase 3, before any release.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum ErrorCode {
10    /// Input was not deterministically encoded (re-encode mismatch).
11    NonCanonical,
12    /// CBOR map contained a duplicate key.
13    DuplicateMapKey,
14    /// Forbidden CBOR construct (float, tag, indefinite, bignum, reserved ai).
15    ForbiddenCborConstruct,
16    /// Recomputed object id did not match stated id.
17    IdMismatch,
18    /// COSE signature did not verify.
19    SignatureInvalid,
20    /// Algorithm number unknown to this verifier.
21    UnknownAlgorithm,
22    /// Algorithm known but deprecated (e.g. COSE -8/-7). Fail closed.
23    DeprecatedAlgorithm,
24    /// Object/proof/policy version not supported.
25    UnsupportedVersion,
26    /// A configured resource limit was exceeded.
27    LimitExceeded,
28    /// Structurally malformed (truncated, trailing bytes, bad UTF-8, odd map).
29    Malformed,
30    /// Base64url id component invalid.
31    InvalidBase64Url,
32    /// COSE header contained an unexpected label/value.
33    UnexpectedHeaderParam,
34    /// Key algorithm does not match attestation algorithm.
35    AlgorithmConfusion,
36    /// Schema violation (unknown enum variant, unknown field, missing field).
37    SchemaViolation,
38    /// Trust-relevant relationship lacks backing evidence/attestation.
39    RelationshipUngrounded,
40    /// Relationship endpoint or reference points at an unknown object.
41    DanglingReference,
42    /// Forbidden cycle detected (SUPERSEDES subgraph must be acyclic).
43    CycleDetected,
44    /// Policy file invalid (syntax, unknown requirement, bad version).
45    /// Returned before any evaluation; never evaluated partially.
46    PolicyInvalid,
47    /// Attestation outside its validity window at the verifier clock
48    /// (`issued_at > now+skew` or `now > expires_at+skew`).
49    Expired,
50    /// Attestation covered by a valid signed revocation (claim.type="revoke").
51    Revoked,
52    /// Revocation status cannot be established: no or stale revocation
53    /// information was supplied. Fail closed, never PASS.
54    RevocationUnknown,
55    /// Signed status object (revoke/supersede) whose signer has no authority
56    /// over its target (not the original issuer, not in `revocation_authorities`).
57    UnauthorizedStatus,
58}
59
60impl ErrorCode {
61    /// Stable wire string. Used in reports and golden vectors.
62    pub fn as_str(self) -> &'static str {
63        match self {
64            Self::NonCanonical => "NON_CANONICAL",
65            Self::DuplicateMapKey => "DUPLICATE_MAP_KEY",
66            Self::ForbiddenCborConstruct => "FORBIDDEN_CBOR_CONSTRUCT",
67            Self::IdMismatch => "ID_MISMATCH",
68            Self::SignatureInvalid => "SIGNATURE_INVALID",
69            Self::UnknownAlgorithm => "UNKNOWN_ALGORITHM",
70            Self::DeprecatedAlgorithm => "DEPRECATED_ALGORITHM",
71            Self::UnsupportedVersion => "UNSUPPORTED_VERSION",
72            Self::LimitExceeded => "LIMIT_EXCEEDED",
73            Self::Malformed => "MALFORMED",
74            Self::InvalidBase64Url => "INVALID_BASE64URL",
75            Self::UnexpectedHeaderParam => "UNEXPECTED_HEADER_PARAM",
76            Self::AlgorithmConfusion => "ALGORITHM_CONFUSION",
77            Self::SchemaViolation => "SCHEMA_VIOLATION",
78            Self::RelationshipUngrounded => "RELATIONSHIP_UNGROUNDED",
79            Self::DanglingReference => "DANGLING_REFERENCE",
80            Self::CycleDetected => "CYCLE_DETECTED",
81            Self::PolicyInvalid => "POLICY_INVALID",
82            Self::Expired => "EXPIRED",
83            Self::Revoked => "REVOKED",
84            Self::RevocationUnknown => "REVOCATION_UNKNOWN",
85            Self::UnauthorizedStatus => "UNAUTHORIZED_STATUS",
86        }
87    }
88}
89
90impl std::fmt::Display for ErrorCode {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        f.write_str(self.as_str())
93    }
94}
95
96#[derive(Debug, Error)]
97#[error("[{code}] {message}")]
98pub struct ProofError {
99    pub code: ErrorCode,
100    pub message: String,
101}
102
103impl ProofError {
104    pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
105        Self {
106            code,
107            message: message.into(),
108        }
109    }
110
111    pub fn code_str(&self) -> &'static str {
112        self.code.as_str()
113    }
114}
115
116impl ErrorCode {
117    pub fn err(self, message: impl Into<String>) -> ProofError {
118        ProofError::new(self, message)
119    }
120}