Skip to content

Commit 6b07e4f

Browse files
committed
Improve Error handling
1 parent 0c72017 commit 6b07e4f

20 files changed

+200
-383
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = ["tests/wasm_bindgen"]
1919

2020
[dependencies]
2121
log = { version = "0.4", optional = true }
22+
cfg-if = "0.1"
2223

2324
[target.'cfg(any(unix, target_os = "redox", target_os = "wasi"))'.dependencies]
2425
libc = "0.2.54"

src/cloudabi.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@
77
// except according to those terms.
88

99
//! Implementation for CloudABI
10-
use crate::Error;
11-
use core::num::NonZeroU32;
10+
extern crate std;
11+
12+
use std::io;
13+
14+
pub type Error = io::Error;
1215

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

1720
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1821
let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) };
19-
if let Some(code) = NonZeroU32::new(errno as u32) {
20-
error!("cloudabi_sys_random_get failed with code {}", code);
21-
Err(Error::from(code))
22+
if errno != 0 {
23+
error!("cloudabi_sys_random_get failed with code {}", errno);
24+
Err(Error::from_raw_os_error(errno as i32))
2225
} else {
2326
Ok(()) // Zero means success for CloudABI
2427
}
2528
}
26-
27-
#[inline(always)]
28-
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
29-
None
30-
}

src/dummy.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,11 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

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

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

19-
#[inline(always)]
20-
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
21-
None
14+
pub fn getrandom_inner(_: &mut [u8]) -> Result<(), Error> {
15+
Err(StaticError("getrandom: this target is not supported"))
2216
}

src/error.rs

-93
This file was deleted.

src/error_impls.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,12 @@
55
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
8-
extern crate std;
8+
use crate::Error;
99

10-
use crate::error::Error;
11-
use core::convert::From;
12-
use core::num::NonZeroU32;
13-
use std::{error, io};
14-
15-
impl From<io::Error> for Error {
16-
fn from(err: io::Error) -> Self {
17-
err.raw_os_error()
18-
.and_then(|code| NonZeroU32::new(code as u32))
19-
.map(|code| Error(code))
20-
// in practice this should never happen
21-
.unwrap_or(Error::UNKNOWN)
22-
}
23-
}
24-
25-
impl From<Error> for io::Error {
10+
impl From<Error> for std::io::Error {
2611
fn from(err: Error) -> Self {
27-
match err.msg() {
28-
Some(msg) => io::Error::new(io::ErrorKind::Other, msg),
29-
None => io::Error::from_raw_os_error(err.0.get() as i32),
30-
}
12+
err.0.into()
3113
}
3214
}
3315

34-
impl error::Error for Error {}
16+
impl std::error::Error for Error {}

src/freebsd.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
//! Implementation for FreeBSD
1010
extern crate std;
1111

12-
use crate::Error;
13-
use core::num::NonZeroU32;
1412
use core::ptr;
1513
use std::io;
1614

15+
pub type Error = io::Error;
16+
1717
fn kern_arnd(buf: &mut [u8]) -> Result<usize, Error> {
1818
static MIB: [libc::c_int; 2] = [libc::CTL_KERN, libc::KERN_ARND];
1919
let mut len = buf.len();
@@ -29,7 +29,7 @@ fn kern_arnd(buf: &mut [u8]) -> Result<usize, Error> {
2929
};
3030
if ret == -1 {
3131
error!("freebsd: kern.arandom syscall failed");
32-
return Err(io::Error::last_os_error().into());
32+
return Err(Error::last_os_error());
3333
}
3434
Ok(len)
3535
}
@@ -41,8 +41,3 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
4141
}
4242
Ok(())
4343
}
44-
45-
#[inline(always)]
46-
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
47-
None
48-
}

src/fuchsia.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,23 @@
77
// except according to those terms.
88

99
//! Implementation for Fuchsia Zircon
10-
use crate::Error;
11-
use core::num::NonZeroU32;
10+
use core::fmt;
11+
12+
#[derive(Debug)]
13+
pub enum Error {} // Zircon's RNG cannot fail
14+
15+
impl fmt::Display for Error {
16+
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
17+
match *self {}
18+
}
19+
}
20+
21+
#[cfg(feature = "std")]
22+
impl From<Error> for std::io::Error {
23+
fn from(err: Error) -> Self {
24+
match err {}
25+
}
26+
}
1227

1328
#[link(name = "zircon")]
1429
extern "C" {
@@ -19,8 +34,3 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1934
unsafe { zx_cprng_draw(dest.as_mut_ptr(), dest.len()) }
2035
Ok(())
2136
}
22-
23-
#[inline(always)]
24-
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
25-
None
26-
}

src/ios.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
// except according to those terms.
88

99
//! Implementation for iOS
10-
extern crate std;
10+
use crate::util::StaticError;
1111

12-
use crate::Error;
13-
use core::num::NonZeroU32;
14-
use std::io;
12+
pub type Error = StaticError;
1513

1614
// TODO: Make extern once extern_types feature is stabilized. See:
1715
// https://github.com/rust-lang/rust/issues/43467
@@ -28,14 +26,8 @@ extern "C" {
2826
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
2927
let ret = unsafe { SecRandomCopyBytes(kSecRandomDefault, dest.len(), dest.as_mut_ptr()) };
3028
if ret == -1 {
31-
error!("SecRandomCopyBytes call failed");
32-
Err(io::Error::last_os_error().into())
29+
Err(StaticError("SecRandomCopyBytes: call failed"))
3330
} else {
3431
Ok(())
3532
}
3633
}
37-
38-
#[inline(always)]
39-
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> {
40-
None
41-
}

0 commit comments

Comments
 (0)