Browse Source

better error message when executing code fails

pull/5/head
Fredrik Ekre 8 years ago
parent
commit
0d424b6081
  1. 19
      src/Literate.jl
  2. 10
      test/runtests.jl

19
src/Literate.jl

@ -452,16 +452,13 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
if execute if execute
@info "executing notebook $(name * ".ipynb")" @info "executing notebook $(name * ".ipynb")"
try try
# run(`jupyter nbconvert --ExecutePreprocessor.timeout=-1 --to notebook --execute $(abspath(outputfile)) --output $(filename(outputfile)).ipynb`)
cd(outputdir) do cd(outputdir) do
nb = execute_notebook(nb) nb = execute_notebook(nb)
end end
catch err catch err
@error "error when executing notebook $(name * ".ipynb")" @error "error when executing notebook based on input file: $(inputfile)"
rethrow(err) rethrow(err)
end end
# clean up (only needed for jupyter-nbconvert)
rm(joinpath(outputdir, ".ipynb_checkpoints"), force=true, recursive = true)
end end
# write to file # write to file
@ -477,7 +474,6 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
end end
function execute_notebook(nb) function execute_notebook(nb)
# sandbox module for the notebook (TODO: Do this in Main?)
m = Module(gensym()) m = Module(gensym())
io = IOBuffer() io = IOBuffer()
@ -486,7 +482,7 @@ function execute_notebook(nb)
cell["cell_type"] == "code" || continue cell["cell_type"] == "code" || continue
execution_count += 1 execution_count += 1
cell["execution_count"] = execution_count cell["execution_count"] = execution_count
block = join(cell["source"], '\n') block = join(cell["source"])
# r is the result # r is the result
# status = (true|false) # status = (true|false)
# _: backtrace # _: backtrace
@ -494,7 +490,16 @@ function execute_notebook(nb)
r, status, _, str = Documenter.withoutput() do r, status, _, str = Documenter.withoutput() do
include_string(m, block) include_string(m, block)
end 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 # str should go into stream
if !isempty(str) if !isempty(str)

10
test/runtests.jl

@ -565,6 +565,16 @@ end
@test idx !== nothing @test idx !== nothing
lastidx = nextind(notebook, last(idx)) lastidx = nextind(notebook, last(idx))
end 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 end
end end

Loading…
Cancel
Save