# Throw badarg with more information from a NIF?

**URL:** <https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323>\
**Category:** Questions / Help\
**Tags:** erlang, nif\
**Created:** [February 22, 2024, 1:04pm UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323 "2024-02-22T13:04:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NAR](https://erlangforums.com/user_avatar/erlangforums.com/nar/32/1363_2.png) [@NAR](https://erlangforums.com/u/NAR)\
**Post date:** [February 22, 2024, 1:04pm UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323/1 "2024-02-22T13:04:57Z")

</div>

I’ve just stumbled on [Eep 0054 - Erlang/OTP](https://www.erlang.org/eeps/eep-0054) that’s about producing more useful `badarg` errors. Is there a way to produce such `badarg` errors from NIFs? ` enif_make_badarg` does not accept any extra parameters, nor does `enif_raise_exception`.

---

<div class="post-metadata">

**Author:** ![williamthome](https://erlangforums.com/user_avatar/erlangforums.com/williamthome/32/471_2.png) [@williamthome](https://erlangforums.com/u/williamthome)\
**Post date:** [February 22, 2024, 2:48pm UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323/2 "2024-02-22T14:48:24Z")

</div>

Apparently, currently, it’s not possible:  
[https://groups.google.com/g/erlang-programming/c/5P4ZqRzjXnc](https://groups.google.com/g/erlang-programming/c/5P4ZqRzjXnc)

> There are no NIF primitives yet to raise that type of exception, but we have been talking about adding them. The best you can do now is to throw an error and catch it in a NIF wrapper and then rethrow it again with the extra error\_info. Something like:

> my\_wrapper(Arg1) →   
> try my\_nif(Arg1)  
> catch error:badarg →   
> error(badarg, [Arg1], [{error\_info, #{}}])  
> end.

> format\_error(\_Reason, [{?MODULE, my\_wrapper, 1, _}|_]) →   
> #{ 1 =\> “invalid password” }.

---

<div class="post-metadata">

**Author:** ![raimo](https://erlangforums.com/user_avatar/erlangforums.com/raimo/32/867_2.png) [@raimo](https://erlangforums.com/u/raimo)\
**Post date:** [February 22, 2024, 4:07pm UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323/3 "2024-02-22T16:07:53Z")

</div>

`enif_raise_exception(ErlNifEnv* env, ERL_NIF_TERM reason)`'s second argument can be any term.

The socket NIF consistently uses `{invalid, What}`. This becomes an exception of class `error` with content `{invalid, What}`.

Might this be useful in your case?

---

<div class="post-metadata">

**Author:** ![NAR](https://erlangforums.com/user_avatar/erlangforums.com/nar/32/1363_2.png) [@NAR](https://erlangforums.com/u/NAR)\
**Post date:** [February 26, 2024, 6:08pm UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323/4 "2024-02-26T18:08:46Z")

</div>

Technically I could also return `{error, Reason}` instead of throwing badarg 🙂 My code uses similar code like this (from the examples) to check the arguments

```erlang
if (!enif_get_int(env, argv[0], &x)) {
  return enif_make_badarg(env);
}

```

I was wondering maybe there’s a way to modify this pattern to produce more descriptive error messages in the shell.

---

<div class="post-metadata">

**Author:** ![max-au](https://erlangforums.com/user_avatar/erlangforums.com/max-au/32/1627_2.png) [@max-au](https://erlangforums.com/u/max-au)\
**Post date:** [April 17, 2024, 3:03am UTC](https://erlangforums.com/t/throw-badarg-with-more-information-from-a-nif/3323/5 "2024-04-17T03:03:04Z")

</div>

> [@NAR](#):
>
> Is there a way to produce such `badarg` errors from NIFs?

The easiest way is to add a “proxy” function in the Erlang module, that throws exception based on the NIF result. I used this technique in `sqlite` bindings, e.g. this way: [sqlite/src/sqlite.erl at master · max-au/sqlite · GitHub](https://github.com/max-au/sqlite/blob/master/src/sqlite.erl#L390)
