spl_math/
error.rs

1//! Error types
2
3use {
4    num_derive::FromPrimitive,
5    solana_program::{decode_error::DecodeError, program_error::ProgramError},
6    thiserror::Error,
7};
8
9/// Errors that may be returned by the Math program.
10#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
11pub enum MathError {
12    /// Calculation overflowed the destination number
13    #[error("Calculation overflowed the destination number")]
14    Overflow,
15    /// Calculation underflowed the destination number
16    #[error("Calculation underflowed the destination number")]
17    Underflow,
18}
19impl From<MathError> for ProgramError {
20    fn from(e: MathError) -> Self {
21        ProgramError::Custom(e as u32)
22    }
23}
24impl<T> DecodeError<T> for MathError {
25    fn type_of() -> &'static str {
26        "Math Error"
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use {super::*, solana_program::program_error::ProgramError};
33
34    #[test]
35    fn test_math_error_from() {
36        let program_error = ProgramError::from(MathError::Overflow);
37        assert_eq!(program_error, ProgramError::Custom(0));
38
39        let program_error = ProgramError::from(MathError::Underflow);
40        assert_eq!(program_error, ProgramError::Custom(1));
41    }
42}