Skip to main content

credstore_sdk/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur during credential store operations.
4#[derive(Debug, Error)]
5pub enum CredStoreError {
6    #[error("invalid secret reference: {reason}")]
7    InvalidSecretRef { reason: String },
8
9    #[error("secret not found")]
10    NotFound,
11
12    #[error("no plugin available")]
13    NoPluginAvailable,
14
15    #[error("service unavailable: {0}")]
16    ServiceUnavailable(String),
17
18    #[error("internal error: {0}")]
19    Internal(String),
20}
21
22impl CredStoreError {
23    #[must_use]
24    pub fn invalid_ref(reason: impl Into<String>) -> Self {
25        Self::InvalidSecretRef {
26            reason: reason.into(),
27        }
28    }
29
30    #[must_use]
31    pub fn service_unavailable(msg: impl Into<String>) -> Self {
32        Self::ServiceUnavailable(msg.into())
33    }
34
35    #[must_use]
36    pub fn internal(msg: impl Into<String>) -> Self {
37        Self::Internal(msg.into())
38    }
39}
40
41#[cfg(test)]
42#[cfg_attr(coverage_nightly, coverage(off))]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn invalid_ref_constructor_sets_reason() {
48        let e = CredStoreError::invalid_ref("must not be empty");
49        assert_eq!(e.to_string(), "invalid secret reference: must not be empty");
50    }
51
52    #[test]
53    fn service_unavailable_constructor_sets_message() {
54        let e = CredStoreError::service_unavailable("backend down");
55        assert!(matches!(e, CredStoreError::ServiceUnavailable(ref m) if m == "backend down"));
56        assert_eq!(e.to_string(), "service unavailable: backend down");
57    }
58
59    #[test]
60    fn internal_constructor_sets_message() {
61        let e = CredStoreError::internal("unexpected state");
62        assert!(matches!(e, CredStoreError::Internal(ref m) if m == "unexpected state"));
63        assert_eq!(e.to_string(), "internal error: unexpected state");
64    }
65}