Skip to main content

jito_jsm_core/
error.rs

1use solana_program::{decode_error::DecodeError, program_error::ProgramError};
2use thiserror::Error;
3
4#[derive(Debug, Error, PartialEq, Eq)]
5pub enum CoreError {
6    #[error("Bad epoch length")]
7    BadEpochLength,
8}
9
10impl<T> DecodeError<T> for CoreError {
11    fn type_of() -> &'static str {
12        "jito::core"
13    }
14}
15
16impl From<CoreError> for ProgramError {
17    fn from(e: CoreError) -> Self {
18        Self::Custom(e as u32)
19    }
20}
21
22impl From<CoreError> for u64 {
23    fn from(e: CoreError) -> Self {
24        e as Self
25    }
26}
27
28impl From<CoreError> for u32 {
29    fn from(e: CoreError) -> Self {
30        e as Self
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_error_display() {
40        let error = CoreError::BadEpochLength;
41        assert_eq!(error.to_string(), "Bad epoch length");
42    }
43
44    #[test]
45    fn test_decode_error_type() {
46        assert_eq!(
47            <CoreError as DecodeError<CoreError>>::type_of(),
48            "jito::core"
49        );
50    }
51
52    #[test]
53    fn test_conversion_to_program_error() {
54        let error = CoreError::BadEpochLength;
55        let program_error: ProgramError = error.into();
56
57        // Verify the conversion creates a Custom program error with the correct error code
58        match program_error {
59            ProgramError::Custom(code) => {
60                assert_eq!(code, CoreError::BadEpochLength as u32);
61            }
62            _ => panic!("Expected ProgramError::Custom"),
63        }
64    }
65
66    #[test]
67    fn test_conversion_to_u64() {
68        let error = CoreError::BadEpochLength;
69        let code: u64 = error.into();
70        assert_eq!(code, CoreError::BadEpochLength as u64);
71    }
72
73    #[test]
74    fn test_conversion_to_u32() {
75        let error = CoreError::BadEpochLength;
76        let code: u32 = error.into();
77        assert_eq!(code, CoreError::BadEpochLength as u32);
78    }
79
80    #[test]
81    fn test_error_equality() {
82        let error1 = CoreError::BadEpochLength;
83        let error2 = CoreError::BadEpochLength;
84        assert_eq!(error1, error2);
85    }
86
87    #[test]
88    fn test_debug_implementation() {
89        let error = CoreError::BadEpochLength;
90        assert_eq!(format!("{:?}", error), "BadEpochLength");
91    }
92}