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

raising inside dbg is not handled #14005

Open
dkuku opened this issue Nov 20, 2024 · 4 comments
Open

raising inside dbg is not handled #14005

dkuku opened this issue Nov 20, 2024 · 4 comments

Comments

@dkuku
Copy link
Contributor

dkuku commented Nov 20, 2024

Elixir and Erlang/OTP versions

any recent

Operating system

any

Current behavior

Currently when there is an exception we don't get any output in dbg. When debugging pipes I need to add inspects in the pipeline idealy with labels to see where it failed.

[:a, :b]
|> then(fn _ -> raise "breaks dbg" end)
|> dbg

returns

** (RuntimeError) breaks dbg
    #cell:exemuvvgh6ktixl2:2: (file)

Expected behavior

Rendering partial result would be ideal, this way we would know what was the input to the failing function.
Currently the dbg is generated in 2 steps

  • code is executed
  • then the result string is generated

It may be possible if the dbg result would be generated where the code is executed and in case of exceptions we could just display what was generated up to that point.

      abc() #=> [:a, :b, :c]
     |> tl() #=> [:b, :c]
     |> then(fn _ -> raise "boom" end)
      ** (RuntimeError) boom
    #cell:exemuvvgh6ktixl2:3: (file)
@sabiwara
Copy link
Contributor

It may be possible if the dbg result would be generated where the code is executed and in case of exceptions we could just display what was generated up to that point.

It's an interesting idea, but I think it's a trade-off though: if intermediate steps are printing to stdout too, the pipeline might become unreadable:

iex(1)> :world |> then(&IO.puts("hello #{&1}!")) |> Atom.to_string() |> dbg()
hello world!
[iex:1: (file)]
:world #=> :world
|> then(&IO.puts("hello #{&1}!")) #=> :ok
|> Atom.to_string() #=> "ok"

would become

[iex:1: (file)]
:world #=> :world
hello world!
|> then(&IO.puts("hello #{&1}!")) #=> :ok
|> Atom.to_string() #=> "ok"

We could also imagine wrapping in a try + reraise, but I'm not sure it's a good approach either. Adding more intermediate dbg before the raising step might be the best approach here?

@dkuku
Copy link
Contributor Author

dkuku commented Nov 20, 2024

What motivated me to create this issue was dbg on a simple function call:

function(param1, param2)
|> dbg

The function raised and I had to add inspects before it.

@josevalim
Copy link
Member

I would avoid try-catch. But I think the intermediate printing would be nice!

@dkuku
Copy link
Contributor Author

dkuku commented Nov 23, 2024

Something like this example? This will allow calling dbg on the condition.
It would be good to refactor it first to a new Macro.Dbg module

  defp dbg_ast_to_debuggable({:if, meta, [condition_ast, clauses]} = ast, env, options) do
    condition_result_var = unique_var(:condition_result, __MODULE__)
    result_var = unique_var(:result, __MODULE__)

    quote do
      Macro.write_underline("If condition", unquote(options))
      unquote(condition_result_var) = unquote(condition_ast)

      Macro.write_ast_value(
        unquote(escape(condition_ast)),
        unquote(condition_result_var),
        unquote(options)
      )

      Macro.write_underline("If expression", unquote(options))
      unquote(result_var) = unquote({:if, meta, [condition_result_var, clauses]})

      Macro.write_ast_value(
        unquote(escape(ast)),
        unquote(result_var),
        unquote(options)
      )

      unquote(result_var)
    end
  end

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

No branches or pull requests

3 participants