Skip to content

Commit aaabb4d

Browse files
committed
Add a testcase that pp_expr from call_remote_exception.ex example
1 parent c6c4b04 commit aaabb4d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/gradient/elixir_expr_test.exs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,69 @@ defmodule Gradient.ElixirExprTest do
326326
actual
327327
end
328328
end
329+
330+
test "pp and format complex try expression" do
331+
{_tokens, ast} =
332+
Gradient.TestHelpers.load("/Elixir.CallRemoteException.beam", "/call_remote_exception.ex")
333+
334+
{:function, _, :call, 2, [{:clause, _ann, _args, _guards, [try_expr]}]} =
335+
Enum.reverse(ast) |> List.first()
336+
337+
res = ElixirExpr.pp_expr_format(try_expr)
338+
339+
# FIXME `raise {:badkey, :stack, _gen}` is not correct
340+
341+
expected = ~s"""
342+
try do
343+
:ok
344+
catch
345+
:error, %Plug.Conn.WrapperError{} = e ->
346+
exception =
347+
Exception.normalize(
348+
:error,
349+
case e do
350+
%{reason: _gen} -> _gen
351+
_gen when :erlang.is_map(_gen) -> raise {:badkey, :reason, _gen}
352+
_gen -> _gen.reason()
353+
end,
354+
case e do
355+
%{stack: _gen} -> _gen
356+
_gen when :erlang.is_map(_gen) -> raise {:badkey, :stack, _gen}
357+
_gen -> _gen.stack()
358+
end
359+
)
360+
361+
_ =
362+
Sentry.capture_exception(exception, [
363+
{:stacktrace,
364+
case e do
365+
%{stack: _gen} -> _gen
366+
_gen when :erlang.is_map(_gen) -> raise {:badkey, :stack, _gen}
367+
_gen -> _gen.stack()
368+
end},
369+
{:event_source, :plug}
370+
])
371+
372+
Plug.Conn.WrapperError.reraise(e)
373+
374+
:error, e ->
375+
_ = Sentry.capture_exception(e, [{:stacktrace, __STACKTRACE__}, {:event_source, :plug}])
376+
:erlang.raise(:error, e, __STACKTRACE__)
377+
378+
kind, reason ->
379+
message =
380+
<<"Uncaught ",
381+
case kind do
382+
_gen when :erlang.is_binary(_gen) -> _gen
383+
_gen -> String.Chars.to_string(_gen)
384+
end::binary, " - ", Kernel.inspect(reason)::binary>>
385+
386+
stack = __STACKTRACE__
387+
_ = Sentry.capture_message(message, [{:stacktrace, stack}, {:event_source, :plug}])
388+
:erlang.raise(kind, reason, stack)
389+
end
390+
"""
391+
392+
assert expected == :erlang.iolist_to_binary(res) <> "\n"
393+
end
329394
end

0 commit comments

Comments
 (0)