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) @@ -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

8
test/runtests.jl

@ -770,8 +770,12 @@ end @@ -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")),

Loading…
Cancel
Save