Skip to main content

ma_core/
error.rs

1//! Error types for ma-core.
2
3use thiserror::Error;
4
5// ─── Primitive DID/message errors (from ma-did) ─────────────────────────────
6
7pub type MaResult<T> = std::result::Result<T, MaError>;
8
9#[derive(Debug, Error)]
10pub enum MaError {
11    #[error("empty DID")]
12    EmptyDid,
13    #[error("invalid DID prefix, expected did:ma:")]
14    InvalidDidPrefix,
15    #[error("missing DID identifier")]
16    MissingIdentifier,
17    #[error("missing DID fragment")]
18    MissingFragment,
19    #[error("unexpected DID fragment")]
20    UnexpectedFragment,
21    #[error("invalid DID format")]
22    InvalidDidFormat,
23    #[error("invalid DID fragment: {0}")]
24    InvalidFragment(String),
25    #[error("invalid DID identifier")]
26    InvalidIdentifier,
27    #[error("invalid message id")]
28    InvalidMessageId,
29    #[error("empty message id")]
30    EmptyMessageId,
31    #[error("invalid message type")]
32    InvalidMessageType,
33    #[error("invalid key type")]
34    InvalidKeyType,
35    #[error("invalid identity secret")]
36    InvalidIdentitySecret,
37    #[error("invalid recipient")]
38    InvalidRecipient,
39    #[error("missing message content")]
40    MissingContent,
41    #[error("missing message content type")]
42    MissingContentType,
43    #[error("missing sender")]
44    MissingSender,
45    #[error("missing signature")]
46    MissingSignature,
47    #[error("message timestamp is invalid")]
48    InvalidMessageTimestamp,
49    #[error("message is too old")]
50    MessageTooOld,
51    #[error("message timestamp is too far in the future")]
52    MessageFromFuture,
53    #[error("replay detected")]
54    ReplayDetected,
55    #[error("sender and recipient must differ")]
56    SameActor,
57    #[error("broadcast messages must not have a recipient")]
58    BroadcastMustNotHaveRecipient,
59    #[error("encrypted messages require a recipient")]
60    MessageRequiresRecipient,
61    #[error("context missing")]
62    EmptyContext,
63    #[error("controller missing")]
64    EmptyController,
65    #[error("verification method missing type")]
66    VerificationMethodMissingType,
67    #[error("unknown verification method: {0}")]
68    UnknownVerificationMethod(String),
69    #[error("public key multibase is empty")]
70    EmptyPublicKeyMultibase,
71    #[error("invalid public key multibase")]
72    InvalidPublicKeyMultibase,
73    #[error("invalid multicodec, expected {expected}, got {actual}")]
74    InvalidMulticodec { expected: u64, actual: u64 },
75    #[error("invalid key length, expected {expected}, got {actual}")]
76    InvalidKeyLength { expected: usize, actual: usize },
77    #[error("proof is missing")]
78    MissingProof,
79    #[error("document signature is invalid")]
80    InvalidDocumentSignature,
81    #[error("message signature is invalid")]
82    InvalidMessageSignature,
83    #[error("invalid createdAt timestamp: {0}")]
84    InvalidCreatedAt(String),
85    #[error("invalid updatedAt timestamp: {0}")]
86    InvalidUpdatedAt(String),
87    #[error("identity CID is invalid")]
88    InvalidIdentity,
89    #[error("missing envelope field: {0}")]
90    MissingEnvelopeField(&'static str),
91    #[error("invalid ephemeral key length")]
92    InvalidEphemeralKeyLength,
93    #[error("ciphertext too short")]
94    CiphertextTooShort,
95    #[error("cryptographic operation failed")]
96    Crypto,
97    #[error("CBOR encode failed: {0}")]
98    CborEncode(String),
99    #[error("CBOR decode failed: {0}")]
100    CborDecode(String),
101    #[error("JSON encode failed: {0}")]
102    JsonEncode(String),
103    #[error("JSON decode failed: {0}")]
104    JsonDecode(String),
105}
106
107// ─── Service-level errors ────────────────────────────────────────────────────
108
109/// Errors returned by ma-core public APIs.
110#[derive(Debug, Error)]
111pub enum Error {
112    // ─── Transport ──────────────────────────────────────────────────────
113    #[error("transport error: {0}")]
114    Transport(String),
115
116    #[error("transport connect failed: {0}")]
117    Connect(String),
118
119    #[error("transport bind failed: {0}")]
120    Bind(String),
121
122    #[error("stream open failed: {0}")]
123    StreamOpen(String),
124
125    #[error("connection closed: {0}")]
126    ConnectionClosed(String),
127
128    // ─── Validation ─────────────────────────────────────────────────────
129    #[error("message validation failed: {0}")]
130    Validation(#[from] MaError),
131
132    #[error("message signature verification failed")]
133    SignatureVerification,
134
135    #[error("replay detected for message {0}")]
136    Replay(String),
137
138    // ─── Resolution ─────────────────────────────────────────────────────
139    #[error("DID resolution failed for {did}: {detail}")]
140    Resolution { did: String, detail: String },
141
142    #[error("no inbox transport in DID document for {0}")]
143    NoInboxTransport(String),
144
145    #[error("invalid transport string: {0}")]
146    InvalidTransport(String),
147
148    // ─── Identity / key bootstrap ───────────────────────────────────────
149    #[error("secret key error: {0}")]
150    SecretKey(String),
151
152    #[error("endpoint ID derivation failed: {0}")]
153    EndpointId(String),
154
155    // ─── Config ─────────────────────────────────────────────────────────
156    #[cfg(feature = "config")]
157    #[error("config error: {0}")]
158    Config(String),
159
160    // ─── Secrets bundle ──────────────────────────────────────────────────
161    #[cfg(feature = "config")]
162    #[error("secrets error: {0}")]
163    Secrets(String),
164
165    // ─── ACL ─────────────────────────────────────────────────────────────
166    #[cfg(feature = "acl")]
167    #[error("acl error: {0}")]
168    Acl(String),
169
170    // ─── Service registration ───────────────────────────────────────────
171    #[error("duplicate service ALPN: {0}")]
172    DuplicateService(String),
173
174    // ─── Generic pass-through ───────────────────────────────────────────
175    #[error(transparent)]
176    Other(#[from] anyhow::Error),
177}
178
179pub type Result<T> = std::result::Result<T, Error>;