From 2d24c29376ec7be689d830511351e27007ab1050 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Fri, 4 Oct 2024 10:48:49 +0200 Subject: [PATCH] Stack trace: bracket-matched hiding and expanding --- frontend/components/ErrorMessage.js | 57 +++++++++++-------- .../PlutoRunner/src/display/Exception.jl | 16 +++++- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/frontend/components/ErrorMessage.js b/frontend/components/ErrorMessage.js index c980ef119..a8c19f964 100644 --- a/frontend/components/ErrorMessage.js +++ b/frontend/components/ErrorMessage.js @@ -89,38 +89,47 @@ const ignore_funccall = (frame) => frame.call === "top-level scope" const ignore_location = (frame) => frame.file === "none" const Funccall = ({ frame }) => { - if (ignore_funccall(frame)) return null + let [expanded_state, set_expanded] = useState(false) + useEffect(() => { + set_expanded(false) + }, [frame]) - const bracket_index = frame.call.indexOf("(") + const silly_to_hide = (frame.call_short.match(/…/g) ?? "").length <= 1 && frame.call.length < frame.call_short.length + 7 - let inner = - bracket_index != -1 - ? html`${frame.call.substr(0, bracket_index)}<${ClickToExpandIfLong} text=${frame.call.substr(bracket_index)} />` - : html`${frame.call}` + const expanded = expanded_state || (frame.call === frame.call_short && frame.func === frame.call.split("(", 2)[0]) || silly_to_hide - return html`${inner}` -} + if (ignore_funccall(frame)) return null -const LIMIT_LONG = 200, - LIMIT_PREVIEW = 100 + const call = expanded ? frame.call : frame.call_short -const ClickToExpandIfLong = ({ text }) => { - let [expanded, set_expanded] = useState(false) + const bracket_index = call.indexOf("(") + const funcname = expanded ? call.split("(", 2)[0] : frame.func - useEffect(() => { - set_expanded(false) - }, [text]) + // if function name is #12 or #15#16 then it is an anonymous function - const collaped_text = html`${text.slice(0, LIMIT_PREVIEW)} { - e.preventDefault() - set_expanded(true) - }} - >...Show more...${text.slice(-1)}` + const funcname_display = funcname.match(/^#\d+(#\d+)?$/) + ? html`anonymous function` + : funcname + console.log(funcname, funcname.match(/^#\d+(#\d+)?$/), funcname_display) + + let inner = bracket_index != -1 ? html`${funcname_display}${call.substr(bracket_index)}` : html`${funcname_display}` + + const id = useMemo(() => Math.random().toString(36).substring(7), [frame]) - return html`${expanded ? text : text.length < LIMIT_LONG ? text : collaped_text}` + return html`${inner} ${!expanded + ? html` { + e.preventDefault() + set_expanded(true) + }} + >...show types...` + : null}` } const LinePreview = ({ frame, num_context_lines = 2 }) => { diff --git a/src/runner/PlutoRunner/src/display/Exception.jl b/src/runner/PlutoRunner/src/display/Exception.jl index 835c500ce..66e7e1b76 100644 --- a/src/runner/PlutoRunner/src/display/Exception.jl +++ b/src/runner/PlutoRunner/src/display/Exception.jl @@ -60,12 +60,16 @@ function format_output(val::CapturedException; context=default_iocontext) stack_relevant = stack[1:something(limit, end)] pretty = map(stack_relevant) do s + func = s.func === nothing ? nothing : s.func isa Symbol ? String(s.func) : repr(s.func) method = method_from_frame(s) sp = source_package(method) pm = VERSION >= v"1.9" && method isa Method ? parentmodule(method) : nothing + call = replace(pretty_stackcall(s, s.linfo), r"Main\.var\"workspace#\d+\"\." => "") Dict( - :call => replace(pretty_stackcall(s, s.linfo), r"Main\.var\"workspace#\d+\"\." => ""), + :call => call, + :call_short => type_depth_limit(call, 0), + :func => func, :inlined => s.inlined, :from_c => s.from_c, :file => basename(String(s.file)), @@ -124,6 +128,16 @@ function pretty_stackcall(frame::Base.StackFrame, linfo::Module) end +function type_depth_limit(call::String, n::Int) + !occursin("{" , call) && return call + @static if isdefined(Base, :type_depth_limit) && hasmethod(Base.type_depth_limit, Tuple{String, Int}) + Base.type_depth_limit(call, n) + else + call + end +end + + "Because even showerror can error... 👀" function try_showerror(io::IO, e, args...) try