diff --git a/src/Literate.jl b/src/Literate.jl index db442a5..041b67d 100644 --- a/src/Literate.jl +++ b/src/Literate.jl @@ -452,16 +452,13 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide if execute @info "executing notebook $(name * ".ipynb")" try - # run(`jupyter nbconvert --ExecutePreprocessor.timeout=-1 --to notebook --execute $(abspath(outputfile)) --output $(filename(outputfile)).ipynb`) cd(outputdir) do nb = execute_notebook(nb) end catch err - @error "error when executing notebook $(name * ".ipynb")" + @error "error when executing notebook based on input file: $(inputfile)" rethrow(err) end - # clean up (only needed for jupyter-nbconvert) - rm(joinpath(outputdir, ".ipynb_checkpoints"), force=true, recursive = true) end # write to file @@ -477,7 +474,6 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide end function execute_notebook(nb) - # sandbox module for the notebook (TODO: Do this in Main?) m = Module(gensym()) io = IOBuffer() @@ -486,7 +482,7 @@ function execute_notebook(nb) cell["cell_type"] == "code" || continue execution_count += 1 cell["execution_count"] = execution_count - block = join(cell["source"], '\n') + block = join(cell["source"]) # r is the result # status = (true|false) # _: backtrace @@ -494,7 +490,16 @@ function execute_notebook(nb) r, status, _, str = Documenter.withoutput() do include_string(m, block) end - status || error("something went wrong when evaluating code") + if !status + error(""" + $(sprint(showerror, r)) + when executing the following code block + + ```julia + $block + ``` + """) + end # str should go into stream if !isempty(str) diff --git a/test/runtests.jl b/test/runtests.jl index 2fe8c02..7d9ac05 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -565,6 +565,16 @@ end @test idx !== nothing lastidx = nextind(notebook, last(idx)) end + + # test error when executing notebook + write(inputfile, "for i in 1:10\n println(i)") + r = try + Literate.notebook(inputfile, outdir) + catch err + err + end + @test isa(r, ErrorException) + @test occursin("when executing the following code block", r.msg) end end end