-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch panics in encoding NIF results (#656)
Also adds tests for panicking in parameters, return values and the NIF function itself. Fixes #655
- Loading branch information
Showing
6 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rustler::{Decoder, Encoder, Env, Term}; | ||
|
||
#[rustler::nif] | ||
pub fn panic_in_nif() -> i32 { | ||
panic!("panic!") | ||
} | ||
|
||
struct Panicking; | ||
|
||
impl Encoder for Panicking { | ||
fn encode<'a>(&self, _env: Env<'a>) -> Term<'a> { | ||
panic!("panic in encode!") | ||
} | ||
} | ||
|
||
impl<'a> Decoder<'a> for Panicking { | ||
fn decode(_term: Term<'a>) -> rustler::NifResult<Self> { | ||
panic!("panic in decode!") | ||
} | ||
} | ||
|
||
#[rustler::nif] | ||
pub fn panic_in_encode() -> Panicking { | ||
Panicking | ||
} | ||
|
||
#[rustler::nif] | ||
pub fn panic_in_decode(_p: Panicking) -> i32 { | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule PanicTest do | ||
use ExUnit.Case | ||
|
||
test "panics in NIFs are caught" do | ||
assert_raise ErlangError, &RustlerTest.panic_in_nif/0 | ||
assert_raise ErlangError, &RustlerTest.panic_in_encode/0 | ||
|
||
assert_raise ErlangError, fn -> | ||
RustlerTest.panic_in_decode(:anything) | ||
end | ||
end | ||
end |