Browse Source

Make AssertionError more informative by printing the expression

pull/13/head
Fredrik Ekre 2 years ago
parent
commit
864b46a819
  1. 13
      src/Prometheus.jl
  2. 8
      test/runtests.jl

13
src/Prometheus.jl

@ -20,16 +20,19 @@ function Base.showerror(io::IO, err::ArgumentError)
print(io, "Prometheus.", nameof(typeof(err)), ": ", err.msg) print(io, "Prometheus.", nameof(typeof(err)), ": ", err.msg)
end end
struct AssertionError <: PrometheusException end struct AssertionError <: PrometheusException
msg::String
end
macro assert(cond) macro assert(cond)
return :($(esc(cond)) || throw(AssertionError())) msg = string(cond)
return :($(esc(cond)) || throw(AssertionError($msg)))
end end
function Base.showerror(io::IO, ::AssertionError) function Base.showerror(io::IO, err::AssertionError)
print( print(
io, io,
"Prometheus.AssertionError: this is unexpected. Please file an issue at " * "Prometheus.AssertionError: `", err.msg, "`. This is unexpected, please file an " *
"https://github.com/fredrikekre/Prometheus.jl/issues/new.", "issue at https://github.com/fredrikekre/Prometheus.jl/issues/new.",
) )
end end

8
test/runtests.jl

@ -770,8 +770,12 @@ end
end end
@testset "Utilities" begin @testset "Utilities" begin
@test_throws Prometheus.AssertionError Prometheus.@assert false x = 1
@test occursin("Please file an issue", sprint(showerror, Prometheus.AssertionError())) err = try Prometheus.@assert x === nothing; catch e; e; end
@test err isa Prometheus.AssertionError
@test err.msg == "x === nothing"
@test occursin("`x === nothing`", sprint(showerror, err))
@test occursin("please file an issue", sprint(showerror, err))
@test occursin( @test occursin(
"Prometheus.ArgumentError: err", "Prometheus.ArgumentError: err",
sprint(showerror, Prometheus.ArgumentError("err")), sprint(showerror, Prometheus.ArgumentError("err")),

Loading…
Cancel
Save