Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

follow RMarkdown more: make throw_error option into chunk option #371

Merged
merged 1 commit into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/src/chunk_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ we've mostly followed [RMarkdown's namings](http://yihui.name/knitr/options), bu
### Evaluation

- `eval = true`: Evaluate the code chunk. If `false` the chunk won’t be executed.
- `error = true`: If `true` [`weave`](@ref) won't stop on errors and rather they will be included in output document. If `false`, [`weave`](@ref) will halt on any of un-caught errors.
- `cache = false`: Cache results, depending on `cache` parameter on [`weave`](@ref) function.
- `tangle = true`: Set tangle to `false` to exclude chunk from tangled code.

Expand Down
4 changes: 0 additions & 4 deletions src/Weave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ Weave an input document to output file.
* `:all` caches everything
* `:user` caches based on chunk options
* `:refresh` runs all code chunks and save new cache
- `throw_errors::Bool = false`: If `false` errors are included in output document and the whole document is executed. If `true` errors are thrown when they occur
- `template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing`: Template (file path) or `Mustache.MustacheTokens`s for `md2html` or `md2tex` formats
- `css::Union{Nothing,AbstractString} = nothing`: Path of a CSS file used for md2html format
- `highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing`: Theme used for syntax highlighting (defaults to `Highlights.Themes.DefaultTheme`)
Expand All @@ -118,7 +117,6 @@ function weave(
fig_ext::Union{Nothing,AbstractString} = nothing,
cache_path::AbstractString = "cache",
cache::Symbol = :off,
throw_errors::Bool = false,
template::Union{Nothing,AbstractString,Mustache.MustacheTokens} = nothing,
css::Union{Nothing,AbstractString} = nothing, # TODO: rename to `stylesheet`
highlight_theme::Union{Nothing,Type{<:Highlights.AbstractTheme}} = nothing,
Expand Down Expand Up @@ -158,7 +156,6 @@ function weave(
fig_ext = get(weave_options, "fig_ext", fig_ext)
cache_path = get(weave_options, "cache_path", cache_path)
cache = Symbol(get(weave_options, "cache", cache))
throw_errors = get(weave_options, "throw_errors", throw_errors)
end

doc = run_doc(
Expand All @@ -171,7 +168,6 @@ function weave(
fig_ext = fig_ext,
cache_path = cache_path,
cache = cache,
throw_errors = throw_errors,
)

# render document
Expand Down
1 change: 1 addition & 0 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _DEFAULT_PARAMS = Dict{Symbol,Any}(
:hold => false,
:fig => true,
:eval => true,
:error => true,
:tangle => true,
:cache => false,
:fig_cap => nothing,
Expand Down
18 changes: 2 additions & 16 deletions src/display_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,10 @@ mutable struct Report <: AbstractDisplay
mimetypes::Vector{String}
first_plot::Bool
header_script::String
throw_errors::Bool
end

function Report(cwd, basename, format, mimetypes, throw_errors)
Report(
cwd,
basename,
format,
"",
1,
String[],
nothing,
mimetypes,
true,
"",
throw_errors,
)
end
Report(cwd, basename, format, mimetypes) =
Report(cwd, basename, format, "", 1, String[], nothing, mimetypes, true, "")

# Default mimetypes in order, can be overridden for some inside `run method` formats
const default_mime_types = ["image/svg+xml", "image/png", "text/html", "text/plain"]
Expand Down
9 changes: 4 additions & 5 deletions src/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function run_doc(
fig_ext::Union{Nothing,AbstractString} = nothing,
cache_path::AbstractString = "cache",
cache::Symbol = :off,
throw_errors::Bool = false,
)
# cache :all, :user, :off, :refresh

Expand Down Expand Up @@ -43,7 +42,7 @@ function run_doc(

mimetypes = doc.format.mimetypes

report = Report(cwd, doc.basename, doc.format, mimetypes, throw_errors)
report = Report(cwd, doc.basename, doc.format, mimetypes)
cd_back = let d = pwd(); () -> cd(d); end
cd(cwd)
pushdisplay(report)
Expand Down Expand Up @@ -207,7 +206,7 @@ function run_code(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module)
doc.path,
chunk.options[:term],
i == n,
report.throw_errors,
chunk.options[:error],
)
figures = report.figures # Captured figures
result = ChunkOutput(s, out, report.rich_output, figures)
Expand All @@ -230,7 +229,7 @@ function parse_input(s)
return res
end

function capture_output(mod, s, path, term, lastline, throw_errors = false)
function capture_output(mod, s, path, term, lastline, error)
local out = nothing
local obj = nothing

Expand All @@ -244,7 +243,7 @@ function capture_output(mod, s, path, term, lastline, throw_errors = false)
!isnothing(obj) && (term || lastline) && display(obj)
catch _err
err = unwrap_load_err(_err)
throw_errors && throw(err)
error || throw(err)
display(err)
@warn "ERROR: $(typeof(err)) occurred, including output in Weaved document"
finally
Expand Down
15 changes: 13 additions & 2 deletions test/test_error_rendering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $err_stmt1
$err_stmt2
```

```julia; term=true
```julia; term = true
$err_stmt3
```
"""
Expand All @@ -47,6 +47,17 @@ let doc = mock_run(str; doctype = "github")
@test occursin(err_str3_2, get_output(3))
end

@test_throws ArgumentError mock_run(str; doctype = "github", throw_errors = true)
# TODO: move this into chunk option tests
str = """
```julia; error = true
using # won't be thrown
```

```julia; error = false
using NonExisting # will be thrown
```
"""

@test_throws ArgumentError mock_run(str; doctype = "github")

# TODO: test error rendering in `rich_output`