From c886fefd7214c0e24d9ea1453cc8c302eeceb363 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Thu, 6 Dec 2018 13:31:56 +1000 Subject: [PATCH] Make ExceptionStack a type alias only 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. --- base/error.jl | 23 +++++++++-------------- base/errorshow.jl | 17 ----------------- stdlib/Logging/src/ConsoleLogger.jl | 1 + 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/base/error.jl b/base/error.jl index da254b823df7d..b67b1d5126016 100644 --- a/base/error.jl +++ b/base/error.jl @@ -91,19 +91,14 @@ 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 @@ -111,16 +106,16 @@ 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 diff --git a/base/errorshow.jl b/base/errorshow.jl index be2f481d12989..54e3e4c678656 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -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 diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index 1f8ecba69188b..a484124cdf0fa 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -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)