Skip to content

call_function returns terminal-formatted (ANSI + location + suggestion) string as :error reason #336

Closed

Description

@davydog187

Summary

Lua.call_function/3 (and the {:error, reason, _} it returns) surfaces the full terminal-formatted error string as reason — ANSI escape codes, the at <source>:<line>: location header, the Suggestion: block, and stack-trace frames — rather than a terse message. Host code that protects a call and shows the reason to users gets a multi-line, ANSI-laced blob.

Repro

lua = Lua.new()
{[f], lua} = Lua.load!(lua, "return function() error('boom') end")

{:error, reason, _} = Lua.call_function(lua, f, [])
reason
#=> "Lua runtime error: \e[2mat <eval>:\e[0m\n\n  runtime error: boom"

Same shape for index nil / call non-function (each also appends a Suggestion: section).

Why it's a problem

do_call_function/3 rescues the VM exception and returns Exception.message(e) as reason. For Lua.VM.RuntimeError/TypeError/AssertionError, Exception.message/1 runs ErrorFormatter.format/3 — the terminal renderer (location header, source pointer, stack trace, suggestion, ANSI when IO.ANSI.enabled?/0).

That's the right output for a CLI/REPL, but call_function's :error reason is a programmatic value. Consumers that catch it and re-surface it (logs, events, an error string handed back to Lua) now get:

  • ANSI escape codes embedded in stored/transmitted strings (when a TTY is attached),
  • a redundant doubled prefix — Lua runtime error: ... runtime error: boom,
  • multi-line location/suggestion/stack-trace noise where a one-line message is expected.

Downstream (sidecar): our host-side protected-call helper feeds reason into a workflow_fail_step event reason shown in the product UI. Pre-1.0 (luerl) this was Lua runtime error: boom; under rc.1 it's the blob above. PUC-Lua's own pcall returns just <source>:<line>: boom.

Proposed fix

Decouple the programmatic reason from the terminal render. call_function's :error reason should be the terse message (what pcall hands back — the §6.1 source:line:-prefixed value, available as RuntimeError.lua_value, or the raw raw_message), with no ANSI / suggestion / stack-trace. Keep ErrorFormatter.format/3 for Lua.RuntimeException's display message and Mix/CLI surfaces, but don't bake terminal formatting into the value returned from a programmatic call boundary.

(Reported from sidecar pinned at 9c3f2b2.)

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