Skip to content

Exception.message/1 on exception returns ANSI codes #384

Closed

Description

@kevinschweikert

After #383 i tried it again in our codebase. Claude created this summary of the issue. I verified the reproduction.

VM error structs and CompilerException bake their fully-formatted (andANSI-colored) text into :message at construction time. Since that happens during VM execution — where IO.ANSI.enabled?/0 is true — the escape codes and multi-line render survive into non-TTY sinks: log files, containerstdout, Sentry/AppSignal titles. The formatter's TTY gate never fires at the point output is actually written.

Reproduction

iex(1)> ex = Lua.VM.RuntimeError.exception(value: "kaboom", source: "demo", line: 1)
#Lua.VM.RuntimeError<
  value: "kaboom",
  lua_value: nil,
  source: "demo",
  message: "\e[2mat demo:1:\e[0m\n\n  runtime error: kaboom",
  call_stack: [],
  line: 1,
  ...
>
iex(2)> Application.put_env(:elixir, :ansi_enabled, false)
iex(3)> Exception.message(ex)
"\e[2mat demo:1:\e[0m\n\n  runtime error: kaboom"

Root cause

ErrorFormatter.format/3 is correctly gated (color/2 → IO.ANSI.enabled?/0,error_formatter.ex:115), but it's called eagerly and the result is memoized:

  • runtime_error.ex:42 — message = ... || format_message(...) (same in assertion_error.ex, type_error.ex, argument_error.ex, internal_error.ex)
  • compiler_exception.ex:16-18 — exception/1 stores Util.format_error/1 output as strings in :errors, so parse_chunk/1's exception carries pre-rendered text too.

Freezing a TTY-dependent render at build time defeats the gate.

Expectations

No newlines and ANSI codes in an exception message, to cleanly log the errors in our log system

Activity

  1. kevinschweikert commented on Jul 13, 2026

    @kevinschweikert
    ContributorAuthor

    For a nice log i currently have to do:

    Exception.message(ex)
    |> String.replace(~r/\e\[[0-9;]*m/, "")
    |> String.split("\n")
    |> Enum.map(&String.trim/1)
    |> Enum.reject(&(&1 == ""))
    |> Enum.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions