canton_core/error.rs
1//! The SDK-wide error type and [`Result`] alias.
2
3/// The single error type for the whole Canton Rust SDK.
4///
5/// It is `#[non_exhaustive]` so new variants can be added without a breaking
6/// change. Large upstream error types are boxed so that `Result<T, Error>`
7/// stays cheap to move on the happy path.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11 /// gRPC transport failure (DNS, TCP, TLS, HTTP/2). Retriable.
12 #[error("transport error")]
13 Transport(#[source] Box<tonic::transport::Error>),
14
15 /// A non-gRPC connection failure (e.g. an HTTP/JSON or token-endpoint
16 /// request that could not be sent). Retriable.
17 #[error("connection error: {0}")]
18 Connection(String),
19
20 /// The server returned a gRPC status. The full [`tonic::Status`] is kept so
21 /// callers can inspect the code, message, and metadata; see [`Error::code`].
22 #[error("grpc status {}: {}", .0.code(), .0.message())]
23 Status(#[source] Box<tonic::Status>),
24
25 /// A non-success HTTP response from the JSON API, a token endpoint or a
26 /// token-standard registry. Retriable for transient status codes (see
27 /// [`Error::is_retriable`]).
28 ///
29 /// `body` is the response body and nothing else — this variant's readers
30 /// (`code`, `category`, `retry_delay`, `error_info`) parse it as the
31 /// participant's JSON. Where the request went is `url`, so a 404 from a
32 /// mistyped base URL and a 404 from an unknown contract can be told apart
33 /// without corrupting the body. `#[non_exhaustive]`: construct it with
34 /// [`Error::http`](Self::http), match it with `..`.
35 #[error("http {status}{}: {body}", url.as_deref().map(|u| format!(" at {u}")).unwrap_or_default())]
36 #[non_exhaustive]
37 Http {
38 /// The HTTP status code.
39 status: u16,
40 /// The response body (truncated by the caller if large).
41 body: String,
42 /// The URL the request went to, when the caller kept it.
43 url: Option<String>,
44 },
45
46 /// JSON (de)serialization error.
47 #[error("json error: {0}")]
48 Json(#[source] Box<serde_json::Error>),
49
50 /// A command was rejected by the ledger for business/interpretation
51 /// reasons (as opposed to a transport failure). Not retriable: this is a
52 /// terminal outcome of that submission read back from the completion
53 /// stream, and re-submitting is an application decision — the automatic
54 /// retry path (`submit*` RPC errors) surfaces rejections as
55 /// [`Error::Status`] instead, with full category/retry-delay precision.
56 #[error("command rejected ({code}): {message}")]
57 CommandRejected {
58 /// The rejection status code.
59 code: String,
60 /// The rejection message.
61 message: String,
62 },
63
64 /// Authentication/authorization was rejected (bad or expired credentials).
65 /// Not retriable — a token-transport failure surfaces as [`Error::Connection`]
66 /// or [`Error::Http`] instead.
67 #[error("authentication failed: {0}")]
68 Auth(String),
69
70 /// A request precondition or configuration value was invalid before send.
71 #[error("invalid request: {0}")]
72 InvalidRequest(String),
73
74 /// The server's response was well-formed at the transport level but not
75 /// what the protocol expects (e.g. a missing field, or a stream that ended
76 /// unexpectedly). Not a caller-input error.
77 #[error("unexpected response: {0}")]
78 UnexpectedResponse(String),
79
80 /// The operation exceeded its configured deadline. Retriable.
81 #[error("operation timed out")]
82 Timeout,
83
84 /// A typed payload failed to convert to or from the Ledger API `Value` —
85 /// a `canton-daml` codec error. Not retriable: the shape will not change
86 /// on a retry.
87 // The cause is in the message *and* kept as `source`: every other variant
88 // here prints its detail, and an application that logs `{err}` without
89 // walking the chain would otherwise be told only that something failed.
90 #[error("payload conversion failed: {0}")]
91 Payload(#[source] Box<dyn std::error::Error + Send + Sync>),
92}
93
94impl Error {
95 /// A non-success HTTP response, with the body left as the body.
96 #[must_use]
97 pub fn http(status: u16, body: impl Into<String>) -> Self {
98 Error::Http {
99 status,
100 body: body.into(),
101 url: None,
102 }
103 }
104
105 /// [`http`](Self::http), remembering where the request went.
106 #[must_use]
107 pub fn http_at(status: u16, body: impl Into<String>, url: impl Into<String>) -> Self {
108 Error::Http {
109 status,
110 body: body.into(),
111 url: Some(url.into()),
112 }
113 }
114
115 /// The gRPC status code the participant answered with, on **either**
116 /// transport.
117 ///
118 /// The JSON Ledger API reports it numerically as `grpcCodeValue`, so the
119 /// same failure yields the same code whichever lane carried it — the HTTP
120 /// status alone does not, since Canton maps several codes onto one status.
121 /// `None` when there is no participant verdict to report: a transport
122 /// failure, a timeout, or an HTTP body that is not a Canton error object
123 /// (a proxy's error page, say).
124 #[must_use]
125 pub fn code(&self) -> Option<tonic::Code> {
126 match self {
127 Error::Status(status) => Some(status.code()),
128 Error::Http { body, .. } => http_grpc_code(body),
129 _ => None,
130 }
131 }
132
133 /// Whether retrying the operation may succeed.
134 ///
135 /// For gRPC statuses, Canton's own verdict wins: every Ledger API error
136 /// carries an [`ErrorCategory`] whose retryability is defined by the
137 /// [error-code documentation], and retryable errors additionally carry a
138 /// `google.rpc.RetryInfo` detail. Only when a status carries neither (a
139 /// proxy in the middle, a non-Canton server) does the classification fall
140 /// back to the transient gRPC codes (`Unavailable`, `DeadlineExceeded`,
141 /// `ResourceExhausted`, `Aborted`).
142 ///
143 /// Beyond statuses, transient conditions are retriable: timeouts,
144 /// transport/connection failures, and transient HTTP status codes
145 /// (408, 429, 5xx). Everything else — invalid input, auth rejection,
146 /// command rejection, `NotFound`/`AlreadyExists`, deserialization — is not.
147 ///
148 /// [error-code documentation]: https://docs.daml.com/canton/reference/error_codes.html
149 #[must_use]
150 pub fn is_retriable(&self) -> bool {
151 use tonic::Code::{Aborted, DeadlineExceeded, ResourceExhausted, Unavailable};
152 match self {
153 Error::Timeout | Error::Transport(_) | Error::Connection(_) => true,
154 Error::Status(status) => match status_category(status) {
155 Some(category) => category.is_retriable(),
156 // No category ⇒ not a Canton self-service error. A RetryInfo
157 // detail is still an explicit "retry me"; else fall back to
158 // the transient codes, plus a connection that died in flight.
159 None => {
160 status_retry_delay(status).is_some()
161 || matches!(
162 status.code(),
163 Unavailable | DeadlineExceeded | ResourceExhausted | Aborted
164 )
165 || is_transport_death(status)
166 }
167 },
168 // The JSON Ledger API carries the same verdict in the error body
169 // (`errorCategory`, `retryInfo`); parse it before falling back to
170 // the transient HTTP status codes.
171 Error::Http { status, body, .. } => match http_category(body) {
172 Some(category) => category.is_retriable(),
173 None => http_retry_delay(body).is_some() || matches!(status, 408 | 429 | 500..=599),
174 },
175 _ => false,
176 }
177 }
178
179 /// The Canton [`ErrorCategory`] of this error, when it carries one: from
180 /// `ErrorInfo.metadata["category"]` on a gRPC status, or the
181 /// `errorCategory` field of a JSON API error body. This is the field the
182 /// [error-code documentation] tells clients to base error handling on;
183 /// [`Error::is_retriable`] already does.
184 ///
185 /// [error-code documentation]: https://docs.daml.com/canton/reference/error_codes.html
186 #[must_use]
187 pub fn category(&self) -> Option<ErrorCategory> {
188 match self {
189 Error::Status(status) => status_category(status),
190 Error::Http { body, .. } => http_category(body),
191 _ => None,
192 }
193 }
194
195 /// The server-recommended delay before retrying, from the
196 /// `google.rpc.RetryInfo` detail of a gRPC status or the `retryInfo`
197 /// field of a JSON API error body. Canton attaches it to retryable
198 /// errors; the retry helper ([`crate::retry::run_with_retry`]) already
199 /// honours it.
200 #[must_use]
201 pub fn retry_delay(&self) -> Option<std::time::Duration> {
202 match self {
203 Error::Status(status) => status_retry_delay(status),
204 Error::Http { body, .. } => http_retry_delay(body),
205 _ => None,
206 }
207 }
208
209 /// The correlation id of the failed request, from the
210 /// `google.rpc.RequestInfo` detail of a gRPC status or the
211 /// `correlationId`/`traceId` of a JSON API error body. Canton echoes it
212 /// in every error; quote it when reporting a problem to the participant's
213 /// operator, who can find the server-side trace by it.
214 #[must_use]
215 pub fn correlation_id(&self) -> Option<String> {
216 match self {
217 Error::Status(status) => {
218 use tonic_types::StatusExt as _;
219 status
220 .get_details_request_info()
221 .map(|info| info.request_id)
222 }
223 Error::Http { body, .. } => http_correlation_id(body),
224 _ => None,
225 }
226 }
227
228 /// The resources this error is about: which contract, package, party or
229 /// synchronizer the participant is complaining of. Canton attaches these to
230 /// the errors where "which one?" is the first question — `CONTRACT_NOT_FOUND`
231 /// names the contract id, contention names the locked contracts.
232 ///
233 /// A `Vec` rather than an `Option` because the wire carries a list: the JSON
234 /// Ledger API's `resources` is an array of `[type, name]` pairs, and one
235 /// error can name several. The gRPC side yields at most one today — that is
236 /// a limit of `tonic_types`, which models a single `google.rpc.ResourceInfo`
237 /// detail, not of the protocol.
238 #[must_use]
239 pub fn resource_info(&self) -> Vec<ResourceInfo> {
240 match self {
241 Error::Status(status) => {
242 use tonic_types::StatusExt as _;
243 status
244 .get_details_resource_info()
245 .map(|info| ResourceInfo {
246 resource_type: info.resource_type,
247 resource_name: info.resource_name,
248 owner: info.owner,
249 description: info.description,
250 })
251 .into_iter()
252 .collect()
253 }
254 Error::Http { body, .. } => http_resource_info(body),
255 _ => Vec::new(),
256 }
257 }
258
259 /// The machine-readable identity of the failure: Canton's error `reason`
260 /// (e.g. `DUPLICATE_COMMAND`) plus its context `metadata`. Prefer it over
261 /// string-matching [`Display`](std::fmt::Display) output.
262 ///
263 /// Available on **either** transport. gRPC carries it as a
264 /// `google.rpc.ErrorInfo` detail; the JSON Ledger API spells the same two
265 /// things as `code` and `context`, and this reads whichever is there. That
266 /// matters because the alternative on the JSON lane was the string matching
267 /// this method exists to replace.
268 ///
269 /// `None` when the participant published no identity to report: a transport
270 /// failure, a status without the detail, or a **redacted** error — Canton
271 /// answers a security-sensitive failure with the literal `"NA"`, which is
272 /// the absence of an error id rather than an error id.
273 ///
274 /// `domain` is empty on the JSON lane, and Canton leaves it empty on gRPC
275 /// too.
276 #[must_use]
277 pub fn error_info(&self) -> Option<ErrorInfo> {
278 match self {
279 Error::Status(status) => {
280 use tonic_types::StatusExt as _;
281 status
282 .get_error_details()
283 .error_info()
284 .map(|info| ErrorInfo {
285 reason: info.reason.clone(),
286 domain: info.domain.clone(),
287 metadata: info.metadata.clone(),
288 })
289 }
290 Error::Http { body, .. } => http_error_info(body),
291 _ => None,
292 }
293 }
294}
295
296/// The category of the status's `ErrorInfo`, when present and recognized.
297/// Whether a status is a connection that died *mid-call*, rather than a verdict
298/// the participant returned.
299///
300/// tonic maps a broken stream or a dropped unary connection to a `Status` with
301/// code `Unknown` (message "transport error" / "h2 protocol error"), not to a
302/// `transport::Error` — that variant is only for connection *establishment*. So
303/// a real drop reaches [`Error::is_retriable`] as a category-less `Unknown` and,
304/// without this, is treated as terminal: the resumable stream gives up on the
305/// first blip and the retry pipeline never fires on the most ordinary failure
306/// there is.
307///
308/// The signal is the source chain, not the message text (which is
309/// tonic-version-specific). A client-side transport failure carries a `source`
310/// — tonic built the status *from* the underlying h2/hyper/io error. A status
311/// decoded from response trailers, which is how a genuine application `Unknown`
312/// arrives, has none. So `Unknown` (or `Internal`, tonic's other transport-error
313/// code) with a source present is a dropped connection; the same code without a
314/// source is the server's own answer and stays terminal.
315fn is_transport_death(status: &tonic::Status) -> bool {
316 use std::error::Error as _;
317 matches!(status.code(), tonic::Code::Unknown | tonic::Code::Internal)
318 && status.source().is_some()
319}
320
321fn status_category(status: &tonic::Status) -> Option<ErrorCategory> {
322 use tonic_types::StatusExt as _;
323 let info = status.get_details_error_info()?;
324 ErrorCategory::from_i32(info.metadata.get("category")?.parse().ok()?)
325}
326
327/// The `RetryInfo.retry_delay` of a status, when present.
328fn status_retry_delay(status: &tonic::Status) -> Option<std::time::Duration> {
329 use tonic_types::StatusExt as _;
330 status.get_details_retry_info()?.retry_delay
331}
332
333/// The `errorCategory` of a JSON Ledger API error body, when present and
334/// recognized. Non-JSON bodies (a proxy's HTML error page, a token endpoint)
335/// simply yield `None`.
336fn http_category(body: &str) -> Option<ErrorCategory> {
337 let body: serde_json::Value = serde_json::from_str(body).ok()?;
338 let id = body.get("errorCategory")?.as_i64()?;
339 ErrorCategory::from_i32(i32::try_from(id).ok()?)
340}
341
342/// The `retryInfo` of a JSON Ledger API error body, when present. The field
343/// is a human-readable duration (e.g. `"1 second"`, `"250 milliseconds"`).
344fn http_retry_delay(body: &str) -> Option<std::time::Duration> {
345 let body: serde_json::Value = serde_json::from_str(body).ok()?;
346 parse_spelled_duration(body.get("retryInfo")?.as_str()?)
347}
348
349/// The `resources` of a JSON Ledger API error body: an array of `[type, name]`
350/// pairs (e.g. `[["ErrorResource(CONTRACT_ID)", "00abc…"]]`). Entries that are
351/// not a pair of strings are skipped rather than failing the whole read — a
352/// diagnostic must not itself become an error.
353fn http_resource_info(body: &str) -> Vec<ResourceInfo> {
354 let Ok(body) = serde_json::from_str::<serde_json::Value>(body) else {
355 return Vec::new();
356 };
357 let Some(resources) = body.get("resources").and_then(serde_json::Value::as_array) else {
358 return Vec::new();
359 };
360 resources
361 .iter()
362 .filter_map(|entry| {
363 let pair = entry.as_array()?;
364 Some(ResourceInfo {
365 resource_type: pair.first()?.as_str()?.to_string(),
366 resource_name: pair.get(1)?.as_str()?.to_string(),
367 owner: String::new(),
368 description: String::new(),
369 })
370 })
371 .collect()
372}
373
374/// The `grpcCodeValue` of a JSON Ledger API error body — the numeric gRPC code
375/// the participant would have answered with over the other transport.
376///
377/// Only reported when the field is actually there: `tonic::Code::from` maps an
378/// unknown number to `Unknown`, and manufacturing `Unknown` for a body that
379/// never carried a code would claim a verdict the participant never gave.
380fn http_grpc_code(body: &str) -> Option<tonic::Code> {
381 let body: serde_json::Value = serde_json::from_str(body).ok()?;
382 let value = body.get("grpcCodeValue")?.as_i64()?;
383 Some(tonic::Code::from(i32::try_from(value).ok()?))
384}
385
386/// The `code` and `context` of a JSON Ledger API error body, as the
387/// `ErrorInfo` the gRPC lane carries as a status detail.
388///
389/// `"NA"` is Canton's placeholder on a redacted (security-sensitive) error and
390/// is treated as no id at all — reporting it would hand the caller a string
391/// that looks like an error code and matches nothing.
392///
393/// A context value that is not a string keeps its JSON spelling rather than
394/// being dropped: `metadata` is a string map, and losing a field silently is
395/// worse than rendering it.
396fn http_error_info(body: &str) -> Option<ErrorInfo> {
397 let body: serde_json::Value = serde_json::from_str(body).ok()?;
398 let reason = body.get("code")?.as_str()?;
399 if reason.is_empty() || reason == "NA" {
400 return None;
401 }
402 let metadata = body
403 .get("context")
404 .and_then(serde_json::Value::as_object)
405 .map(|context| {
406 context
407 .iter()
408 .map(|(key, value)| {
409 let value = match value.as_str() {
410 Some(text) => text.to_string(),
411 None => value.to_string(),
412 };
413 (key.clone(), value)
414 })
415 .collect()
416 })
417 .unwrap_or_default();
418 Some(ErrorInfo {
419 reason: reason.to_string(),
420 // Canton sets no domain on either transport, and the JSON body has no
421 // field for one. Empty here says the same thing gRPC says.
422 domain: String::new(),
423 metadata,
424 })
425}
426
427/// The `correlationId` (or, failing that, `traceId`) of a JSON Ledger API
428/// error body, when present.
429fn http_correlation_id(body: &str) -> Option<String> {
430 let body: serde_json::Value = serde_json::from_str(body).ok()?;
431 ["correlationId", "traceId"]
432 .iter()
433 .find_map(|key| Some(body.get(key)?.as_str()?.to_string()))
434}
435
436/// Parse a `"<number> <unit>"` duration as the JSON API spells `retryInfo`
437/// (Scala `Duration#toString`: `"1 second"`, `"5 seconds"`, …).
438fn parse_spelled_duration(text: &str) -> Option<std::time::Duration> {
439 let mut words = text.split_whitespace();
440 let amount: f64 = words.next()?.parse().ok()?;
441 let unit = words.next()?;
442 if words.next().is_some() {
443 return None;
444 }
445 let seconds = match unit.strip_suffix('s').unwrap_or(unit) {
446 "day" => amount * 86_400.0,
447 "hour" => amount * 3_600.0,
448 "minute" => amount * 60.0,
449 "second" => amount,
450 "millisecond" => amount / 1e3,
451 "microsecond" => amount / 1e6,
452 "nanosecond" => amount / 1e9,
453 _ => return None,
454 };
455 // `from_secs_f64` panics on a value a `Duration` cannot hold, and this
456 // number arrives from the server: a `retryInfo` of `"1e300 seconds"` — or
457 // an ordinary-looking `"1e15 days"` — would abort the caller's process
458 // inside error classification, the one place that must stay infallible.
459 // `try_from_secs_f64` rejects those the same way it rejects the NaN and
460 // negative values this guarded against before.
461 std::time::Duration::try_from_secs_f64(seconds).ok()
462}
463
464/// Canton's error categories — the coarse classification every Ledger API
465/// error carries (`ErrorInfo.metadata["category"]`), which the [error-code
466/// documentation] defines retryability on. `#[non_exhaustive]`: Canton may add
467/// categories.
468///
469/// The variants are the categories of the Canton 3.x docs, by their stable
470/// numeric ids (13 is a server-log-only warning that never reaches the API).
471///
472/// [error-code documentation]: https://docs.daml.com/canton/reference/error_codes.html
473#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
474#[non_exhaustive]
475pub enum ErrorCategory {
476 /// 1 — a service was momentarily unavailable; the request may or may not
477 /// have been processed. Retry with backoff.
478 TransientServerFailure,
479 /// 2 — contention on shared resources (locks, rate limits, a locked
480 /// contract). Retry with backoff.
481 ContentionOnSharedResources,
482 /// 3 — the request's deadline expired with its outcome unknown. Retry a
483 /// bounded number of times, relying on command deduplication.
484 DeadlineExceededRequestStateUnknown,
485 /// 4 — a system-internal invariant was violated (implementation bug or
486 /// data corruption). Not retriable; needs operator/vendor attention.
487 SystemInternalAssumptionViolated,
488 /// 5 — a potential attack or faulty peer was detected; details are
489 /// deliberately withheld. Not retriable.
490 SecurityAlert,
491 /// 6 — missing or invalid authentication credentials. Not retriable
492 /// until the credentials are fixed.
493 AuthInterceptorInvalidAuthenticationCredentials,
494 /// 7 — authenticated, but not permitted to perform the operation. Not
495 /// retriable until permissions change.
496 InsufficientPermission,
497 /// 8 — the request is invalid regardless of system state (malformed
498 /// arguments, size limits). Not retriable.
499 InvalidIndependentOfSystemState,
500 /// 9 — the current ledger state does not satisfy the request's
501 /// preconditions (Daml interpretation failures land here). Not blindly
502 /// retriable; needs an application-level strategy.
503 InvalidGivenCurrentSystemStateOther,
504 /// 10 — a referenced resource already exists (e.g. a duplicate command).
505 /// Not retriable as-is.
506 InvalidGivenCurrentSystemStateResourceExists,
507 /// 11 — a referenced resource does not exist (contract, package, party).
508 /// Not retriable as-is.
509 InvalidGivenCurrentSystemStateResourceMissing,
510 /// 12 — the request reads past the current ledger end. Retriable: the
511 /// system may naturally progress to make it valid.
512 InvalidGivenCurrentSystemStateSeekAfterEnd,
513 /// 14 — the operation is not implemented / not enabled on this node. Not
514 /// retriable.
515 InternalUnsupportedOperation,
516}
517
518/// The std spelling of [`ErrorCategory::from_i32`], so the category can be
519/// used with generic conversion bounds. The inherent method stays: it is the
520/// documented name and the `Option` shape matches "unknown id" better than an
521/// error type would.
522impl TryFrom<i32> for ErrorCategory {
523 type Error = i32;
524 fn try_from(id: i32) -> std::result::Result<Self, i32> {
525 Self::from_i32(id).ok_or(id)
526 }
527}
528
529impl From<ErrorCategory> for i32 {
530 fn from(category: ErrorCategory) -> i32 {
531 category.as_i32()
532 }
533}
534
535impl ErrorCategory {
536 /// The category for Canton's numeric id, `None` when unrecognized.
537 #[must_use]
538 pub const fn from_i32(id: i32) -> Option<Self> {
539 Some(match id {
540 1 => Self::TransientServerFailure,
541 2 => Self::ContentionOnSharedResources,
542 3 => Self::DeadlineExceededRequestStateUnknown,
543 4 => Self::SystemInternalAssumptionViolated,
544 5 => Self::SecurityAlert,
545 6 => Self::AuthInterceptorInvalidAuthenticationCredentials,
546 7 => Self::InsufficientPermission,
547 8 => Self::InvalidIndependentOfSystemState,
548 9 => Self::InvalidGivenCurrentSystemStateOther,
549 10 => Self::InvalidGivenCurrentSystemStateResourceExists,
550 11 => Self::InvalidGivenCurrentSystemStateResourceMissing,
551 12 => Self::InvalidGivenCurrentSystemStateSeekAfterEnd,
552 14 => Self::InternalUnsupportedOperation,
553 _ => return None,
554 })
555 }
556
557 /// Canton's numeric id for this category.
558 #[must_use]
559 pub const fn as_i32(self) -> i32 {
560 match self {
561 Self::TransientServerFailure => 1,
562 Self::ContentionOnSharedResources => 2,
563 Self::DeadlineExceededRequestStateUnknown => 3,
564 Self::SystemInternalAssumptionViolated => 4,
565 Self::SecurityAlert => 5,
566 Self::AuthInterceptorInvalidAuthenticationCredentials => 6,
567 Self::InsufficientPermission => 7,
568 Self::InvalidIndependentOfSystemState => 8,
569 Self::InvalidGivenCurrentSystemStateOther => 9,
570 Self::InvalidGivenCurrentSystemStateResourceExists => 10,
571 Self::InvalidGivenCurrentSystemStateResourceMissing => 11,
572 Self::InvalidGivenCurrentSystemStateSeekAfterEnd => 12,
573 Self::InternalUnsupportedOperation => 14,
574 }
575 }
576
577 /// Whether the error-code documentation classifies this category as
578 /// retryable (transient failures, contention, unknown-outcome deadlines,
579 /// and reads past the ledger end).
580 #[must_use]
581 pub const fn is_retriable(self) -> bool {
582 matches!(
583 self,
584 Self::TransientServerFailure
585 | Self::ContentionOnSharedResources
586 | Self::DeadlineExceededRequestStateUnknown
587 | Self::InvalidGivenCurrentSystemStateSeekAfterEnd
588 )
589 }
590}
591
592/// Structured `google.rpc.ErrorInfo` details from a gRPC status: the machine-
593/// readable `reason`, its `domain`, and error `metadata`. `#[non_exhaustive]`.
594#[derive(Clone, Debug, Default, PartialEq, Eq)]
595#[non_exhaustive]
596pub struct ErrorInfo {
597 /// Machine-readable error reason (e.g. a Canton/Daml error code).
598 pub reason: String,
599 /// The logical grouping the `reason` belongs to.
600 pub domain: String,
601 /// Additional structured context for the error.
602 pub metadata: std::collections::HashMap<String, String>,
603}
604
605/// A resource a failure is about, from a `google.rpc.ResourceInfo` detail on a
606/// gRPC status or an entry of a JSON API error body's `resources`.
607/// `#[non_exhaustive]`.
608#[derive(Clone, Debug, Default, PartialEq, Eq)]
609#[non_exhaustive]
610pub struct ResourceInfo {
611 /// What kind of thing it is, as Canton names it (e.g.
612 /// `ErrorResource(CONTRACT_ID)`).
613 pub resource_type: String,
614 /// The identifier itself — a contract id, package name, party, …
615 pub resource_name: String,
616 /// The owner, when the server reports one. Empty on the JSON transport,
617 /// whose `resources` entries carry only the type and the name.
618 pub owner: String,
619 /// A human-readable note, when the server reports one. Empty on the JSON
620 /// transport for the same reason.
621 pub description: String,
622}
623
624/// Most transport errors keep the sentence that matters in their source chain
625/// and print only a generic outer line: `reqwest` says `error sending request
626/// for url (…)` while `invalid peer certificate: UnknownIssuer` sits one level
627/// down, and `tokio_postgres` says `db error` over `FATAL: password
628/// authentication failed`. Reporting only the outer message hides the one
629/// thing an operator needs.
630///
631/// Lives here because four crates were each losing it separately, and three of
632/// them had grown their own private copy of this function before anyone noticed
633/// the fourth still had none.
634#[must_use]
635pub fn chain(error: &dyn std::error::Error) -> String {
636 let mut message = error.to_string();
637 let mut source = error.source();
638 while let Some(cause) = source {
639 message.push_str(": ");
640 message.push_str(&cause.to_string());
641 source = cause.source();
642 }
643 message
644}
645
646impl From<tonic::Status> for Error {
647 fn from(status: tonic::Status) -> Self {
648 Error::Status(Box::new(status))
649 }
650}
651
652impl From<tonic::transport::Error> for Error {
653 fn from(err: tonic::transport::Error) -> Self {
654 Error::Transport(Box::new(err))
655 }
656}
657
658impl From<serde_json::Error> for Error {
659 fn from(err: serde_json::Error) -> Self {
660 Error::Json(Box::new(err))
661 }
662}
663
664/// SDK-wide result alias. Re-exported by the facade as `canton::Result`.
665pub type Result<T, E = Error> = std::result::Result<T, E>;
666
667#[cfg(test)]
668#[allow(clippy::unwrap_used, clippy::expect_used)]
669mod tests {
670 use super::*;
671
672 #[test]
673 fn error_info_is_extracted_from_a_status_and_absent_otherwise() {
674 use tonic_types::{ErrorDetails, StatusExt as _};
675
676 let mut metadata = std::collections::HashMap::new();
677 metadata.insert("resource".to_string(), "contract-1".to_string());
678 let details = ErrorDetails::with_error_info("DUPLICATE_COMMAND", "canton", metadata);
679 let status = tonic::Status::with_error_details(tonic::Code::AlreadyExists, "dup", details);
680
681 let info = Error::from(status)
682 .error_info()
683 .expect("error info present");
684 assert_eq!(info.reason, "DUPLICATE_COMMAND");
685 assert_eq!(info.domain, "canton");
686 assert_eq!(
687 info.metadata.get("resource").map(String::as_str),
688 Some("contract-1")
689 );
690
691 // A status without ErrorInfo, and a non-status error, yield None.
692 assert!(
693 Error::from(tonic::Status::not_found("x"))
694 .error_info()
695 .is_none()
696 );
697 assert!(Error::Timeout.error_info().is_none());
698 }
699
700 /// A synthetic Canton-style status: `ErrorInfo` with a `category` metadata
701 /// entry, `RequestInfo` with the correlation id, and — when `delay` is set —
702 /// a `RetryInfo` recommendation.
703 fn canton_status(
704 code: tonic::Code,
705 category: i32,
706 delay: Option<std::time::Duration>,
707 ) -> tonic::Status {
708 use tonic_types::{ErrorDetails, StatusExt as _};
709
710 let mut metadata = std::collections::HashMap::new();
711 metadata.insert("category".to_string(), category.to_string());
712 let mut details = ErrorDetails::with_error_info("SOME_ERROR_CODE", "participant", metadata);
713 details.set_request_info("corr-1234", "");
714 if let Some(delay) = delay {
715 details.set_retry_info(Some(delay));
716 }
717 tonic::Status::with_error_details(code, "boom", details)
718 }
719
720 #[test]
721 fn the_canton_category_decides_retryability_over_the_grpc_code() {
722 use std::time::Duration;
723
724 // ABORTED is transient by code — but category 10 (resource exists,
725 // e.g. a duplicate change id) says no. The category wins.
726 let err = Error::from(canton_status(tonic::Code::Aborted, 10, None));
727 assert_eq!(
728 err.category(),
729 Some(ErrorCategory::InvalidGivenCurrentSystemStateResourceExists)
730 );
731 assert!(!err.is_retriable());
732
733 // OUT_OF_RANGE is not transient by code — but category 12 (seek past
734 // the ledger end) says retry. The category wins again.
735 let err = Error::from(canton_status(
736 tonic::Code::OutOfRange,
737 12,
738 Some(Duration::from_secs(1)),
739 ));
740 assert_eq!(
741 err.category(),
742 Some(ErrorCategory::InvalidGivenCurrentSystemStateSeekAfterEnd)
743 );
744 assert!(err.is_retriable());
745 assert_eq!(err.retry_delay(), Some(Duration::from_secs(1)));
746 }
747
748 #[test]
749 fn correlation_id_and_retry_delay_are_extracted() {
750 use std::time::Duration;
751
752 let err = Error::from(canton_status(
753 tonic::Code::Unavailable,
754 1,
755 Some(Duration::from_millis(250)),
756 ));
757 assert_eq!(err.category(), Some(ErrorCategory::TransientServerFailure));
758 assert_eq!(err.correlation_id().as_deref(), Some("corr-1234"));
759 assert_eq!(err.retry_delay(), Some(Duration::from_millis(250)));
760
761 // Plain statuses and non-status errors carry none of it.
762 let plain = Error::from(tonic::Status::unavailable("x"));
763 assert_eq!(plain.category(), None);
764 assert_eq!(plain.correlation_id(), None);
765 assert_eq!(plain.retry_delay(), None);
766 assert_eq!(Error::Timeout.category(), None);
767 }
768
769 #[test]
770 fn statuses_without_a_category_fall_back_to_code_classification() {
771 use tonic_types::{ErrorDetails, StatusExt as _};
772
773 // No details at all: the transient codes still classify.
774 assert!(Error::from(tonic::Status::unavailable("x")).is_retriable());
775 assert!(!Error::from(tonic::Status::not_found("x")).is_retriable());
776
777 // An unrecognized category id is ignored (forward compatibility), and
778 // the code fallback applies.
779 let err = Error::from(canton_status(tonic::Code::Unavailable, 99, None));
780 assert_eq!(err.category(), None);
781 assert!(err.is_retriable());
782
783 // A bare RetryInfo (no category) is an explicit "retry me", even on a
784 // code the fallback would refuse.
785 let mut details = ErrorDetails::new();
786 details.set_retry_info(Some(std::time::Duration::from_secs(2)));
787 let status =
788 tonic::Status::with_error_details(tonic::Code::FailedPrecondition, "wait", details);
789 assert!(Error::from(status).is_retriable());
790 }
791
792 #[test]
793 fn json_api_error_bodies_classify_by_category() {
794 // A real body captured from a LocalNet participant (JSON Ledger API):
795 // category 12 (seek after end) is retryable although the HTTP status
796 // (400) is not in the transient set — the category verdict wins.
797 let body = r#"{
798 "code": "OFFSET_AFTER_LEDGER_END",
799 "cause": "Begin offset (999999999) is after ledger end (23577)",
800 "correlationId": null,
801 "traceId": "36a33702b2fa7908a7349be166ccfa38",
802 "context": {"participant": "'app-provider'", "category": "12"},
803 "resources": [],
804 "errorCategory": 12,
805 "grpcCodeValue": 11,
806 "retryInfo": "1 second",
807 "definiteAnswer": null
808 }"#;
809 let err = Error::Http {
810 status: 400,
811 body: body.to_string(),
812 url: None,
813 };
814 assert_eq!(
815 err.category(),
816 Some(ErrorCategory::InvalidGivenCurrentSystemStateSeekAfterEnd)
817 );
818 assert!(err.is_retriable());
819 assert_eq!(err.retry_delay(), Some(std::time::Duration::from_secs(1)));
820 // correlationId is null — falls back to traceId.
821 assert_eq!(
822 err.correlation_id().as_deref(),
823 Some("36a33702b2fa7908a7349be166ccfa38")
824 );
825
826 // The same failure over gRPC yields OutOfRange and an ErrorInfo naming
827 // OFFSET_AFTER_LEDGER_END. The JSON body spells both — `grpcCodeValue`
828 // and `code`/`context` — so both accessors must answer here too, or a
829 // caller on this transport is left with the string matching
830 // `error_info` exists to replace.
831 assert_eq!(err.code(), Some(tonic::Code::OutOfRange));
832 let info = err.error_info().expect("the body names the error");
833 assert_eq!(info.reason, "OFFSET_AFTER_LEDGER_END");
834 assert_eq!(
835 info.metadata.get("category").map(String::as_str),
836 Some("12")
837 );
838 assert_eq!(
839 info.metadata.get("participant").map(String::as_str),
840 Some("'app-provider'")
841 );
842
843 // A non-retryable category on a retryable-looking HTTP status: the
844 // category still wins (e.g. a 503 whose body says "invalid argument").
845 let err = Error::Http {
846 status: 503,
847 body: r#"{"errorCategory": 8}"#.to_string(),
848 url: None,
849 };
850 assert_eq!(
851 err.category(),
852 Some(ErrorCategory::InvalidIndependentOfSystemState)
853 );
854 assert!(!err.is_retriable());
855 }
856
857 #[test]
858 fn non_json_http_bodies_fall_back_to_status_code_classification() {
859 let retriable = Error::Http {
860 status: 503,
861 body: "<html>Service Unavailable</html>".to_string(),
862 url: None,
863 };
864 assert!(retriable.is_retriable());
865 assert_eq!(retriable.category(), None);
866 assert_eq!(retriable.retry_delay(), None);
867
868 let terminal = Error::Http {
869 status: 404,
870 body: String::new(),
871 url: None,
872 };
873 assert!(!terminal.is_retriable());
874 assert_eq!(terminal.correlation_id(), None);
875 }
876
877 #[test]
878 fn spelled_durations_parse_and_garbage_is_refused() {
879 use std::time::Duration;
880 for (text, expected) in [
881 ("1 second", Duration::from_secs(1)),
882 ("5 seconds", Duration::from_secs(5)),
883 ("250 milliseconds", Duration::from_millis(250)),
884 ("2 minutes", Duration::from_secs(120)),
885 ("1 hour", Duration::from_secs(3600)),
886 ("0.5 seconds", Duration::from_millis(500)),
887 // The rarer units and the day arm: mutation testing showed these
888 // conversion factors were never exercised, so a wrong multiplier
889 // in day/microsecond/nanosecond would have passed unnoticed.
890 ("2 days", Duration::from_secs(2 * 86_400)),
891 ("1 day", Duration::from_secs(86_400)),
892 ("500 microseconds", Duration::from_micros(500)),
893 ("250 nanoseconds", Duration::from_nanos(250)),
894 ] {
895 assert_eq!(parse_spelled_duration(text), Some(expected), "{text}");
896 }
897 // A server-supplied number too large for a `Duration` is refused, not
898 // panicked on: this runs while an error is being classified, and the
899 // value is whatever the participant put in the body.
900 for bad in [
901 "",
902 "soon",
903 "1",
904 "1 fortnight",
905 "-1 second",
906 "1 second ago",
907 "1e300 seconds",
908 "1e300 days",
909 "NaN seconds",
910 "inf seconds",
911 ] {
912 assert_eq!(parse_spelled_duration(bad), None, "{bad}");
913 }
914 }
915
916 #[test]
917 fn category_ids_round_trip_and_follow_the_docs_retryability() {
918 for id in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14] {
919 let category = ErrorCategory::from_i32(id).expect("known id");
920 assert_eq!(category.as_i32(), id);
921 // Per the error-code docs: 1, 2, 3 and 12 are the retryable ones.
922 assert_eq!(category.is_retriable(), matches!(id, 1 | 2 | 3 | 12));
923 }
924 assert_eq!(ErrorCategory::from_i32(0), None);
925 // 13 (BackgroundProcessDegradationWarning) never reaches the API.
926 assert_eq!(ErrorCategory::from_i32(13), None);
927 assert_eq!(ErrorCategory::from_i32(15), None);
928 }
929
930 /// Canton strips the detail from security-sensitive failures — auth,
931 /// permission, internal — and tells the caller to ask the operator. Every
932 /// accessor must degrade to "nothing" rather than mislead, and the one
933 /// thing that survives must keep surviving: without the correlation id
934 /// there is no way to ask.
935 ///
936 /// Both fixtures are the real shapes, taken from a live 3.5.7 participant
937 /// answering with an invalid token.
938 #[test]
939 fn a_redacted_status_yields_nothing_except_the_correlation_id() {
940 use tonic_types::{ErrorDetails, StatusExt as _};
941
942 // gRPC: no ErrorInfo, no RetryInfo, no ResourceInfo — only RequestInfo.
943 let mut details = ErrorDetails::new();
944 details.set_request_info("93199811c5b2090c51cf45fe8c88060c", "");
945 let status = tonic::Status::with_error_details(
946 tonic::Code::Unauthenticated,
947 "An error occurred. Please contact the operator and inquire about the request \
948 93199811c5b2090c51cf45fe8c88060c",
949 details,
950 );
951 let err = Error::from(status);
952
953 assert_eq!(err.category(), None, "a redacted status has no category");
954 assert_eq!(err.error_info(), None);
955 assert!(err.resource_info().is_empty());
956 assert_eq!(err.retry_delay(), None);
957 assert_eq!(
958 err.correlation_id().as_deref(),
959 Some("93199811c5b2090c51cf45fe8c88060c"),
960 "the correlation id is the only actionable thing left"
961 );
962 // Classification falls back to the code, which is the right answer:
963 // bad credentials will not become good on a retry.
964 assert!(!err.is_retriable());
965
966 // JSON: the category is reported as -1 rather than omitted, which must
967 // not be mistaken for a real category.
968 let body = r#"{"code":"NA","cause":"An error occurred. Please contact the operator",
969 "errorCategory":-1,"retryInfo":null,"resources":[],
970 "correlationId":"41f217564e4e76f6cbc853a94a82fa80",
971 "traceId":"41f217564e4e76f6cbc853a94a82fa80"}"#;
972 let err = Error::Http {
973 status: 401,
974 body: body.to_string(),
975 url: None,
976 };
977
978 assert_eq!(err.category(), None, "-1 is not a category");
979 assert!(err.resource_info().is_empty());
980 assert_eq!(err.retry_delay(), None);
981 // `"NA"` is Canton saying it withheld the id, not an id spelled "NA".
982 assert_eq!(err.error_info(), None, "\"NA\" is not an error id");
983 assert_eq!(
984 err.correlation_id().as_deref(),
985 Some("41f217564e4e76f6cbc853a94a82fa80")
986 );
987 assert!(!err.is_retriable(), "401 is not transient");
988 }
989
990 #[test]
991 fn resource_info_names_what_the_error_is_about() {
992 use tonic_types::{ErrorDetails, StatusExt as _};
993
994 // gRPC: one `google.rpc.ResourceInfo` detail is all tonic_types models.
995 let mut details = ErrorDetails::new();
996 details.set_resource_info("ErrorResource(CONTRACT_ID)", "00abc", "alice", "not found");
997 let status = tonic::Status::with_error_details(tonic::Code::NotFound, "gone", details);
998 let found = Error::from(status).resource_info();
999 assert_eq!(found.len(), 1);
1000 assert_eq!(found[0].resource_type, "ErrorResource(CONTRACT_ID)");
1001 assert_eq!(found[0].resource_name, "00abc");
1002 assert_eq!(found[0].owner, "alice");
1003
1004 // A status with no such detail yields nothing, not a default-filled entry.
1005 assert!(
1006 Error::from(tonic::Status::not_found("x"))
1007 .resource_info()
1008 .is_empty()
1009 );
1010 assert!(Error::Timeout.resource_info().is_empty());
1011 }
1012
1013 #[test]
1014 fn resource_info_reads_the_json_apis_resources_array() {
1015 // The body shape is verbatim from a live Canton 3.5.7 participant
1016 // answering an exercise on a contract that does not exist.
1017 let body = r#"{"code":"CONTRACT_NOT_FOUND","cause":"…","errorCategory":11,
1018 "resources":[["ErrorResource(CONTRACT_ID)","00ababab"]],"retryInfo":null}"#;
1019 let err = Error::Http {
1020 status: 404,
1021 body: body.to_string(),
1022 url: None,
1023 };
1024 let found = err.resource_info();
1025 assert_eq!(found.len(), 1);
1026 assert_eq!(found[0].resource_type, "ErrorResource(CONTRACT_ID)");
1027 assert_eq!(found[0].resource_name, "00ababab");
1028 // The JSON pairs carry no owner or description; they are empty, not absent.
1029 assert!(found[0].owner.is_empty());
1030
1031 // Several resources — contention names more than one, which is why this
1032 // returns a Vec and not an Option.
1033 let many = Error::Http {
1034 status: 409,
1035 body: r#"{"resources":[["A","1"],["B","2"]]}"#.to_string(),
1036 url: None,
1037 };
1038 assert_eq!(many.resource_info().len(), 2);
1039
1040 // A malformed entry is skipped, and an empty/absent array is not an error:
1041 // a diagnostic accessor must never itself fail.
1042 let ragged = Error::Http {
1043 status: 500,
1044 body: r#"{"resources":[["A"],["B","2"],42,null]}"#.to_string(),
1045 url: None,
1046 };
1047 assert_eq!(ragged.resource_info().len(), 1);
1048 for body in [r#"{"resources":[]}"#, "{}", "not json at all", ""] {
1049 let err = Error::Http {
1050 status: 500,
1051 body: body.to_string(),
1052 url: None,
1053 };
1054 assert!(err.resource_info().is_empty(), "{body}");
1055 }
1056 }
1057
1058 #[test]
1059 fn transient_conditions_are_retriable() {
1060 assert!(Error::Timeout.is_retriable());
1061 assert!(Error::Connection("reset".to_string()).is_retriable());
1062 assert!(Error::from(tonic::Status::unavailable("x")).is_retriable());
1063 assert!(Error::from(tonic::Status::deadline_exceeded("x")).is_retriable());
1064 assert!(Error::from(tonic::Status::resource_exhausted("x")).is_retriable());
1065 assert!(Error::from(tonic::Status::aborted("x")).is_retriable());
1066 }
1067
1068 #[test]
1069 fn a_connection_that_dies_mid_call_is_retriable_but_a_server_unknown_is_not() {
1070 use std::io;
1071
1072 // What tonic hands back when a stream or unary connection dies in
1073 // flight: code Unknown, built *from* the transport error, so the source
1074 // chain is populated. This is the ordinary dropped connection, and the
1075 // resumable stream and the retry pipeline both depend on it being
1076 // retriable — before this, they gave up on the first blip.
1077 let dropped = tonic::Status::from_error(Box::new(io::Error::new(
1078 io::ErrorKind::ConnectionReset,
1079 "h2 protocol error: error reading a body from connection",
1080 )));
1081 assert_eq!(
1082 dropped.code(),
1083 tonic::Code::Unknown,
1084 "from_error yields Unknown"
1085 );
1086 assert!(Error::from(dropped).is_retriable());
1087
1088 // A genuine `Unknown` the participant returned is decoded from response
1089 // trailers and carries no local source. That is the server's verdict,
1090 // and retrying it would just re-ask the same losing question.
1091 let server_unknown = tonic::Status::new(tonic::Code::Unknown, "business rule failed");
1092 assert!(!Error::from(server_unknown).is_retriable());
1093
1094 // `Internal` follows the same rule: transport-built is retriable, a
1095 // bare server Internal is not.
1096 assert!(
1097 Error::from(tonic::Status::from_error(Box::new(io::Error::other(
1098 "broken pipe"
1099 ))))
1100 .is_retriable()
1101 );
1102 assert!(!Error::from(tonic::Status::internal("server bug")).is_retriable());
1103 }
1104
1105 #[test]
1106 fn transient_http_codes_are_retriable_but_client_codes_are_not() {
1107 // The whole 5xx range is transient (per the doc), plus 408/429 — not just
1108 // a hand-picked subset. 501/509/511/520 were previously missed.
1109 for status in [
1110 408, 429, 500, 501, 502, 503, 504, 507, 509, 511, 520, 527, 599,
1111 ] {
1112 assert!(
1113 Error::Http {
1114 status,
1115 body: String::new(),
1116 url: None,
1117 }
1118 .is_retriable(),
1119 "http {status} should be retriable"
1120 );
1121 }
1122 // 4xx (incl. the JSON API's 413 too-large) stay non-retriable.
1123 for status in [400, 401, 403, 404, 409, 413, 422] {
1124 assert!(
1125 !Error::Http {
1126 status,
1127 body: String::new(),
1128 url: None,
1129 }
1130 .is_retriable(),
1131 "http {status} should not be retriable"
1132 );
1133 }
1134 }
1135
1136 #[test]
1137 fn definite_failures_are_not_retriable() {
1138 assert!(!Error::from(tonic::Status::not_found("x")).is_retriable());
1139 assert!(!Error::from(tonic::Status::already_exists("dup")).is_retriable());
1140 assert!(!Error::from(tonic::Status::invalid_argument("x")).is_retriable());
1141 assert!(!Error::InvalidRequest("x".to_string()).is_retriable());
1142 assert!(!Error::Auth("x".to_string()).is_retriable());
1143 assert!(
1144 !Error::CommandRejected {
1145 code: "GrpcStatus".to_string(),
1146 message: "boom".to_string()
1147 }
1148 .is_retriable()
1149 );
1150 assert!(!Error::UnexpectedResponse("x".to_string()).is_retriable());
1151 }
1152
1153 #[test]
1154 fn code_is_exposed_only_for_status_errors() {
1155 assert_eq!(
1156 Error::from(tonic::Status::not_found("x")).code(),
1157 Some(tonic::Code::NotFound)
1158 );
1159 assert_eq!(Error::Timeout.code(), None);
1160 assert_eq!(Error::Connection("x".to_string()).code(), None);
1161 assert_eq!(
1162 Error::Http {
1163 status: 503,
1164 body: String::new(),
1165 url: None,
1166 }
1167 .code(),
1168 None
1169 );
1170 }
1171
1172 #[test]
1173 fn display_messages_are_lowercase_and_informative() {
1174 assert_eq!(Error::Timeout.to_string(), "operation timed out");
1175 assert_eq!(
1176 Error::InvalidRequest("bad uri".to_string()).to_string(),
1177 "invalid request: bad uri"
1178 );
1179 assert_eq!(
1180 Error::Auth("token expired".to_string()).to_string(),
1181 "authentication failed: token expired"
1182 );
1183 assert_eq!(
1184 Error::Http {
1185 status: 503,
1186 body: "down".to_string(),
1187 url: None,
1188 }
1189 .to_string(),
1190 "http 503: down"
1191 );
1192 assert_eq!(
1193 Error::CommandRejected {
1194 code: "INVALID_ARGUMENT".to_string(),
1195 message: "nope".to_string()
1196 }
1197 .to_string(),
1198 "command rejected (INVALID_ARGUMENT): nope"
1199 );
1200 }
1201}