Skip to content

Commit 080a6bd

Browse files
authored
Merge pull request #47 from esl/fix-pp-remote-name
Fix pretty printing remote name in pp_expr function
2 parents 38cd2d8 + 9504b3f commit 080a6bd

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

lib/gradient/elixir_expr.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ defmodule Gradient.ElixirExpr do
5555
"\'" <> List.to_string(charlist) <> "\'"
5656
end
5757

58+
def pp_expr({:remote, _, _, _} = remote_name) do
59+
pp_name(remote_name)
60+
end
61+
5862
def pp_expr({:cons, _, _, _} = cons) do
5963
case cons_to_int_list(cons) do
6064
{:ok, l} ->
@@ -291,6 +295,9 @@ defmodule Gradient.ElixirExpr do
291295
{:throw, :not_found} ->
292296
# throw
293297
pp_expr(type) <> ", " <> pp_expr(var) <> " -> " <> pp_expr(body)
298+
299+
{_variable, :not_found} ->
300+
pp_expr(type) <> ", " <> pp_expr(var) <> " -> " <> pp_expr(body)
294301
end
295302
end
296303

@@ -482,6 +489,9 @@ defmodule Gradient.ElixirExpr do
482489
defp pp_cons({:cons, _, h, {:var, _, _} = v}), do: pp_expr(h) <> " | " <> pp_expr(v)
483490
defp pp_cons({:cons, _, h, t}), do: pp_expr(h) <> ", " <> pp_cons(t)
484491

492+
defp pp_name({:remote, _, {:var, _, _} = var, {:atom, _, n}}),
493+
do: pp_expr(var) <> "." <> to_string(n)
494+
485495
defp pp_name({:remote, _, {:atom, _, m}, {:atom, _, n}}),
486496
do: ElixirFmt.parse_module(m) <> to_string(n)
487497

1.99 KB
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule CallRemoteException do
2+
def call(conn, opts) do
3+
try do
4+
:ok
5+
rescue
6+
e in Plug.Conn.WrapperError ->
7+
exception = Exception.normalize(:error, e.reason, e.stack)
8+
_ = Sentry.capture_exception(exception, stacktrace: e.stack, event_source: :plug)
9+
Plug.Conn.WrapperError.reraise(e)
10+
11+
e ->
12+
_ = Sentry.capture_exception(e, stacktrace: __STACKTRACE__, event_source: :plug)
13+
:erlang.raise(:error, e, __STACKTRACE__)
14+
catch
15+
kind, reason ->
16+
message = "Uncaught #{kind} - #{inspect(reason)}"
17+
stack = __STACKTRACE__
18+
_ = Sentry.capture_message(message, stacktrace: stack, event_source: :plug)
19+
:erlang.raise(kind, reason, stack)
20+
end
21+
end
22+
end

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

test/gradient/elixir_fmt_test.exs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,53 @@ defmodule Gradient.ElixirFmtTest do
239239
end
240240
end
241241

242+
@tag :skip
243+
test "format call_intersect error" do
244+
error =
245+
{:type_error, :call_intersect, 7,
246+
[
247+
{:type, 0, :bounded_fun,
248+
[
249+
{:type, 0, :fun,
250+
[
251+
{:type, 0, :product,
252+
[
253+
{:atom, 0, :error},
254+
{:type, 0, :any, []},
255+
{:user_type, [file: 'Elixir.Exception.erl', location: 0], :stacktrace, []}
256+
]},
257+
{:user_type, [file: 'Elixir.Exception.erl', location: 0], :t, []}
258+
]},
259+
[]
260+
]},
261+
{:type, 0, :bounded_fun,
262+
[
263+
{:type, 0, :fun,
264+
[
265+
{:type, 0, :product,
266+
[
267+
{:user_type, [file: 'Elixir.Exception.erl', location: 0], :non_error_kind, []},
268+
{:var, 0, :payload},
269+
{:user_type, [file: 'Elixir.Exception.erl', location: 0], :stacktrace, []}
270+
]},
271+
{:var, 0, :payload}
272+
]},
273+
[]
274+
]}
275+
], {:remote, 7, {:atom, 7, Exception}, {:atom, 7, :normalize}}}
276+
277+
res = ElixirFmt.format_error(error, [])
278+
279+
expected = ~s"""
280+
The type of the function Exception.normalize, called on line 7 doesn't match the surrounding calling context.
281+
It has the following type
282+
\e[35m(:error, any(), stacktrace() -> t())\e[0m
283+
\e[35m(non_error_kind(), payload, stacktrace() -> payload)\e[0m\n
284+
"""
285+
286+
assert expected == :erlang.iolist_to_binary(res)
287+
end
288+
242289
@tag :skip
243290
test "format_expr_type_error/4" do
244291
opts = [forms: basic_erlang_forms()]

test/support/expr_data.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ defmodule Gradient.ExprData do
2828
{"char", {:char, 0, ?c}, "?c"},
2929
{"float", {:float, 0, 12.0}, "12.0"},
3030
{"integer", {:integer, 0, 1}, "1"},
31-
{"erlang string", {:string, 0, 'ala ma kota'}, ~s('ala ma kota')}
31+
{"erlang string", {:string, 0, 'ala ma kota'}, ~s('ala ma kota')},
32+
{"remote name", {:remote, 7, {:atom, 7, Exception}, {:atom, 7, :normalize}},
33+
"Exception.normalize"}
3234
]
3335
end
3436

0 commit comments

Comments
 (0)