Add a conversion from native_tls::Error.

This commit is contained in:
Tom Prince
2017-05-31 19:27:45 -06:00
parent 8d784faa98
commit a36ed4a87a
2 changed files with 11 additions and 20 deletions

View File

@@ -74,10 +74,7 @@ impl Certificate {
/// ///
/// If the provided buffer is not valid DER, an error will be returned. /// If the provided buffer is not valid DER, an error will be returned.
pub fn from_der(der: &[u8]) -> ::Result<Certificate> { pub fn from_der(der: &[u8]) -> ::Result<Certificate> {
let inner = try_!( let inner = try_!(native_tls::Certificate::from_der(der));
native_tls::Certificate::from_der(der)
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
);
Ok(Certificate(inner)) Ok(Certificate(inner))
} }
} }
@@ -124,10 +121,7 @@ struct Config {
impl ClientBuilder { impl ClientBuilder {
/// Constructs a new `ClientBuilder` /// Constructs a new `ClientBuilder`
pub fn new() -> ::Result<ClientBuilder> { pub fn new() -> ::Result<ClientBuilder> {
let tls_connector_builder = try_!( let tls_connector_builder = try_!(native_tls::TlsConnector::builder());
native_tls::TlsConnector::builder()
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
);
Ok(ClientBuilder { Ok(ClientBuilder {
config: Some(Config { config: Some(Config {
gzip: true, gzip: true,
@@ -149,12 +143,7 @@ impl ClientBuilder {
pub fn build(&mut self) -> ::Result<Client> { pub fn build(&mut self) -> ::Result<Client> {
let config = self.take_config(); let config = self.take_config();
let tls_connector = try_!( let tls_connector = try_!(config.tls.build());
config
.tls
.build()
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
);
let mut tls_client = NativeTlsClient::from(tls_connector); let mut tls_client = NativeTlsClient::from(tls_connector);
if !config.hostname_verification { if !config.hostname_verification {
tls_client.danger_disable_hostname_verification(true); tls_client.danger_disable_hostname_verification(true);
@@ -186,12 +175,7 @@ impl ClientBuilder {
/// This can be used to connect to a server that has a self-signed /// This can be used to connect to a server that has a self-signed
/// certificate for example. /// certificate for example.
pub fn add_root_certificate(&mut self, cert: Certificate) -> ::Result<&mut ClientBuilder> { pub fn add_root_certificate(&mut self, cert: Certificate) -> ::Result<&mut ClientBuilder> {
try_!( try_!(self.config_mut().tls.add_root_certificate(cert.0));
self.config_mut()
.tls
.add_root_certificate(cert.0)
.map_err(|e| ::hyper::Error::Ssl(Box::new(e)))
);
Ok(self) Ok(self)
} }

View File

@@ -143,6 +143,13 @@ impl From<::serde_json::Error> for Kind {
} }
} }
impl From<::hyper_native_tls::native_tls::Error> for Kind {
fn from(other: ::hyper_native_tls::native_tls::Error) -> Kind {
::hyper::Error::Ssl(Box::new(other)).into()
}
}
pub struct InternalFrom<T>(pub T, pub Option<Url>); pub struct InternalFrom<T>(pub T, pub Option<Url>);
impl From<InternalFrom<Error>> for Error { impl From<InternalFrom<Error>> for Error {