Skip to main content

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