From 7ea442be94ac1450d5d9d6c5670c10b0e7a5b05a Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 23 Oct 2013 21:28:08 -0700 Subject: [PATCH] Ssl errors may return a stack --- error.rs | 25 +++++++++++++++++-------- lib.rs | 12 ++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/error.rs b/error.rs index 5e5479f8..d009b38c 100644 --- a/error.rs +++ b/error.rs @@ -6,6 +6,11 @@ use super::ffi; pub enum SslError { StreamEof, SslSessionClosed, + OpenSslErrors(~[OpensslError]) +} + +#[deriving(ToStr)] +pub enum OpensslError { UnknownError { library: u8, function: u16, @@ -26,14 +31,18 @@ fn get_reason(err: c_ulong) -> u16 { } impl SslError { - pub fn get() -> Option { - match unsafe { ffi::ERR_get_error() } { - 0 => None, - err => Some(UnknownError { - library: get_lib(err), - function: get_func(err), - reason: get_reason(err) - }) + pub fn get() -> SslError { + let mut errs = ~[]; + loop { + match unsafe { ffi::ERR_get_error() } { + 0 => break, + err => errs.push(UnknownError { + library: get_lib(err), + function: get_func(err), + reason: get_reason(err) + }) + } } + OpenSslErrors(errs) } } diff --git a/lib.rs b/lib.rs index a15b7964..2742a48f 100644 --- a/lib.rs +++ b/lib.rs @@ -98,7 +98,7 @@ impl SslContext { let ctx = unsafe { ffi::SSL_CTX_new(method.to_raw()) }; if ctx == ptr::null() { - return Err(SslError::get().unwrap()); + return Err(SslError::get()); } Ok(SslContext { ctx: ctx }) @@ -130,7 +130,7 @@ impl SslContext { }; if ret == 0 { - Some(SslError::get().unwrap()) + Some(SslError::get()) } else { None } @@ -151,19 +151,19 @@ impl Ssl { fn try_new(ctx: &SslContext) -> Result { let ssl = unsafe { ffi::SSL_new(ctx.ctx) }; if ssl == ptr::null() { - return Err(SslError::get().unwrap()); + return Err(SslError::get()); } let ssl = Ssl { ssl: ssl }; let rbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) }; if rbio == ptr::null() { - return Err(SslError::get().unwrap()); + return Err(SslError::get()); } let wbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) }; if wbio == ptr::null() { unsafe { ffi::BIO_free_all(rbio) } - return Err(SslError::get().unwrap()); + return Err(SslError::get()); } unsafe { ffi::SSL_set_bio(ssl.ssl, rbio, wbio) } @@ -307,7 +307,7 @@ impl SslStream { } ErrorWantWrite => self.flush(), ErrorZeroReturn => return Err(SslSessionClosed), - ErrorSsl => return Err(SslError::get().unwrap()), + ErrorSsl => return Err(SslError::get()), _ => unreachable!() } }