[Proposal] Implement String.Chars for Exceptions

51 views
Skip to first unread message

Allen Madsen

unread,
Dec 6, 2016, 9:59:49 PM12/6/16
to elixir-lang-core
Hi all,

I was trying to log an exception and noticed that this does not work: Logger.error(exception). Logger could handle this case itself, but I think it would be better to handle it more generically.

So, I propose when a module uses the defexception macro, that Elixir automatically define the String.Chars implementation for that module. Inside the defexception macro, the implementation of String.Chars would be the following:

defimpl String.Chars, for: env.module do
  def to_string(error), do: Exception.format(:error, error)
end

Louis Pop

unread,
Dec 7, 2016, 3:18:05 AM12/7/16
to elixir-lang-core

What's the reason for not using inspect?


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/88a99607-d4ea-472c-bd1a-48dee887f5e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Valim

unread,
Dec 7, 2016, 4:03:50 AM12/7/16
to elixir-l...@googlegroups.com
Please check the functions on the Exception module for formatting exceptions into messages using different formats and arguments.

Allen Madsen

unread,
Dec 7, 2016, 8:22:36 AM12/7/16
to elixir-l...@googlegroups.com
Louis,

It will generate the string for structs rather than the formatted exception with the stacktrace. See the example below:

require Logger

defmodule X do
  defexception [:message]

  defimpl String.Chars, for: X do
    def to_string(x), do: Exception.format(:error, x)
  end
end

try do
  raise X, "x"
rescue error ->
  Logger.error(error)
end

# 08:17:59.812 [error] ** (X) x
#     (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
#     (stdlib) erl_eval.erl:904: :erl_eval.try_clauses/8
#     (elixir) src/elixir.erl:223: :elixir.erl_eval/3
#     (elixir) src/elixir.erl:211: :elixir.eval_forms/4
#     (iex) lib/iex/evaluator.ex:135: IEx.Evaluator.handle_eval/6
#     (iex) lib/iex/evaluator.ex:128: IEx.Evaluator.do_eval/4
#     (iex) lib/iex/evaluator.ex:108: IEx.Evaluator.eval/4
#     (iex) lib/iex/evaluator.ex:27: IEx.Evaluator.loop/3

defmodule Y do
  defexception [:message]

  defimpl String.Chars, for: Y do
    def to_string(y), do: inspect(y)
  end
end

try do
  raise Y, "y"
rescue error ->
  Logger.error(error)
end

# 08:18:01.364 [error] %Y{message: "y"}
On Wed, Dec 7, 2016 at 4:03 AM, José Valim <jose....@plataformatec.com.br> wrote:
Please check the functions on the Exception module for formatting exceptions into messages using different formats and arguments.
On Wed, Dec 7, 2016 at 09:18 Louis Pop <louisp...@gmail.com> wrote:

What's the reason for not using inspect?


On Wed, 7 Dec 2016, 02:59 Allen Madsen, <bla...@gmail.com> wrote:
Hi all,

I was trying to log an exception and noticed that this does not work: Logger.error(exception). Logger could handle this case itself, but I think it would be better to handle it more generically.

So, I propose when a module uses the defexception macro, that Elixir automatically define the String.Chars implementation for that module. Inside the defexception macro, the implementation of String.Chars would be the following:

defimpl String.Chars, for: env.module do
  def to_string(error), do: Exception.format(:error, error)
end

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B1tECy%2BSWBCKCwQi5KNaJ2%3Dg4stu87HouOxBU5M%3DxttQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages