1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! Error types for the syncbat runtime shell.
use std::error::Error;
use std::fmt;
/// Error returned while assembling a [`crate::core::Core`].
///
/// `#[non_exhaustive]` so post-1.0 we can add validation variants
/// (e.g. cross-module descriptor checks, schema-version drift) without
/// breaking downstream exhaustive matches.
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BuildError {
/// An operation descriptor was registered more than once.
DuplicateOperation {
/// Duplicate operation name.
name: String,
},
/// A handler was registered more than once for the same operation name.
DuplicateHandler {
/// Duplicate handler name.
name: String,
},
/// A handler was registered without a matching operation descriptor.
MissingDescriptor {
/// Handler name without a descriptor.
name: String,
},
/// An operation descriptor was registered without a matching handler.
MissingHandler {
/// Operation name without a handler.
name: String,
},
/// A module descriptor failed shape validation.
InvalidModule {
/// Module name.
name: String,
/// Validation message.
message: String,
},
/// An operation descriptor failed shape validation.
InvalidOperation {
/// Operation name.
name: String,
/// Validation message.
message: String,
},
/// A handler name failed shape validation.
InvalidHandler {
/// Handler name.
name: String,
/// Validation message.
message: String,
},
/// The builder was finalized without a receipt sink and without an explicit
/// receipts opt-out.
///
/// A sinkless core silently drops every runtime receipt, so the build fails
/// closed. Wire a sink with
/// [`crate::builder::CoreBuilder::receipt_sink`], or state on purpose that
/// this core records no receipts with
/// [`crate::builder::CoreBuilder::without_receipts`].
MissingReceiptSink,
}
impl BuildError {
/// Build a duplicate-operation error.
#[must_use]
pub fn duplicate_operation(name: impl Into<String>) -> Self {
Self::DuplicateOperation { name: name.into() }
}
/// Build a duplicate-handler error.
#[must_use]
pub fn duplicate_handler(name: impl Into<String>) -> Self {
Self::DuplicateHandler { name: name.into() }
}
/// Build a missing-descriptor error.
#[must_use]
pub fn missing_descriptor(name: impl Into<String>) -> Self {
Self::MissingDescriptor { name: name.into() }
}
/// Build a missing-handler error.
#[must_use]
pub fn missing_handler(name: impl Into<String>) -> Self {
Self::MissingHandler { name: name.into() }
}
/// Build an invalid-module error.
#[must_use]
pub fn invalid_module(name: impl Into<String>, message: impl Into<String>) -> Self {
Self::InvalidModule {
name: name.into(),
message: message.into(),
}
}
/// Build an invalid-operation error.
#[must_use]
pub fn invalid_operation(name: impl Into<String>, message: impl Into<String>) -> Self {
Self::InvalidOperation {
name: name.into(),
message: message.into(),
}
}
/// Build an invalid-handler error.
#[must_use]
pub fn invalid_handler(name: impl Into<String>, message: impl Into<String>) -> Self {
Self::InvalidHandler {
name: name.into(),
message: message.into(),
}
}
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DuplicateOperation { name } => {
write!(f, "operation `{name}` is already registered")
}
Self::DuplicateHandler { name } => {
write!(f, "handler for operation `{name}` is already registered")
}
Self::MissingDescriptor { name } => {
write!(f, "handler `{name}` has no matching operation descriptor")
}
Self::MissingHandler { name } => {
write!(f, "operation `{name}` has no registered handler")
}
Self::InvalidModule { name, message } => {
write!(f, "module `{name}` is invalid: {message}")
}
Self::InvalidOperation { name, message } => {
write!(f, "operation `{name}` is invalid: {message}")
}
Self::InvalidHandler { name, message } => {
write!(f, "handler `{name}` is invalid: {message}")
}
Self::MissingReceiptSink => f.write_str(
"core has no receipt sink: a sinkless core silently drops every runtime \
receipt. Wire one with CoreBuilder::receipt_sink, or opt out explicitly \
with CoreBuilder::without_receipts",
),
}
}
}
impl Error for BuildError {}
/// Handler failure preserved when receipt recording also fails.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct ReceiptSinkHandlerCause {
/// Handler-supplied error class.
pub code: String,
/// Handler-supplied error message.
pub message: String,
}
impl ReceiptSinkHandlerCause {
/// Build a handler cause for a receipt-sink failure.
#[must_use]
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
}
}
/// Stable handler error class.
#[must_use]
pub fn code(&self) -> &str {
&self.code
}
/// Handler-supplied error message.
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
/// Error returned by synchronous operation dispatch.
///
/// `#[non_exhaustive]` so the wire-error vocabulary can grow (e.g.
/// rate-limit, auth, schema-mismatch variants) without breaking
/// downstream matches that translate `RuntimeError` into transport-
/// layer error codes.
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum RuntimeError {
/// The requested operation name is not known to the runtime.
UnknownOperation {
/// Requested operation name.
name: String,
},
/// The operation descriptor exists, but no handler is available.
MissingHandler {
/// Requested operation name.
name: String,
},
/// The handler rejected the invocation.
Handler {
/// Operation name being handled.
name: String,
/// Handler-supplied error class.
code: String,
/// Handler-supplied error message.
message: String,
},
/// Runtime policy denied the invocation. Admission guards and observed
/// effect-row enforcement both record a `Denied` receipt before returning
/// this variant.
Denied {
/// Operation name that was denied.
name: String,
/// Guard-supplied denial class.
code: String,
/// Guard-supplied denial message.
message: String,
},
/// The configured receipt sink rejected a runtime-emitted receipt.
ReceiptSink {
/// Operation name whose receipt could not be recorded.
name: String,
/// Sink error message.
message: String,
/// Handler failure that preceded this sink failure, when present.
caused_by_handler: Option<ReceiptSinkHandlerCause>,
},
/// The configured operation-status sink rejected a runtime-emitted fact.
StatusSink {
/// Operation name whose status fact could not be recorded.
name: String,
/// Sink error message.
message: String,
/// Handler failure that preceded this sink failure, when present.
caused_by_handler: Option<ReceiptSinkHandlerCause>,
},
}
impl RuntimeError {
/// Build an unknown-operation error.
#[must_use]
pub fn unknown_operation(name: impl Into<String>) -> Self {
Self::UnknownOperation { name: name.into() }
}
/// Build a missing-handler error.
#[must_use]
pub fn missing_handler(name: impl Into<String>) -> Self {
Self::MissingHandler { name: name.into() }
}
/// Build a handler error with an operation name and message.
#[must_use]
pub fn handler(
name: impl Into<String>,
code: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::Handler {
name: name.into(),
code: code.into(),
message: message.into(),
}
}
/// Build an admission-denied error with an operation name, class, and message.
#[must_use]
pub fn denied(
name: impl Into<String>,
code: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::Denied {
name: name.into(),
code: code.into(),
message: message.into(),
}
}
/// Build a receipt-sink error with an operation name and message.
#[must_use]
pub fn receipt_sink(name: impl Into<String>, message: impl Into<String>) -> Self {
Self::ReceiptSink {
name: name.into(),
message: message.into(),
caused_by_handler: None,
}
}
/// Build a receipt-sink error after a handler failure.
#[must_use]
pub fn receipt_sink_after_handler_failure(
name: impl Into<String>,
message: impl Into<String>,
cause: ReceiptSinkHandlerCause,
) -> Self {
Self::ReceiptSink {
name: name.into(),
message: message.into(),
caused_by_handler: Some(cause),
}
}
/// Build a status-sink error with an operation name and message.
#[must_use]
pub fn status_sink(name: impl Into<String>, message: impl Into<String>) -> Self {
Self::StatusSink {
name: name.into(),
message: message.into(),
caused_by_handler: None,
}
}
/// Build a status-sink error after a handler failure.
#[must_use]
pub fn status_sink_after_handler_failure(
name: impl Into<String>,
message: impl Into<String>,
cause: ReceiptSinkHandlerCause,
) -> Self {
Self::StatusSink {
name: name.into(),
message: message.into(),
caused_by_handler: Some(cause),
}
}
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownOperation { name } => write!(f, "unknown operation `{name}`"),
Self::MissingHandler { name } => {
write!(f, "operation `{name}` has no registered handler")
}
Self::Handler {
name,
code,
message,
} => {
write!(
f,
"handler for operation `{name}` failed with {code}: {message}"
)
}
Self::Denied {
name,
code,
message,
} => {
write!(f, "operation `{name}` denied with {code}: {message}")
}
Self::ReceiptSink {
name,
message,
caused_by_handler,
} => {
if let Some(cause) = caused_by_handler {
write!(
f,
"receipt sink for operation `{name}` failed after handler error {}: {}: {message}",
cause.code(),
cause.message()
)
} else {
write!(f, "receipt sink for operation `{name}` failed: {message}")
}
}
Self::StatusSink {
name,
message,
caused_by_handler,
} => {
if let Some(cause) = caused_by_handler {
write!(
f,
"status sink for operation `{name}` failed after handler error {}: {}: {message}",
cause.code(),
cause.message()
)
} else {
write!(f, "status sink for operation `{name}` failed: {message}")
}
}
}
}
}
impl Error for RuntimeError {}