diff --git a/src/Prometheus.jl b/src/Prometheus.jl index 9ed1115..fb2e9c5 100644 --- a/src/Prometheus.jl +++ b/src/Prometheus.jl @@ -20,16 +20,19 @@ function Base.showerror(io::IO, err::ArgumentError) print(io, "Prometheus.", nameof(typeof(err)), ": ", err.msg) end -struct AssertionError <: PrometheusException end +struct AssertionError <: PrometheusException + msg::String +end macro assert(cond) - return :($(esc(cond)) || throw(AssertionError())) + msg = string(cond) + return :($(esc(cond)) || throw(AssertionError($msg))) end -function Base.showerror(io::IO, ::AssertionError) +function Base.showerror(io::IO, err::AssertionError) print( io, - "Prometheus.AssertionError: this is unexpected. Please file an issue at " * - "https://github.com/fredrikekre/Prometheus.jl/issues/new.", + "Prometheus.AssertionError: `", err.msg, "`. This is unexpected, please file an " * + "issue at https://github.com/fredrikekre/Prometheus.jl/issues/new.", ) end diff --git a/test/runtests.jl b/test/runtests.jl index 5b60872..3b69c06 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -770,8 +770,12 @@ end end @testset "Utilities" begin - @test_throws Prometheus.AssertionError Prometheus.@assert false - @test occursin("Please file an issue", sprint(showerror, Prometheus.AssertionError())) + x = 1 + 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( "Prometheus.ArgumentError: err", sprint(showerror, Prometheus.ArgumentError("err")),