nats_counters/
errors.rs

1// Copyright 2025 Synadia Communications Inc.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use std::fmt;
15
16/// Error type for counter operations.
17pub type CounterError = async_nats::error::Error<CounterErrorKind>;
18
19/// Result type for counter operations.
20pub type Result<T> = std::result::Result<T, CounterError>;
21
22/// Kinds of errors that can occur when working with NATS counters.
23#[derive(Debug, Clone, PartialEq)]
24pub enum CounterErrorKind {
25    /// Stream is not configured for counters (AllowMsgCounter must be true).
26    CounterNotEnabled,
27    /// Stream must be configured for direct access (AllowDirect must be true).
28    DirectAccessRequired,
29    /// Invalid counter value.
30    InvalidCounterValue,
31    /// Counter stream not found.
32    CounterNotFound,
33    /// Counter not initialized for the given subject.
34    NoCounterForSubject,
35    /// Invalid counter value in response.
36    InvalidResponseValue,
37    /// Missing counter value in response.
38    MissingResponseValue,
39    /// Failed to parse counter sources.
40    InvalidSources,
41    /// JSON parsing error.
42    Serialization,
43    /// Stream operation error.
44    Stream,
45    Publish,
46    GetStream,
47    Request,
48    Other,
49}
50
51impl fmt::Display for CounterErrorKind {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::CounterNotEnabled => {
55                write!(
56                    f,
57                    "stream is not configured for counters (AllowMsgCounter must be true)"
58                )
59            }
60            Self::DirectAccessRequired => {
61                write!(
62                    f,
63                    "stream must be configured for direct access (AllowDirect must be true)"
64                )
65            }
66            Self::InvalidCounterValue => write!(f, "invalid counter value"),
67            Self::CounterNotFound => write!(f, "counter not found"),
68            Self::NoCounterForSubject => write!(f, "counter not initialized for subject"),
69            Self::InvalidResponseValue => write!(f, "invalid counter value in response"),
70            Self::MissingResponseValue => write!(f, "counter increment response missing value"),
71            Self::InvalidSources => write!(f, "failed to parse counter sources"),
72            Self::Serialization => write!(f, "serialization error"),
73            Self::Stream => write!(f, "stream operation error"),
74            Self::Publish => write!(f, "publish operation error"),
75            Self::GetStream => write!(f, "get stream error"),
76            Self::Request => write!(f, "request error"),
77            Self::Other => write!(f, "other error"),
78        }
79    }
80}