Skip to content

Commit

Permalink
Change connection errors to use APNSError type
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Apr 18, 2017
1 parent 5b5058e commit f20c0a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 47 deletions.
5 changes: 2 additions & 3 deletions src/client/certificate.rs
Expand Up @@ -7,9 +7,8 @@ use time::precise_time_ns;
use std::str;
use std::result::Result;
use std::io::Read;
use client::response::ProviderResponse;
use client::response::{ProviderResponse, APNSError};
use client::headers::default_headers;
use client::error::ProviderError;
use notification::Notification;
use client::{DEVELOPMENT, PRODUCTION};

Expand Down Expand Up @@ -52,7 +51,7 @@ impl CertificateClient {
/// Create a new connection to APNs with custom certificate and key. Can be
/// used to send notification to only one app.
pub fn new<'a, R: Read>(sandbox: bool, certificate: &mut R, private_key: &mut R)
-> Result<CertificateClient, ProviderError> {
-> Result<CertificateClient, APNSError> {
let host = if sandbox { DEVELOPMENT } else { PRODUCTION };
let mut ctx = SslContext::new(SslMethod::Sslv23).unwrap();

Expand Down
39 changes: 0 additions & 39 deletions src/client/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/client/mod.rs
Expand Up @@ -6,7 +6,6 @@
mod certificate;
mod response;
mod headers;
mod error;
mod token;

pub use self::token::TokenClient;
Expand Down
25 changes: 24 additions & 1 deletion src/client/response.rs
Expand Up @@ -2,11 +2,14 @@ use rustc_serialize::json::{Json, Object};
use std::str;
use time::{Tm, Timespec, at};
use solicit::http::{Header, Response as HttpResponse};
use solicit::http::client::tls::TlsConnectError;
use solicit::client::ClientConnectError;
use std::time::{Duration, Instant};
use std::error::Error;
use std::fmt;
use std::thread;
use std::sync::mpsc::Receiver;
use openssl::ssl::error::SslError;

use self::APNSError::*;

Expand Down Expand Up @@ -96,6 +99,12 @@ pub enum APNSError {
/// The apns-topic header is mandatory when the client is connected using a
/// certificate that supports multiple topics.
MissingTopic,

/// OpenSSL configuration failed. Check the certificates and system support for OpenSSL
SslError(String),

/// Couldn't connect to APNS2.
ClientConnectError(String)
}

impl fmt::Debug for APNSError {
Expand All @@ -111,6 +120,18 @@ impl fmt::Display for APNSError {
}
}

impl From<SslError> for APNSError {
fn from(e: SslError) -> APNSError {
APNSError::SslError(format!("Error generating an SSL context: {}", e.description()))
}
}

impl From<ClientConnectError<TlsConnectError>> for APNSError {
fn from(e: ClientConnectError<TlsConnectError>) -> APNSError {
APNSError::ClientConnectError(format!("Error connecting to the APNs servers: {}", e.description()))
}
}

impl Error for APNSError {
fn description(&self) -> &str {
match *self {
Expand Down Expand Up @@ -152,7 +173,9 @@ impl Error for APNSError {
"The apns-topic header of the request was not specified and was required. The \
apns-topic header is mandatory when the client is connected using a certificate \
that supports multiple topics"
}
},
SslError(ref e) => e,
ClientConnectError(ref e) => e,
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/client/token.rs
Expand Up @@ -3,9 +3,8 @@ use solicit::client::{Client};
use time::precise_time_ns;
use std::result::Result;

use client::response::ProviderResponse;
use client::response::{ProviderResponse, APNSError};
use client::headers::{default_headers, create_header};
use client::error::ProviderError;
use notification::Notification;
use client::{DEVELOPMENT, PRODUCTION};

Expand Down Expand Up @@ -53,7 +52,7 @@ pub struct TokenClient {
impl TokenClient {
/// Create a new connection to APNs. `certificates` should point to system ca certificate
/// file. In Ubuntu it's usually `/etc/ssl/certs/ca-certificates.crt`.
pub fn new<'a>(sandbox: bool, certificates: &str) -> Result<TokenClient, ProviderError> {
pub fn new<'a>(sandbox: bool, certificates: &str) -> Result<TokenClient, APNSError> {
let host = if sandbox { DEVELOPMENT } else { PRODUCTION };
let connector = TlsConnector::new(host, &certificates);
let client = Client::with_connector(connector)?;
Expand Down

0 comments on commit f20c0a0

Please sign in to comment.