openiap_proto/
errors.rs

1#![warn(missing_docs)]
2use std::fmt;
3use super::protos::Envelope;
4impl super::protos::ErrorResponse {
5    /// Creates a new `ErrorResponse` with the given `message` and `code`.
6    pub fn new(message: &str, code: i32) -> Self {
7        Self {
8            code,
9            message: message.to_string(),
10            ..Default::default()
11        }
12    }
13    /// Converts the `ErrorResponse` to an `Envelope`.
14    pub fn to_envelope(&self) -> Envelope {
15        let any_message = prost_types::Any {
16            type_url: "type.googleapis.com/openiap.ErrorResponse".to_string(),
17            value: {
18                let mut buf = Vec::new();
19                prost::Message::encode(self, &mut buf).unwrap_or(());
20                buf
21            },
22        };
23        Envelope {
24            command: "error".into(),
25            data: Some(any_message.clone()),
26            ..Default::default()
27        }
28    }
29}
30
31
32/// Error type for OpenIAP
33#[derive(Debug)]
34pub enum OpenIAPError {
35    /// Client error
36    ClientError(String),
37    /// Server error
38    ServerError(String),
39    /// Custom error
40    CustomError(String),
41}
42impl fmt::Display for OpenIAPError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            OpenIAPError::ClientError(e) => write!(f, "Client Error {}", e),
46            OpenIAPError::ServerError(e) => write!(f, "Server Error {}", e),
47            OpenIAPError::CustomError(e) => write!(f, "Custom Error {}", e),
48        }
49    }
50}
51impl std::error::Error for OpenIAPError {
52    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53        // match self {
54        //     // OpenIAPError::NestedError(e) => Some(&**e),
55        //     _ => None,
56        // }
57        None
58    }
59}