openzeppelin_relayer/models/error/
signer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use serde::Serialize;
use thiserror::Error;

use crate::services::VaultError;

use super::TransactionError;

#[derive(Error, Debug, Serialize)]
#[allow(clippy::enum_variant_names)]
pub enum SignerError {
    #[error("Failed to sign transaction: {0}")]
    SigningError(String),

    #[error("Invalid key format: {0}")]
    KeyError(String),

    #[error("Provider error: {0}")]
    ProviderError(String),

    #[error("Unsupported signer type: {0}")]
    UnsupportedTypeError(String),

    #[error("Invalid transaction: {0}")]
    InvalidTransaction(#[from] TransactionError),

    #[error("Vault error: {0}")]
    VaultError(#[from] VaultError),

    #[error("Not implemented: {0}")]
    NotImplemented(String),

    #[error("Invalid configuration: {0}")]
    Configuration(String),
}

#[derive(Error, Debug, Serialize)]
pub enum SignerFactoryError {
    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),
    #[error("Signer creation failed: {0}")]
    CreationFailed(String),
    #[error("Unsupported signer type: {0}")]
    UnsupportedType(String),
    #[error("Signer error: {0}")]
    SignerError(#[from] SignerError),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_signer_error_display() {
        let test_cases = vec![
            (
                SignerError::SigningError("failed to sign".to_string()),
                "Failed to sign transaction: failed to sign",
            ),
            (
                SignerError::KeyError("invalid key".to_string()),
                "Invalid key format: invalid key",
            ),
            (
                SignerError::ProviderError("connection failed".to_string()),
                "Provider error: connection failed",
            ),
            (
                SignerError::UnsupportedTypeError("unknown type".to_string()),
                "Unsupported signer type: unknown type",
            ),
            (
                SignerError::NotImplemented("feature not ready".to_string()),
                "Not implemented: feature not ready",
            ),
            (
                SignerError::Configuration("missing parameter".to_string()),
                "Invalid configuration: missing parameter",
            ),
        ];

        for (error, expected_message) in test_cases {
            assert_eq!(error.to_string(), expected_message);
        }
    }

    #[test]
    fn test_signer_error_from_transaction_error() {
        let tx_error = TransactionError::ValidationError("bad format".to_string());
        let signer_error = SignerError::from(tx_error);

        match signer_error {
            SignerError::InvalidTransaction(e) => {
                assert_eq!(e.to_string(), "Transaction validation error: bad format");
            }
            _ => panic!("Expected SignerError::InvalidTransaction"),
        }
    }

    #[test]
    fn test_signer_error_from_vault_error() {
        let vault_error = VaultError::AuthenticationFailed("no permission".to_string());
        let signer_error = SignerError::from(vault_error);

        match signer_error {
            SignerError::VaultError(e) => {
                assert_eq!(e.to_string(), "Authentication failed: no permission");
            }
            _ => panic!("Expected SignerError::VaultError"),
        }
    }

    #[test]
    fn test_signer_factory_error_display() {
        let test_cases = vec![
            (
                SignerFactoryError::InvalidConfig("missing key".to_string()),
                "Invalid configuration: missing key",
            ),
            (
                SignerFactoryError::CreationFailed("initialization error".to_string()),
                "Signer creation failed: initialization error",
            ),
            (
                SignerFactoryError::UnsupportedType("unknown signer".to_string()),
                "Unsupported signer type: unknown signer",
            ),
        ];

        for (error, expected_message) in test_cases {
            assert_eq!(error.to_string(), expected_message);
        }
    }

    #[test]
    fn test_signer_factory_error_from_signer_error() {
        let signer_error = SignerError::KeyError("invalid key format".to_string());
        let factory_error = SignerFactoryError::from(signer_error);

        match factory_error {
            SignerFactoryError::SignerError(e) => {
                assert_eq!(e.to_string(), "Invalid key format: invalid key format");
            }
            _ => panic!("Expected SignerFactoryError::SignerError"),
        }
    }
}