84
84
85
85
function scrub_repl_backtrace (bt)
86
86
if bt != = nothing && ! (bt isa Vector{Any}) # ignore our sentinel value types
87
- bt = stacktrace (bt)
87
+ bt = bt isa Vector{StackFrame} ? copy (bt) : stacktrace (bt)
88
88
# remove REPL-related frames from interactive printing
89
89
eval_ind = findlast (frame -> ! frame. from_c && frame. func === :eval , bt)
90
90
eval_ind === nothing || deleteat! (bt, eval_ind: length (bt))
91
91
end
92
92
return bt
93
93
end
94
+ scrub_repl_backtrace (stack:: ExceptionStack ) =
95
+ ExceptionStack (Any[(;x. exception, backtrace = scrub_repl_backtrace (x. backtrace)) for x in stack])
94
96
95
- function display_error (io:: IO , er, bt)
97
+ istrivialerror (stack:: ExceptionStack ) =
98
+ length (stack) == 1 && length (stack[1 ]. backtrace) ≤ 1
99
+ # frame 1 = top level; assumes already went through scrub_repl_backtrace
100
+
101
+ function display_error (io:: IO , stack:: ExceptionStack )
96
102
printstyled (io, " ERROR: " ; bold= true , color= Base. error_color ())
97
- bt = scrub_repl_backtrace (bt)
98
- showerror (IOContext (io, :limit => true ), er, bt, backtrace = bt!= = nothing )
103
+ show_exception_stack (IOContext (io, :limit => true ), stack)
99
104
println (io)
100
105
end
101
- function display_error (io:: IO , stack:: ExceptionStack )
106
+ display_error (stack:: ExceptionStack ) = display_error (stderr , stack)
107
+
108
+ # these forms are depended on by packages outside Julia
109
+ function display_error (io:: IO , exception, backtrace)
102
110
printstyled (io, " ERROR: " ; bold= true , color= Base. error_color ())
103
- bt = Any[ (x[1 ], scrub_repl_backtrace (x[2 ])) for x in stack ]
104
- show_exception_stack (IOContext (io, :limit => true ), bt)
111
+ showerror (IOContext (io, :limit => true ), er, bt, backtrace = bt!= = nothing )
105
112
println (io)
106
113
end
107
- display_error (stack:: ExceptionStack ) = display_error (stderr , stack)
108
114
display_error (er, bt= nothing ) = display_error (stderr , er, bt)
109
115
110
116
function eval_user_input (errio, @nospecialize (ast), show_value:: Bool )
@@ -117,6 +123,8 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool)
117
123
print (color_normal)
118
124
end
119
125
if lasterr != = nothing
126
+ lasterr = scrub_repl_backtrace (lasterr)
127
+ istrivialerror (lasterr) || ccall (:jl_set_global , Cvoid, (Any, Any, Any), Main, :err , lasterr)
120
128
invokelatest (display_error, errio, lasterr)
121
129
errcount = 0
122
130
lasterr = nothing
@@ -143,7 +151,8 @@ function eval_user_input(errio, @nospecialize(ast), show_value::Bool)
143
151
@error " SYSTEM: display_error(errio, lasterr) caused an error"
144
152
end
145
153
errcount += 1
146
- lasterr = current_exceptions ()
154
+ lasterr = scrub_repl_backtrace (current_exceptions ())
155
+ ccall (:jl_set_global , Cvoid, (Any, Any, Any), Main, :err , lasterr)
147
156
if errcount > 2
148
157
@error " It is likely that something important is broken, and Julia will not be able to continue normally" errcount
149
158
break
@@ -260,7 +269,7 @@ function exec_options(opts)
260
269
try
261
270
load_julia_startup ()
262
271
catch
263
- invokelatest (display_error, current_exceptions ())
272
+ invokelatest (display_error, scrub_repl_backtrace ( current_exceptions () ))
264
273
! (repl || is_interactive:: Bool ) && exit (1 )
265
274
end
266
275
end
@@ -294,7 +303,7 @@ function exec_options(opts)
294
303
try
295
304
include (Main, PROGRAM_FILE )
296
305
catch
297
- invokelatest (display_error, current_exceptions ())
306
+ invokelatest (display_error, scrub_repl_backtrace ( current_exceptions () ))
298
307
if ! is_interactive:: Bool
299
308
exit (1 )
300
309
end
@@ -496,7 +505,7 @@ function _start()
496
505
try
497
506
exec_options (JLOptions ())
498
507
catch
499
- invokelatest (display_error, current_exceptions ())
508
+ invokelatest (display_error, scrub_repl_backtrace ( current_exceptions () ))
500
509
exit (1 )
501
510
end
502
511
if is_interactive && get (stdout , :color , false )
0 commit comments