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

errors returned from pcall contains serialized binaries #164

Open
markmeeus opened this issue Sep 29, 2023 · 7 comments
Open

errors returned from pcall contains serialized binaries #164

markmeeus opened this issue Sep 29, 2023 · 7 comments

Comments

@markmeeus
Copy link
Contributor

after running this code:

result, error = pcall(function()     
  ipairs("hello")
end)

The error variable contains badarg in ipairs: [<<104,101,108,108,111>>]

Expected: badarg in ipairs: ["hello"]

The reason for this formatting is that the format_error uses ~w formatting.

format_error({badarg,Where,As}) ->
    io_lib:format("badarg in ~w: ~w", [Where,As]);

Would it be possible to use ~s in the error formatting functions?

format_error({badarg,Where,As}) ->
    io_lib:format("badarg in ~s: ~s", [Where,As]);
@markmeeus markmeeus changed the title errors in pcall contain serialized binaries errors returned from pcall contains serialized binaries Sep 29, 2023
@rvirding
Copy link
Owner

The trouble is that the value of the of the arguments can be any valid Lua data type. Try:

result, error = pcall(function()     
  ipairs(dofile)
end)

return result, error

@markmeeus
Copy link
Contributor Author

Ah yes, I see, the format throws argument errors when the format param is not compatible.
I think I was expecting an elixir 'inspect' type of formatting there.

So to fix this we would need format_argument type of functions for every possible argument type that goes into the io_lib:format. These would then format the structure to a string, (maybe with a default implementation using a plain ~w)
And then use the string format?

So that the format_error would look like this:

format_error({badarg,Where,As}) ->
    io_lib:format("badarg in ~s: ~s", [format_where(Where), format_args(As)]);

@rvirding
Copy link
Owner

rvirding commented Oct 1, 2023

You have discovered another bug which needs fixing first. 😄 The ipairs function never checks its argument and always succeeds, it is the function which it returns which checks that its argument is a table.

I will fix that first and then we can get back to the error message from pcall. I have some ideas about that.

@rvirding
Copy link
Owner

rvirding commented Oct 6, 2023

The ipairs and pairs handling of arguments has now been fixed.

@rvirding
Copy link
Owner

rvirding commented Oct 6, 2023

One thing is that the current get_stacktrace function is really designed to build a "better" current stack and not really print it out. That could be another function.

@markmeeus
Copy link
Contributor Author

The thing is that this formatting happens in the pcall, so these binaries are returned to the lua code. These users may not be familiar to these erlang binary format .

@rvirding
Copy link
Owner

This has now been fixed a commit in the develop branch dd78ff5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants