-
Notifications
You must be signed in to change notification settings - Fork 227
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
Elixir errors using Rust's ?
#506
Comments
As an added bonus, I've also played around with phantom types to add context to the error, see the branch error-context-with-phantom-types or the difference between that branch and main. I haven't merged it as it's a small (but I think cool) distraction. |
Initially I was trying to get rid of the two wrappers, but I have had the realisation that if we have the pattern fn _getenv(key: String) -> Result<String, MyError> {
// ...
}
#[nif]
pub fn getenv(key: String) -> Result<String, MyError> {
_getenv(key)
}
#[nif(name = "getenv!")]
pub fn getenv_bang(key: String) -> rustler::NifResult<String> {
Ok(_getenv(key)?)
} then we don't need to use the #[nif(error = error_mapper, bang = raise_mapper)]
fn getenv(key: String) -> Result<String, Box<dyn Error>> {
// ...
} expanding to
where fn error_mapper(error: Box<dyn Error>) -> rustler::Error {
rustler::Error::Term(Box::new(format!("Error: {}", error)))
}
fn raise_mapper(error: Box<dyn Error>) -> rustler::Error {
rustler::Error::RaiseTerm(Box::new(format!("Error: {}", error)))
} |
To be honest, I don't see how this last part adds value. You can just as well add this mapping to the Elixir module, and the error mapping can be done using a straightforward mapping in the nif implementation. Raising or returning strings as errors is also not a good style, IMO. |
True, it could do that, and it's easy in Elixir to just do a compile-time
where the erlang error is just But also the error mapper doesn't have to return strings of course :) I guess just having an
so duplicating this twice is annoying. The problem is, I cannot implement P.S.: I might be missing something obvious of course -- I'm new to Rust so feel free to tell me I'm just barking up the wrong tree |
Or if we could have an |
Hi,
First of all, Rustler is awesome, creating the NIF seems much simplified compared to C!
I was wondering about what would be the best practice for errors, in particular it would be nice to have errors using
?
. I have put together a repo for it at https://github.com/KoviRobi/rustler_raise_error and I was wondering if there is an easier way to do it?It's possible my whole use-case/assumptions are wrong, of course, and this isn't the right way to go about handling errors. What I wanted is an easy way to do errors that are semi user-understandable (not necessarily in normal English but a tech person should be able to figure it out). Here is what I got:
Using the
{:ok, _}
,{:error, _}
paradigmNote that this paradigm doesn't need the wrapper. I've just used one so that I can also reuse it for
getenv!
. But thinking about this, the whole thing wouldn't be so bad if we could reuse the function definition for the non-!
version (currently it's impossible because the definition is hidden behind a structure).Using the exceptions paradigm -- this also shows what advantage it might have over
{:ok, _} = ...
which is printing the arguments the function was called with.This also brought up the question, is there a way to have functions in Rustler that have
!
in them? I tried doingbut that didn't seem to work, it failed with
which seems like a bug? I've had a go at solving it in #505
The text was updated successfully, but these errors were encountered: