Skip to main content

provide_telemetry/
errors.rs

1// SPDX-FileCopyrightText: Copyright (C) 2026 provide.io llc
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-Comment: Part of provide-telemetry.
4//
5
6/// Why a [`TelemetryError`] was raised. The crate's public functions all return
7/// `TelemetryError`, so the kind is what lets a caller act on a specific failure —
8/// the Rust analogue of Python's `except ProviderImmutableError` and Go's
9/// `errors.As(err, &*ProviderImmutableError)`.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub enum TelemetryErrorKind {
12    #[default]
13    General,
14    /// A provider-changing reconfiguration rejected because providers are live.
15    /// The process must restart to apply it.
16    ProviderImmutable,
17}
18
19#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
20#[error("{message}")]
21pub struct TelemetryError {
22    pub message: String,
23    pub kind: TelemetryErrorKind,
24}
25
26impl TelemetryError {
27    pub fn new(message: impl Into<String>) -> Self {
28        Self {
29            message: message.into(),
30            kind: TelemetryErrorKind::General,
31        }
32    }
33
34    /// True when this error reports a rejected provider-changing reconfiguration.
35    pub fn is_provider_immutable(&self) -> bool {
36        self.kind == TelemetryErrorKind::ProviderImmutable
37    }
38}
39
40impl From<ProviderImmutableError> for TelemetryError {
41    fn from(err: ProviderImmutableError) -> Self {
42        Self {
43            message: err.message,
44            kind: TelemetryErrorKind::ProviderImmutable,
45        }
46    }
47}
48
49#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
50#[error("{message}")]
51pub struct ConfigurationError {
52    pub message: String,
53}
54
55impl ConfigurationError {
56    pub fn new(message: impl Into<String>) -> Self {
57        Self {
58            message: message.into(),
59        }
60    }
61}
62
63#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
64#[error("{message}")]
65pub struct EventSchemaError {
66    pub message: String,
67}
68
69impl EventSchemaError {
70    pub fn new(message: impl Into<String>) -> Self {
71        Self {
72            message: message.into(),
73        }
74    }
75}
76
77#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
78#[error("{message}")]
79pub struct ProviderImmutableError {
80    pub message: String,
81}
82
83impl ProviderImmutableError {
84    pub fn new(message: impl Into<String>) -> Self {
85        Self {
86            message: message.into(),
87        }
88    }
89}
90
91#[allow(non_camel_case_types)]
92pub type provider_immutable_error = ProviderImmutableError;