Skip to main content

arete_hash/protocol/
error.rs

1use std::fmt;
2
3/// A contract validation failure with a stable, language-neutral code.
4///
5/// `path` is a JSON Pointer (RFC 6901) into the document that failed. Rust
6/// and TypeScript report the same `code` and `path` for every vector in
7/// `test-vectors/protocol-v1.json`.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct ProtocolContractError {
10    pub code: &'static str,
11    pub path: String,
12    pub message: String,
13}
14
15impl ProtocolContractError {
16    pub fn new(code: &'static str, path: impl Into<String>, message: impl Into<String>) -> Self {
17        Self {
18            code,
19            path: path.into(),
20            message: message.into(),
21        }
22    }
23}
24
25impl fmt::Display for ProtocolContractError {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        let path = if self.path.is_empty() {
28            "/"
29        } else {
30            &self.path
31        };
32        write!(formatter, "{} at {}: {}", self.code, path, self.message)
33    }
34}
35
36impl std::error::Error for ProtocolContractError {}
37
38pub type ProtocolResult<T> = Result<T, ProtocolContractError>;
39
40/// Every stable error code a protocol contract validator can return.
41pub const PROTOCOL_ERROR_CODES: &[&str] = &[
42    // JSON well-formedness (shared with the hash profile).
43    "invalid-json",
44    "duplicate-json-key",
45    "unsafe-json-integer",
46    "float-not-allowed",
47    // Shape.
48    "invalid-type",
49    "unknown-field",
50    "missing-field",
51    "null-not-allowed",
52    "invalid-schema-tag",
53    "invalid-value",
54    "invalid-identifier",
55    "invalid-decimal",
56    "invalid-pubkey",
57    "invalid-hash-id",
58    "invalid-semver",
59    "invalid-expression",
60    "invalid-signature",
61    "string-too-long",
62    "array-too-large",
63    "map-too-large",
64    "integer-out-of-range",
65    "duplicate-entry",
66    "unsorted-entries",
67    "invalid-schema",
68    // References.
69    "unknown-program",
70    "unknown-read",
71    "unknown-state",
72    "unknown-step",
73    "unknown-event",
74    "unknown-role",
75    "unknown-ephemeral",
76    "unknown-bound-account",
77    "unknown-arg",
78    // Machines.
79    "unreachable-state",
80    "dead-end-state",
81    "terminal-state-exit",
82    "derived-state-without-rule",
83    "derive-targets-recorded-state",
84    "transition-targets-derived-state",
85    "missing-participant-machine",
86    // Steps.
87    "duplicate-guard-code",
88    "reserved-guard-code",
89    "create-step-count",
90    "invalid-authority",
91    "missing-access",
92    "forbidden-access",
93    "missing-role-guard",
94    "role-not-bound",
95    "bind-outside-create",
96    "create-step-transition",
97    "invalid-fee-basis",
98    "handler-abi-required",
99    "handler-abi-unused",
100    "remaining-account-signer",
101    // Reactions and events.
102    "reaction-action-mismatch",
103    "event-not-emitted",
104    "event-machine-mismatch",
105];