Skip to content
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

Always return Elixir exception even there is no Zig stack trace #40

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
fail-fast: false
matrix:
runs-on: ["ubuntu-latest"]
runs-on: ["ubuntu-22.04"]
otp: ["24.2", "25.0"]
elixir: ["1.13.0", "1.16.2", "1.18.0"]
exclude:
Expand Down
30 changes: 12 additions & 18 deletions src/beam.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1373,37 +1373,31 @@ fn writeStackTraceToBuffer(

pub fn make_exception(env_: env, exception_module: []const u8, err: anyerror, error_trace: ?*std.builtin.StackTrace) term {
const erl_err = make_slice(env_, @errorName(err));
var stack_trace = make_nil(env_);
if (error_trace) |trace| {
var stack_trace = make_nil(env_);
if (std.posix.getenv("KINDA_DUMP_STACK_TRACE")) |KINDA_DUMP_STACK_TRACE| {
if (std.mem.eql(u8, KINDA_DUMP_STACK_TRACE, "1")) {
stack_trace = writeStackTraceToBuffer(env_, trace.*) catch make_nil(env_);
} else if (std.mem.eql(u8, KINDA_DUMP_STACK_TRACE, "stderr")) {
std.debug.dumpStackTrace(trace.*);
}
}
var exception = e.enif_make_new_map(env_);
// define the struct
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "__struct__"), make_atom(env_, exception_module), &exception);
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "__exception__"), make_bool(env_, true), &exception);
// define the error
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "message"), erl_err, &exception);
}
var exception = e.enif_make_new_map(env_);
// define the struct
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "__struct__"), make_atom(env_, exception_module), &exception);
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "__exception__"), make_bool(env_, true), &exception);
// define the error
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "message"), erl_err, &exception);

// store the error return trace
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "error_return_trace"), stack_trace, &exception);
// store the error return trace
_ = e.enif_make_map_put(env_, exception, make_atom(env_, "error_return_trace"), stack_trace, &exception);
jackalcooper marked this conversation as resolved.
Show resolved Hide resolved

return exception;
} else {
return erl_err;
}
return exception;
}

pub fn raise_exception(env_: env, exception_module: []const u8, err: anyerror, error_trace: ?*std.builtin.StackTrace) term {
if (error_trace) |_| {
return e.enif_raise_exception(env_, make_exception(env_, exception_module, err, error_trace));
} else {
return make_nil(env_);
}
return e.enif_raise_exception(env_, make_exception(env_, exception_module, err, error_trace));
}

/// !value
Expand Down
Loading