Skip to main content

rustfs_notify/
error.rs

1//  Copyright 2024 RustFS Team
2//
3//  Licensed under the Apache License, Version 2.0 (the "License");
4//  you may not use this file except in compliance with the License.
5//  You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14
15use rustfs_targets::{TargetError, arn::TargetID};
16use std::io;
17use thiserror::Error;
18use tokio::task::JoinError;
19
20/// Errors related to the notification system's lifecycle.
21#[derive(Debug, Error)]
22pub enum LifecycleError {
23    /// Error indicating the system has already been initialized.
24    #[error("System has already been initialized")]
25    AlreadyInitialized,
26
27    /// Error indicating the system has not been initialized yet.
28    #[error("System has not been initialized")]
29    NotInitialized,
30}
31
32/// Error types for the notification system
33#[derive(Debug, Error)]
34pub enum NotificationError {
35    #[error("Target error: {0}")]
36    Target(#[from] TargetError),
37
38    #[error("Configuration error: {0}")]
39    Configuration(String),
40
41    #[error("ARN not found: {0}")]
42    ARNNotFound(String),
43
44    #[error("Invalid ARN: {0}")]
45    InvalidARN(String),
46
47    #[error("Bucket notification error: {0}")]
48    BucketNotification(String),
49
50    #[error("Rule configuration error: {0}")]
51    RuleConfiguration(String),
52
53    #[error("System lifecycle error: {0}")]
54    Lifecycle(#[from] LifecycleError),
55
56    #[error("I/O error: {0}")]
57    Io(io::Error),
58
59    #[error("Failed to read configuration: {0}")]
60    ReadConfig(String),
61
62    #[error("Failed to save configuration: {0}")]
63    SaveConfig(String),
64
65    #[error("Target '{0}' not found")]
66    TargetNotFound(TargetID),
67
68    #[error("System initialization error: {0}")]
69    Initialization(String),
70
71    #[error("Storage not available: {0}")]
72    StorageNotAvailable(String),
73}
74
75pub(crate) fn transition_join_error(error: JoinError) -> NotificationError {
76    let reason = if error.is_cancelled() { "cancelled" } else { "panicked" };
77    NotificationError::Initialization(format!("Notification lifecycle transition task {reason}"))
78}
79
80#[cfg(test)]
81mod tests {
82    use super::transition_join_error;
83
84    #[tokio::test]
85    async fn transition_join_error_does_not_expose_panic_payload() {
86        let join_error = tokio::spawn(async { panic!("do-not-expose-payload") })
87            .await
88            .expect_err("test task should panic");
89
90        let rendered = transition_join_error(join_error).to_string();
91        assert!(rendered.contains("panicked"));
92        assert!(!rendered.contains("do-not-expose-payload"));
93    }
94}