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

Allow rethrowing of error from @require #63

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 22 additions & 4 deletions src/require.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ function withpath(f, path)
end
end

function err(f, listener, mod)
function err(f, listener, mod, rethrowerror)
try
f()
catch e
@warn """
Error requiring $mod from $listener:
$(sprint(showerror, e, catch_backtrace()))
"""
rethrowerror && rethrow(e)
end
end

Expand All @@ -50,10 +51,27 @@ function parsepkg(ex)
(mod isa Symbol && id isa String) || @goto fail
return id, String(mod)
@label fail
error("Requires syntax is: `@require Pkg=\"uuid\"`")
error("Requires syntax is: `@require Pkg=\"uuid\" [rethrowerror=false]`")
end

macro require(pkg, expr)
function parserethrow(ex)
isexpr(ex, :(=)) || @goto fail
kw, flg = ex.args
(kw == :rethrow && flg isa Bool) || @goto fail
return flg
@label fail
error("Requires syntax is: `@require Pkg=\"uuid\" [rethrowerror=false]`")
end

macro require(pkg, args...)
rethrowerror = false
if length(args) == 2
rethrowerror = parserethrow(args[1])
expr = args[2]
else
expr = args[1]
end

pkg isa Symbol &&
return Expr(:macrocall, Symbol("@warn"), __source__,
"Requires now needs a UUID; please see the readme for changes in 0.7.")
Expand All @@ -63,7 +81,7 @@ macro require(pkg, expr)
if !isprecompiling()
listenpkg($pkg) do
withpath($(string(__source__.file))) do
err($__module__, $modname) do
err($__module__, $modname, $rethrowerror) do
$(esc(:(eval($(Expr(:quote, Expr(:block,
:(const $(Symbol(modname)) = Base.require($pkg)),
expr)))))))
Expand Down