Skip to content

Commit

Permalink
Make ExceptionStack a type alias only
Browse files Browse the repository at this point in the history
Now we match on structure alone, which is argubly a bit ropey but
doesn't require a new type.  Also allows us to remove show for
ExceptionStack, without too many ill effects.
  • Loading branch information
c42f committed Dec 6, 2018
1 parent 3137d44 commit c886fef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
23 changes: 9 additions & 14 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,31 @@ function catch_backtrace()
return _reformat_bt(bt[], bt2[])
end

struct ExceptionStack <: AbstractArray{Any,1}
stack
end

"""
current_exceptions(task=current_task(); [inclue_bt=true])
Get the stack of exceptions currently being handled. For nested catch blocks
there may be more than one current exception in which case the most recently
thrown exception is last in the stack. The stack is returned as an
`ExceptionStack` which is an AbstractVector of named tuples
`(exception,backtrace)`. If `backtrace` is false, the backtrace in each pair
will be set to `nothing`.
`ExceptionStack` which is a Vector of named tuples `(exception,backtrace)`. If
`backtrace` is false, the backtrace in each pair will be set to `nothing`.
Explicitly passing `task` will return the current exception stack on an
arbitrary task. This is useful for inspecting tasks which have failed due to
uncaught exceptions.
"""
function current_exceptions(task=current_task(); backtrace=true)
raw = ccall(:jl_get_excstack, Any, (Any,Cint,Cint), task, backtrace, typemax(Cint))
formatted = Any[]
stride = backtrace ? 3 : 1
for i = reverse(1:stride:length(raw))
exc = raw[i]
bt = backtrace ? Base._reformat_bt(raw[i+1],raw[i+2]) : nothing
push!(formatted, (exception=exc,backtrace=bt))
end
ExceptionStack(formatted)
[
(exception=raw[i],
backtrace=backtrace ? Base._reformat_bt(raw[i+1],raw[i+2]) : nothing)
for i = reverse(1:stride:length(raw))
]
end

const ExceptionStack = Array{NamedTuple{(:exception, :backtrace),T}, 1} where {T<:Tuple}

## keyword arg lowering generates calls to this ##
function kwerr(kw, args::Vararg{Any,N}) where {N}
@_noinline_meta
Expand Down
17 changes: 0 additions & 17 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,23 +628,6 @@ function process_backtrace(t::Vector, limit::Int=typemax(Int); skipC = true)
return ret
end

size(s::ExceptionStack) = size(s.stack)
getindex(s::ExceptionStack, i) = s.stack[i]

function show(io::IO, ::MIME"text/plain", stack::ExceptionStack)
nexc = length(stack)
printstyled(io, nexc, "-element ExceptionStack", nexc == 0 ? "" : ":\n")
for i = nexc:-1:1
if nexc != 1
printstyled(io, "caused by [exception ", i, "]\n", color=:light_black)
end
exc,bt = stack[i]
showerror(io, exc, bt, backtrace=bt!==nothing)
println(io)
end
end
show(io::IO, stack::ExceptionStack) = show(io, MIME"text/plain"(), stack)

@noinline function throw_eachindex_mismatch(::IndexLinear, A...)
throw(DimensionMismatch("all inputs to eachindex must have the same indices, got $(join(LinearIndices.(A), ", ", " and "))"))
end
Expand Down
1 change: 1 addition & 0 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function showvalue(io, e::Tuple{Exception,Any})
ex,bt = e
showerror(io, ex, bt; backtrace = bt!=nothing)
end
showvalue(io, stack::Base.ExceptionStack) = Base.display_error(io, stack)
showvalue(io, ex::Exception) = showerror(io, ex)

function default_logcolor(level)
Expand Down

0 comments on commit c886fef

Please sign in to comment.