Skip to main content

apns_h2/
error.rs

1/// Error and result module
2use crate::{response::Response, signer::SignerError};
3use std::io;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum Error {
8    /// User request or Apple response JSON data was faulty.
9    #[error("Error serializing to JSON: {0}")]
10    SerializeError(#[from] serde_json::Error),
11
12    /// A problem connecting to APNs servers.
13    #[error("Error connecting to APNs: {0}")]
14    ConnectionError(#[from] hyper::Error),
15
16    #[error("Http client error: {0}")]
17    ClientError(#[from] hyper_util::client::legacy::Error),
18
19    /// Couldn't generate an APNs token with the given key.
20    #[error("Error creating a signature: {0}")]
21    SignerError(#[from] SignerError),
22
23    /// APNs couldn't accept the notification. Contains
24    /// [Response](response/struct.Response.html) with additional
25    /// information.
26    #[error(
27        "Notification was not accepted by APNs (reason: {})",
28        .0.error
29            .as_ref()
30            .map(|e| e.reason.to_string())
31            .unwrap_or_else(|| "Unknown".to_string())
32    )]
33    ResponseError(Response),
34
35    /// Invalid option values given in
36    /// [NotificationOptions](request/notification/struct.NotificationOptions.html)
37    #[error("Invalid options for APNs payload: {0}")]
38    InvalidOptions(String),
39
40    /// Error reading the certificate or private key.
41    #[error("Error in reading a certificate file: {0}")]
42    ReadError(#[from] io::Error),
43
44    #[error("Error building TLS config: {0}")]
45    Tls(#[from] rustls::Error),
46
47    /// Error while creating the HTTP request
48    #[error("Failed to construct HTTP request: {0}")]
49    BuildRequestError(#[source] http::Error),
50
51    /// No repsonse from APNs after the given amount of time
52    #[error("The request timed out after {0} s")]
53    RequestTimeout(u64),
54
55    /// Unexpected private key (only EC keys are supported).
56    #[cfg(feature = "aws-lc-rs")]
57    #[error("Unexpected private key: {0}")]
58    UnexpectedKey(#[from] aws_lc_rs::error::KeyRejected),
59
60    #[error("Invalid certificate")]
61    InvalidCertificate,
62}
63
64#[cfg(all(not(feature = "aws-lc-rs"), feature = "openssl"))]
65impl From<openssl::error::ErrorStack> for Error {
66    fn from(e: openssl::error::ErrorStack) -> Self {
67        Self::SignerError(SignerError::OpenSSL(e))
68    }
69}