Skip to main content

dynamo_runtime/pipeline/
error.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Pipeline Error
5//
6use async_nats::error::Error as NatsError;
7
8pub use anyhow::{Context, Error, Result, anyhow, anyhow as error, bail, ensure};
9
10pub trait PipelineErrorExt {
11    /// Downcast the [`Error`] to a [`PipelineError`]
12    fn try_into_pipeline_error(self) -> Result<PipelineError, Error>;
13
14    /// If the [`Error`] can be downcast to a [`PipelineError`], then the left variant is returned,
15    /// otherwise the right variant is returned.
16    fn either_pipeline_error(self) -> either::Either<PipelineError, Error>;
17}
18
19impl PipelineErrorExt for Error {
20    fn try_into_pipeline_error(self) -> Result<PipelineError, Error> {
21        self.downcast::<PipelineError>()
22    }
23
24    fn either_pipeline_error(self) -> either::Either<PipelineError, Error> {
25        match self.downcast::<PipelineError>() {
26            Ok(err) => either::Left(err),
27            Err(err) => either::Right(err),
28        }
29    }
30}
31
32#[derive(Debug, thiserror::Error)]
33pub enum PipelineError {
34    /// For starter, to remove as code matures.
35    #[error("Generic error: {0}")]
36    Generic(String),
37
38    /// Edges can only be set once. This error is thrown on subsequent attempts to set an edge.s
39    #[error("Link failed: Edge already set")]
40    EdgeAlreadySet,
41
42    /// The source node is not connected to an edge.
43    #[error("Disconnected source; no edge on which to send data")]
44    NoEdge,
45
46    #[error("SegmentSink is not connected to an EgressPort")]
47    NoNetworkEdge,
48
49    /// In the interim between when a request was made and when the stream was received, the
50    /// requesting task was dropped. This maybe a logic error in the pipeline; and become a
51    /// panic/fatal error in the future. This error is thrown when the `on_data` method of a
52    /// terminating sink either cannot find the `oneshot` channel sender or the corresponding
53    /// receiver was dropped
54    #[error("Unlinked request; initiating request task was dropped or cancelled")]
55    DetachedStreamReceiver,
56
57    // In the interim between when a response was made and when the stream was received, the
58    // Sender for the stream was dropped. This maybe a logic error in the pipeline; and become a
59    // panic/fatal error in the future.
60    #[error("Unlinked response; response task was dropped or cancelled")]
61    DetachedStreamSender,
62
63    /// A request was issued while another in-flight request through the same source already
64    /// uses this id. Responses are routed back by id, so the new request is refused.
65    #[error("Duplicate in-flight request id: {0}")]
66    DuplicateRequestId(String),
67
68    #[error("Serialzation Error: {0}")]
69    SerializationError(String),
70
71    #[error("Deserialization Error: {0}")]
72    DeserializationError(String),
73
74    #[error("Failed to issue request to the control plane: {0}")]
75    ControlPlaneRequestError(String),
76
77    #[error("Failed to establish a streaming connection: {0}")]
78    ConnectionFailed(String),
79
80    #[error("Generate Error: {0}")]
81    GenerateError(Error),
82
83    #[error("An endpoint URL must have the format: namespace/component/endpoint")]
84    InvalidEndpointFormat,
85
86    #[error("NATS Request Error: {0}")]
87    NatsRequestError(#[from] NatsError<async_nats::jetstream::context::RequestErrorKind>),
88
89    #[error("NATS Get Stream Error: {0}")]
90    NatsGetStreamError(#[from] NatsError<async_nats::jetstream::context::GetStreamErrorKind>),
91
92    #[error("NATS Create Stream Error: {0}")]
93    NatsCreateStreamError(#[from] NatsError<async_nats::jetstream::context::CreateStreamErrorKind>),
94
95    #[error("NATS Consumer Error: {0}")]
96    NatsConsumerError(#[from] NatsError<async_nats::jetstream::stream::ConsumerErrorKind>),
97
98    #[error("NATS Batch Error: {0}")]
99    NatsBatchError(#[from] NatsError<async_nats::jetstream::consumer::pull::BatchErrorKind>),
100
101    #[error("NATS Publish Error: {0}")]
102    NatsPublishError(#[from] NatsError<async_nats::client::PublishErrorKind>),
103
104    #[error("NATS Connect Error: {0}")]
105    NatsConnectError(#[from] NatsError<async_nats::ConnectErrorKind>),
106
107    #[error("NATS Subscriber Error: {0}")]
108    NatsSubscriberError(#[from] async_nats::SubscribeError),
109
110    #[error("Local IP Address Error: {0}")]
111    LocalIpAddressError(#[from] local_ip_address::Error),
112
113    #[error("Prometheus Error: {0}")]
114    PrometheusError(#[from] prometheus::Error),
115
116    #[error("Other NATS Error: {0}")]
117    NatsError(#[from] Box<dyn std::error::Error + Send + Sync>),
118
119    #[error("Two Part Codec Error: {0}")]
120    TwoPartCodec(#[from] TwoPartCodecError),
121
122    #[error("Serde Json Error: {0}")]
123    SerdeJsonError(#[from] serde_json::Error),
124
125    #[error("NATS KV Err: {0} for bucket '{1}")]
126    KeyValueError(String, String),
127
128    /// All instances are busy and cannot handle new requests
129    #[error("Service temporarily unavailable: {0}")]
130    ServiceOverloaded(String),
131}
132
133#[derive(Debug, thiserror::Error)]
134pub enum TwoPartCodecError {
135    #[error("I/O error: {0}")]
136    Io(#[from] std::io::Error),
137
138    #[error("Message size {0} exceeds the maximum allowed size of {1} bytes")]
139    MessageTooLarge(usize, usize),
140
141    #[error("Invalid message: {0}")]
142    InvalidMessage(String),
143
144    #[error("Checksum mismatch")]
145    ChecksumMismatch,
146}