Skip to content

Commit c4ac614

Browse files
authored
Use showerror for showing captured errors (#261)
1 parent ecae718 commit c4ac614

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
### Fixed
10+
- Errors from code evaluation (with `continue_on_error = true`) are now properly displayed
11+
with `showerror`. ([#261])
12+
813
## [v2.20.0] - 2024-10-16
914
### Added
1015
- A new keyword argument configuration `continue_on_error::Bool = false` has been added

src/Literate.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,24 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source
936936
end
937937
end
938938
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here
939-
if c.error && !continue_on_error
940-
error("""
941-
$(sprint(showerror, c.value))
942-
when executing the following code block from inputfile `$(Base.contractuser(inputfile))`
943-
944-
```julia
945-
$block
946-
```
947-
""")
939+
if c.error
940+
if continue_on_error
941+
err = c.value
942+
if err isa LoadError # include_string may wrap error in LoadError
943+
err = err.error
944+
end
945+
all_output = c.output * "\n\nERROR: " * sprint(showerror, err)
946+
return nothing, all_output, disp.data
947+
else
948+
error("""
949+
$(sprint(showerror, c.value))
950+
when executing the following code block from inputfile `$(Base.contractuser(inputfile))`
951+
952+
```julia
953+
$block
954+
```
955+
""")
956+
end
948957
end
949958
return c.value, c.output, disp.data
950959
end

test/runtests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,17 @@ end end
14621462
@testset "continue_on_error=true" begin
14631463
input_with_error =
14641464
"""
1465-
# The following will error
1466-
1465+
# The following will print something, then error
1466+
print("I always wanted to calculate")
14671467
sqrt(-1.0)
14681468
"""
14691469
mktempdir(@__DIR__) do sandbox
14701470
inputfile = joinpath(sandbox, "input.jl")
14711471
write(inputfile, input_with_error)
14721472
Literate.markdown(inputfile, sandbox; continue_on_error = true, execute = true)
14731473
output_md = read(joinpath(sandbox, "input.md"), String)
1474-
@test occursin("DomainError(-1.0", output_md)
1474+
@test occursin("I always wanted to calculate", output_md)
1475+
@test occursin("ERROR: DomainError with -1.0", output_md)
14751476
end
14761477
end
14771478

0 commit comments

Comments
 (0)