Skip to content

Commit b09a184

Browse files
nsicchaclaude
andcommitted
Pre-format error cause at throw time for readable VSCode fallback
When VSCode can't dispatch to custom showerror (world-age issue on first eval), the default struct display now shows the pre-formatted error string instead of raw backtrace pointers and CanonicalExpr objects. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3a9cbb7 commit b09a184

2 files changed

Lines changed: 25 additions & 17 deletions

File tree

src/StanBlocks.jl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,32 @@ unwrap_error(e::CompositeException) = unwrap_error(first(e.exceptions))
4545
unwrap_error(e::StanBlocksError) = unwrap_error(_cause_error(e))
4646
unwrap_error(e) = e
4747

48-
Base.show(io::IO, e::StanBlocksError) = showerror(io, e)
49-
Base.show(io::IO, ::MIME"text/plain", e::StanBlocksError) = showerror(io, e)
50-
51-
function _showerror_body(io::IO, e::StanBlocksError)
52-
print(io, "StanBlocksError [$(e.phase)]: $(e.context)\n")
53-
print(io, " Caused by: ")
54-
showerror(io, unwrap_error(e))
55-
expr_stack = _cause_expr_stack(e)
56-
if !isempty(expr_stack)
57-
println(io, "\n\n While processing:")
58-
for (i, item) in enumerate(reverse(expr_stack))
59-
x, lnn = item isa Tuple ? item : (item, nothing)
60-
loc = lnn isa LineNumberNode ? " at $(lnn.file):$(lnn.line)" : ""
61-
println(io, " [$i] $x$loc")
48+
function _format_cause(phase, context, cause_error, expr_stack)
49+
sprint() do io
50+
print(io, "StanBlocksError [$(phase)]: $(context)\n")
51+
print(io, " Caused by: ")
52+
showerror(io, cause_error)
53+
if !isempty(expr_stack)
54+
println(io, "\n\n While processing:")
55+
for (i, item) in enumerate(reverse(expr_stack))
56+
x, lnn = item isa Tuple ? item : (item, nothing)
57+
loc = lnn isa LineNumberNode ? " at $(lnn.file):$(lnn.line)" : ""
58+
println(io, " [$i] $x$loc")
59+
end
6260
end
6361
end
6462
end
6563

66-
Base.showerror(io::IO, e::StanBlocksError) = _showerror_body(io, e)
64+
Base.show(io::IO, e::StanBlocksError) = showerror(io, e)
65+
Base.show(io::IO, ::MIME"text/plain", e::StanBlocksError) = showerror(io, e)
66+
67+
function Base.showerror(io::IO, e::StanBlocksError)
68+
print(io, e.cause isa String ? e.cause : _format_cause(e.phase, e.context, unwrap_error(e), _cause_expr_stack(e)))
69+
end
6770

6871
function Base.showerror(io::IO, e::StanBlocksError, bt; kwargs...)
6972
try
70-
_showerror_body(io, e)
73+
showerror(io, e)
7174
catch internal_err
7275
print(io, "StanBlocksError [$(e.phase)]: $(e.context)")
7376
print(io, "\n (internal error in showerror: ")

src/slic_stan/slic.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,12 @@ stan_model(x::SlicModel; info=StanModel()) = begin
273273
catch e
274274
e isa _StanBlocksError && rethrow()
275275
bt = catch_backtrace()
276-
throw(_StanBlocksError(:transpile, "model", (e, bt, _expr_stack)))
276+
msg = try
277+
parentmodule(@__MODULE__)._format_cause(:transpile, "model", e, _expr_stack)
278+
catch
279+
(e, bt, _expr_stack)
280+
end
281+
throw(_StanBlocksError(:transpile, "model", msg))
277282
end
278283
end
279284
end

0 commit comments

Comments
 (0)