Skip to content

Commit

Permalink
Improve Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
josephlr committed Jul 5, 2019
1 parent 0c72017 commit b9cca59
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 366 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = ["tests/wasm_bindgen"]

[dependencies]
log = { version = "0.4", optional = true }
cfg-if = "0.1"

[target.'cfg(any(unix, target_os = "redox", target_os = "wasi"))'.dependencies]
libc = "0.2.54"
Expand Down
18 changes: 8 additions & 10 deletions src/cloudabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
// except according to those terms.

//! Implementation for CloudABI
use crate::Error;
use core::num::NonZeroU32;
extern crate std;

use std::io;

pub type Error = io::Error;

extern "C" {
fn cloudabi_sys_random_get(buf: *mut u8, buf_len: usize) -> u16;
}

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) };
if let Some(code) = NonZeroU32::new(errno as u32) {
error!("cloudabi_sys_random_get failed with code {}", code);
Err(Error::from(code))
if errno != 0 {
error!("cloudabi_sys_random_get failed with code {}", errno);
Err(Error::from_raw_os_error(errno as i32))
} else {
Ok(()) // Zero means success for CloudABI
}
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
None
}
16 changes: 5 additions & 11 deletions src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A dummy implementation for unsupported targets which always returns
//! `Err(Error::UNAVAILABLE)`
use crate::Error;
use core::num::NonZeroU32;
//! A dummy implementation for unsupported targets which always fails.
use crate::util::StaticError;

pub fn getrandom_inner(_: &mut [u8]) -> Result<(), Error> {
error!("no support for this platform");
Err(Error::UNAVAILABLE)
}
pub type Error = StaticError;

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
None
pub fn getrandom_inner(_: &mut [u8]) -> Result<(), Error> {
Err(StaticError("getrandom: this target is not supported"))
}
93 changes: 0 additions & 93 deletions src/error.rs

This file was deleted.

26 changes: 4 additions & 22 deletions src/error_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate std;
use crate::Error;

use crate::error::Error;
use core::convert::From;
use core::num::NonZeroU32;
use std::{error, io};

impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
err.raw_os_error()
.and_then(|code| NonZeroU32::new(code as u32))
.map(|code| Error(code))
// in practice this should never happen
.unwrap_or(Error::UNKNOWN)
}
}

impl From<Error> for io::Error {
impl From<Error> for std::io::Error {
fn from(err: Error) -> Self {
match err.msg() {
Some(msg) => io::Error::new(io::ErrorKind::Other, msg),
None => io::Error::from_raw_os_error(err.0.get() as i32),
}
err.0.into()
}
}

impl error::Error for Error {}
impl std::error::Error for Error {}
11 changes: 3 additions & 8 deletions src/freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
//! Implementation for FreeBSD
extern crate std;

use crate::Error;
use core::num::NonZeroU32;
use core::ptr;
use std::io;

pub type Error = io::Error;

fn kern_arnd(buf: &mut [u8]) -> Result<usize, Error> {
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
let mut len = buf.len();
Expand All @@ -29,7 +29,7 @@ fn kern_arnd(buf: &mut [u8]) -> Result<usize, Error> {
};
if ret == -1 {
error!("freebsd: kern.arandom syscall failed");
return Err(io::Error::last_os_error().into());
return Err(Error::last_os_error());
}
Ok(len)
}
Expand All @@ -41,8 +41,3 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
}
Ok(())
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
None
}
17 changes: 10 additions & 7 deletions src/fuchsia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
// except according to those terms.

//! Implementation for Fuchsia Zircon
use crate::Error;
use core::num::NonZeroU32;
use core::convert::Infallible;

pub type Error = Infallible; // Zircon's RNG cannot fail

#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
fn from(err: Error) -> Self {
match err {}
}
}

#[link(name = "zircon")]
extern "C" {
Expand All @@ -19,8 +27,3 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
unsafe { zx_cprng_draw(dest.as_mut_ptr(), dest.len()) }
Ok(())
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
None
}
14 changes: 3 additions & 11 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
// except according to those terms.

//! Implementation for iOS
extern crate std;
use crate::util::StaticError;

use crate::Error;
use core::num::NonZeroU32;
use std::io;
pub type Error = StaticError;

// TODO: Make extern once extern_types feature is stabilized. See:
// https://github.com/rust-lang/rust/issues/43467
Expand All @@ -28,14 +26,8 @@ extern "C" {
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
if ret == -1 {
error!("SecRandomCopyBytes call failed");
Err(io::Error::last_os_error().into())
Err(StaticError("SecRandomCopyBytes: call failed"))
} else {
Ok(())
}
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
None
}
Loading

0 comments on commit b9cca59

Please sign in to comment.