nanomsg.rs icon indicating copy to clipboard operation
nanomsg.rs copied to clipboard

Enhance error handling

Open blabaere opened this issue 10 years ago • 5 comments

The native nanomsg lib has quite a lot of constants for the errors, and they should be reported to the nanomsg.rs users. The nn_errno and nn_strerror should be used internally to produce usable results.

blabaere avatar Oct 31 '14 10:10 blabaere

Sadly, the nanomsg error code are platform dependants and I don't know how to port this kind of C code in rust:

#ifndef EBADF #define EBADF (NN_HAUSNUMERO + 13) #endif

blabaere avatar Nov 08 '14 11:11 blabaere

We can represent them with cfg and const types.

#[cfg(not(target_os = "windows"))]
use libc::{EPROTONOSUPPORT};

#[cfg(target_os = "windows")]
const EPROTONOSUPPORT: c_int = (NN_HAUSNUMERO + 2)

Some of the errors are already defined in the libc bindings that Rust ships with as they are standard POSIX errors.

The issue is finding out what platforms don't have them defined, I'm guessing windows is the major one that doesn't have them.

Also Rust hardcodes the values for each of these constants while Nanomsg has the NN_HAUSNUMERO random number as to not collide with other OSes error types.

thehydroimpulse avatar Nov 08 '14 20:11 thehydroimpulse

As usual, there is already some Rust goodness to support what we need. All of it in the trait FromPrimitive. I think I will update ErrorKind to take advantage of this.

blabaere avatar Nov 15 '14 22:11 blabaere

While implementing the part that gets the nanomsg error message attached to the error code, I tried to use c_str_to_static_slice to avoid the copy but failed to import the function so far. @thehydroimpulse, any idea about how to do this ?

blabaere avatar Nov 17 '14 07:11 blabaere

@blabaere mm, trying it on play.rust-lang.org seems to work for me. I'm not sure if that has just been added on a more-recent nightly, though.

thehydroimpulse avatar Nov 17 '14 20:11 thehydroimpulse