Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log error details via Display instead of Debug #249

Merged
merged 3 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ErrorKind {
/// A description of this error kind
pub fn description(self) -> &'static str {
match self {
ErrorKind::Unavailable => "permanent failure or unavailable",
ErrorKind::Unavailable => "permanent failure",
ErrorKind::Transient => "transient failure",
ErrorKind::NotReady => "not ready yet",
ErrorKind::Other => "uncategorised",
Expand Down Expand Up @@ -123,7 +123,15 @@ impl Error {

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RNG error [{}]: {}", self.kind.description(), self.msg())
#[cfg(feature="std")] {
if let Some(ref cause) = self.cause {
return write!(f, "{} ({}); cause: {}",
self.msg(),
self.kind.description(),
cause);
}
}
write!(f, "{} ({})", self.msg(), self.kind.description())
}
}

Expand Down
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ thread_local!(
static THREAD_RNG_KEY: Rc<RefCell<ReseedingRng<StdRng, EntropyRng>>> = {
const THREAD_RNG_RESEED_THRESHOLD: u64 = 32_768;
let mut entropy_source = EntropyRng::new();
let r = StdRng::from_rng(&mut entropy_source)
.expect("could not initialize thread_rng");
let r = StdRng::from_rng(&mut entropy_source).unwrap_or_else(|err|
panic!("could not initialize thread_rng: {}", err));
let rng = ReseedingRng::new(r,
THREAD_RNG_RESEED_THRESHOLD,
entropy_source);
Expand Down Expand Up @@ -1041,7 +1041,8 @@ impl Rng for EntropyRng {
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.try_fill_bytes(dest).unwrap();
self.try_fill_bytes(dest).unwrap_or_else(|err|
panic!("all entropy sources failed; first error: {}", err))
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
Expand All @@ -1065,13 +1066,15 @@ impl Rng for EntropyRng {
let os_rng_result = try_os_new(dest);
match os_rng_result {
Ok(os_rng) => {
debug!("EntropyRng: using OsRng");
switch_rng = Some(EntropySource::Os(os_rng));
}
Err(os_rng_error) => {
warn!("EntropyRng: OsRng failed [falling back to JitterRng]: {}",
os_rng_error);
match try_jitter_new(dest) {
Ok(jitter_rng) => {
debug!("EntropyRng: using JitterRng");
switch_rng = Some(EntropySource::Jitter(jitter_rng));
}
Err(_jitter_error) => {
Expand All @@ -1090,6 +1093,7 @@ impl Rng for EntropyRng {
os_rng_error);
match try_jitter_new(dest) {
Ok(jitter_rng) => {
debug!("EntropyRng: using JitterRng");
switch_rng = Some(EntropySource::Jitter(jitter_rng));
}
Err(_jitter_error) => {
Expand All @@ -1102,7 +1106,7 @@ impl Rng for EntropyRng {
}
EntropySource::Jitter(ref mut rng) => {
if let Ok(os_rng) = try_os_new(dest) {
info!("EntropyRng: OsRng available [switching back from JitterRng]");
debug!("EntropyRng: using OsRng");
switch_rng = Some(EntropySource::Os(os_rng));
} else {
return rng.try_fill_bytes(dest); // use JitterRng
Expand Down Expand Up @@ -1194,7 +1198,7 @@ pub fn sample<T, I, R>(rng: &mut R, iterable: I, amount: usize) -> Vec<T>
mod test {
use impls;
#[cfg(feature="std")]
use super::{random, thread_rng};
use super::{random, thread_rng, EntropyRng};
use super::{Rng, SeedableRng, StdRng};
#[cfg(feature="alloc")]
use alloc::boxed::Box;
Expand Down Expand Up @@ -1240,6 +1244,13 @@ mod test {
impls::fill_bytes_via_u64(self, dest)
}
}

#[test]
#[cfg(feature="std")]
fn test_entropy() {
let mut rng = EntropyRng::new();
rng.next_u32();
}

#[test]
fn test_fill_bytes_default() {
Expand Down
12 changes: 6 additions & 6 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ impl Rng for OsRng {
loop {
if let Err(e) = self.try_fill_bytes(dest) {
if err_count >= RETRY_LIMIT {
error!("OsRng failed too many times; last error: {:?}", e);
panic!("OsRng failed too many times; last error: {:?}", e);
error!("OsRng failed too many times; last error: {}", e);
panic!("OsRng failed too many times; last error: {}", e);
}

match e.kind() {
ErrorKind::Transient => {
if !error_logged {
warn!("OsRng failed; retrying up to {} times. Error: {:?}",
warn!("OsRng failed; retrying up to {} times. Error: {}",
TRANSIENT_RETRIES, e);
error_logged = true;
}
Expand All @@ -95,7 +95,7 @@ impl Rng for OsRng {
}
ErrorKind::NotReady => {
if !error_logged {
warn!("OsRng failed; waiting up to {}s and retrying. Error: {:?}",
warn!("OsRng failed; waiting up to {}s and retrying. Error: {}",
MAX_RETRY_PERIOD, e);
error_logged = true;
}
Expand All @@ -104,8 +104,8 @@ impl Rng for OsRng {
continue;
}
_ => {
error!("OsRng failed: {:?}", e);
panic!("OsRng fatal error: {:?}", e);
error!("OsRng failed: {}", e);
panic!("OsRng fatal error: {}", e);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl<R: Rng+SeedableRng, Rsdr: Rng> ReseedingRng<R, Rsdr> {
pub fn reseed_if_necessary(&mut self) {
if self.bytes_generated >= self.generation_threshold {
trace!("Reseeding RNG after {} bytes", self.bytes_generated);
R::from_rng(&mut self.reseeder).map(|result| self.rng = result).unwrap();
R::from_rng(&mut self.reseeder).map(|result| self.rng = result)
.unwrap_or_else(|err| panic!("reseeding failed: {}", err));
self.bytes_generated = 0;
}
}
Expand Down