expo_push_notification_client/
error.rs

1use serde::Deserialize;
2
3#[derive(Debug, Deserialize, PartialEq, thiserror::Error, Eq, Hash)]
4pub enum CustomError {
5    #[error("Gzip error: {0}")]
6    GzipErr(String),
7    #[error("Invalid argument: {0}")]
8    InvalidArgument(String),
9    #[error("Deserialize error: {0}")]
10    DeserializeErr(String),
11    #[error("Serialize error: {0}")]
12    SerializeErr(String),
13    #[error("Server error: {0}")]
14    ServerErr(String),
15}
16
17impl std::convert::From<std::convert::Infallible> for CustomError {
18    fn from(_: std::convert::Infallible) -> Self {
19        unreachable!()
20    }
21}
22
23#[derive(Debug, PartialEq, thiserror::Error)]
24pub enum ValidationError {
25    #[error("Invalid data")]
26    InvalidData,
27    #[error("Invalid token")]
28    InvalidToken,
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_impl_error() {
37        fn assert_error<T: std::error::Error>() {}
38        assert_error::<CustomError>();
39    }
40
41    #[test]
42    fn test_impl_display() {
43        assert_eq!(
44            CustomError::InvalidArgument("x".to_string()).to_string(),
45            "Invalid argument: x"
46        );
47        assert_eq!(
48            CustomError::DeserializeErr("x".to_string()).to_string(),
49            "Deserialize error: x"
50        );
51        assert_eq!(
52            CustomError::ServerErr("x".to_string()).to_string(),
53            "Server error: x"
54        );
55
56        assert_eq!(ValidationError::InvalidToken.to_string(), "Invalid token");
57    }
58}