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"}
Allen Madsen
http://www.allenmadsen.com
On Wed, Dec 7, 2016 at 4:03 AM, José Valim <[email protected]>
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 <[email protected]> wrote:
>
>> What's the reason for not using inspect?
>>
>> On Wed, 7 Dec 2016, 02:59 Allen Madsen, <[email protected]> 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 [email protected].
>> To view this discussion on the web visit https://groups.google.com/d/
>> msgid/elixir-lang-core/88a99607-d4ea-472c-bd1a-
>> 48dee887f5e8%40googlegroups.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/88a99607-d4ea-472c-bd1a-48dee887f5e8%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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 [email protected].
>> To view this discussion on the web visit https://groups.google.com/d/
>> msgid/elixir-lang-core/CAM-pwt5bnVvpeJd%2BKdu_PmQvPRF-
>> 3ZKDFH6Bb%2BH5q-i6TamAAQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/CAM-pwt5bnVvpeJd%2BKdu_PmQvPRF-3ZKDFH6Bb%2BH5q-i6TamAAQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> 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 [email protected].
> 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
> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B1tECy%2BSWBCKCwQi5KNaJ2%3Dg4stu87HouOxBU5M%3DxttQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3CvyFyXLiGh1coo_DR8XNhAkM0fQUmU0CVOaYbK4CJT67g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.