Skip to content

Commit

Permalink
Stack trace: bracket-matched hiding and expanding
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Oct 4, 2024
1 parent 6bb6b98 commit 2d24c29
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
57 changes: 33 additions & 24 deletions frontend/components/ErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`<strong>${frame.call.substr(0, bracket_index)}</strong><${ClickToExpandIfLong} text=${frame.call.substr(bracket_index)} />`
: html`<strong>${frame.call}</strong>`
const expanded = expanded_state || (frame.call === frame.call_short && frame.func === frame.call.split("(", 2)[0]) || silly_to_hide

return html`<mark>${inner}</mark>`
}
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)}<a
href="#"
onClick=${(e) => {
e.preventDefault()
set_expanded(true)
}}
>...Show more...</a
>${text.slice(-1)}`
const funcname_display = funcname.match(/^#\d+(#\d+)?$/)
? html`<abbr title="A (mini-)function that is defined without the 'function' keyword, but using -> or 'do'.">anonymous function</abbr>`
: funcname
console.log(funcname, funcname.match(/^#\d+(#\d+)?$/), funcname_display)

let inner = bracket_index != -1 ? html`<strong>${funcname_display}</strong>${call.substr(bracket_index)}` : html`<strong>${funcname_display}</strong>`

const id = useMemo(() => Math.random().toString(36).substring(7), [frame])

return html`<span>${expanded ? text : text.length < LIMIT_LONG ? text : collaped_text}</span>`
return html`<mark id=${id}>${inner}</mark> ${!expanded
? html`<a
aria-expanded=${expanded}
aria-controls=${id}
title="Display the complete type information of this function call"
role="button"
href="#"
onClick=${(e) => {
e.preventDefault()
set_expanded(true)
}}
>...show types...</a
>`
: null}`
}

const LinePreview = ({ frame, num_context_lines = 2 }) => {
Expand Down
16 changes: 15 additions & 1 deletion src/runner/PlutoRunner/src/display/Exception.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2d24c29

Please sign in to comment.