Skip to content

Commit

Permalink
Add new AssertionError Exception type
Browse files Browse the repository at this point in the history
 * assert() and @Assert throw a more specific AssertionError type

 * the assertion error type has a field for the assertion expr and optional
   message

 * rework the assert macro to special case passing in booleans as the
   asserted expression
  • Loading branch information
jakebolewski committed Jan 12, 2015
1 parent a19cc58 commit dbb0fe1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
9 changes: 9 additions & 0 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ type DimensionMismatch <: Exception
end
DimensionMismatch() = DimensionMismatch("")

type AssertionError <: Exception
expr::Any
msg::AbstractString

AssertionError() = new()
AssertionError(ex) = new(ex)
AssertionError(ex, msg::AbstractString) = new(ex, msg)
end

# For passing constants through type inference
immutable Val{T}
end
Expand Down
30 changes: 19 additions & 11 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,25 @@ systemerror(p, b::Bool) = b ? throw(SystemError(string(p))) : nothing

## assertion functions and macros ##

assert(x) = x ? nothing : error("assertion failed")
macro assert(ex,msgs...)
msg = isempty(msgs) ? ex : msgs[1]
if !isempty(msgs) && isa(msg, Expr)
# message is an expression needing evaluating
msg = :(string("assertion failed: ", $(esc(msg))))
elseif isdefined(Base,:string)
msg = string("assertion failed: ", msg)
assert(x) = x ? nothing : throw(AssertionError())
macro assert(ex, msgs...)
if ex === true
:(nothing)
elseif isempty(msgs)
if isa(ex, Expr)
:($(esc(ex)) ? nothing : throw(AssertionError($(Expr(:quote,ex)))))
elseif ex === false
:(throw(AssertionError(false)))
else
:(throw(TypeError(:toplevel, "", Bool, $(esc(ex)))))
end
else
# string() might not be defined during bootstrap
msg = :(string("assertion failed: ", $(Expr(:quote,msg))))
if isa(ex, Expr)
:($(esc(ex)) ? nothing : throw(AssertionError($(Expr(:quote,ex)), $(esc(msgs[1])))))
elseif ex === false
:(throw(AssertionError(false, $(esc(msgs[1])))))
else
:(throw(TypeError(:toplevel, "", Bool, $(esc(ex)))))
end
end
:($(esc(ex)) ? $(nothing) : error($msg))
end
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export
ProcessExitedException,
SystemError,
TypeError,
AssertionError,

# Global constants and variables
ARGS,
Expand Down
10 changes: 10 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ showerror(io::IO, e::ErrorException) = print(io, e.msg)
showerror(io::IO, e::KeyError) = (print(io, "key not found: "); show(io, e.key))
showerror(io::IO, e::InterruptException) = print(io, "interrupt")

function showerror(io::IO, ex::AssertionError)
if !isdefined(ex, :expr)
print(io, "AssertionError: ")
elseif !isdefined(ex, :msg)
print(io, "AssertionError: $(ex.expr)")
else
print(io, "AssertionError: $(ex.expr), $(ex.msg)")
end
end

function showerror(io::IO, e::MethodError)
name = isgeneric(e.f) ? e.f.env.name : :anonymous
if isa(e.f, DataType)
Expand Down
3 changes: 2 additions & 1 deletion test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ close(server)
server = listen(port)
@async connect("localhost",port)
s1 = accept(server)
@test_throws ErrorException accept(server,s1)
#TODO: should not rely on assertions for error messages
@test_throws AssertionError accept(server,s1)
close(server)

@test_throws Base.UVError connect(".invalid",80)
Expand Down

0 comments on commit dbb0fe1

Please sign in to comment.