Browse Source

Remove unnecessary `UnreachableError` exception

pull/13/head
Fredrik Ekre 2 years ago
parent
commit
8e7b839c84
  1. 18
      src/Prometheus.jl
  2. 14
      test/runtests.jl

18
src/Prometheus.jl

@ -20,20 +20,16 @@ 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 UnreachableError <: PrometheusException end
unreachable() = throw(UnreachableError())
struct AssertionError <: PrometheusException end struct AssertionError <: PrometheusException end
macro assert(cond) macro assert(cond)
return :($(esc(cond)) || throw(AssertionError())) return :($(esc(cond)) || throw(AssertionError()))
end end
function Base.showerror(io::IO, err::Union{AssertionError, UnreachableError}) function Base.showerror(io::IO, ::AssertionError)
print( print(
io, io,
"Prometheus.", nameof(typeof(err)), "Prometheus.AssertionError: this is unexpected. Please file an issue at " *
": this is unexpected. Please file an issue at " * "https://github.com/fredrikekre/Prometheus.jl/issues/new.",
"https://github.com/fredrikekre/Prometheus.jl/issues/new",
) )
end end
@ -544,7 +540,6 @@ end
at_time(gauge::Gauge, v) = set(gauge, v) at_time(gauge::Gauge, v) = set(gauge, v)
at_time(summary::Summary, v) = observe(summary, v) at_time(summary::Summary, v) = observe(summary, v)
at_time(histogram::Histogram, v) = observe(histogram, v) at_time(histogram::Histogram, v) = observe(histogram, v)
at_time(::Collector, v) = unreachable()
""" """
Prometheus.@inprogress collector expr Prometheus.@inprogress collector expr
@ -578,8 +573,6 @@ end
at_inprogress_enter(gauge::Gauge) = inc(gauge) at_inprogress_enter(gauge::Gauge) = inc(gauge)
at_inprogress_exit(gauge::Gauge) = dec(gauge) at_inprogress_exit(gauge::Gauge) = dec(gauge)
at_inprogress_enter(::Collector) = unreachable()
at_inprogress_exit(::Collector) = unreachable()
function expr_gen(macroname, collector, code) function expr_gen(macroname, collector, code)
if macroname === :time if macroname === :time
@ -604,7 +597,7 @@ function expr_gen(macroname, collector, code)
Expr(:call, at_inprogress_exit, cllctr) Expr(:call, at_inprogress_exit, cllctr)
] ]
else else
unreachable() throw(ArgumentError("unknown macro name $(repr(macroname))"))
end end
local ret local ret
@gensym ret @gensym ret
@ -875,7 +868,6 @@ prometheus_type(::Type{Counter}) = "counter"
prometheus_type(::Type{Gauge}) = "gauge" prometheus_type(::Type{Gauge}) = "gauge"
prometheus_type(::Type{Histogram}) = "histogram" prometheus_type(::Type{Histogram}) = "histogram"
prometheus_type(::Type{Summary}) = "summary" prometheus_type(::Type{Summary}) = "summary"
prometheus_type(::Type) = unreachable()
function collect!(metrics::Vector, family::Family{C}) where C function collect!(metrics::Vector, family::Family{C}) where C
type = prometheus_type(C) type = prometheus_type(C)
@ -886,7 +878,7 @@ function collect!(metrics::Vector, family::Family{C}) where C
for (label_values, child) in family.children for (label_values, child) in family.children
# collect!(...) the child, throw away the metric, but keep the samples # collect!(...) the child, throw away the metric, but keep the samples
child_metrics = collect!(resize!(buf, 0), child) child_metrics = collect!(resize!(buf, 0), child)
length(child_metrics) != 1 && unreachable() # TODO: maybe this should be supported? @assert length(child_metrics) == 1 # TODO: maybe this should be supported?
child_metric = child_metrics[1] child_metric = child_metrics[1]
@assert(child_metric.type == type) @assert(child_metric.type == type)
# Unwrap and rewrap samples with the labels # Unwrap and rewrap samples with the labels

14
test/runtests.jl

@ -430,11 +430,10 @@ end
@test gauge.value == 0 @test gauge.value == 0
end end
# TODO: Document interface and test it
@testset "Custom collector with @time/@inprogress" begin @testset "Custom collector with @time/@inprogress" begin
struct Coll <: Prometheus.Collector end # struct Coll <: Prometheus.Collector end
@test_throws Prometheus.UnreachableError Prometheus.at_time(Coll(), 1.0) @test_throws Prometheus.ArgumentError Prometheus.expr_gen(:unknown, nothing, nothing)
@test_throws Prometheus.UnreachableError Prometheus.at_inprogress_enter(Coll())
@test_throws Prometheus.UnreachableError Prometheus.at_inprogress_exit(Coll())
end end
@testset "Prometheus.Family{$Collector}" for Collector in (Prometheus.Histogram, Prometheus.Summary) @testset "Prometheus.Family{$Collector}" for Collector in (Prometheus.Histogram, Prometheus.Summary)
@ -771,13 +770,10 @@ end
end end
@testset "Utilities" begin @testset "Utilities" begin
@test_throws Prometheus.UnreachableError Prometheus.unreachable() @test_throws Prometheus.AssertionError Prometheus.@assert false
@test_throws Prometheus.AssertionError Prometheus.@assert false @test occursin("Please file an issue", sprint(showerror, Prometheus.AssertionError()))
@test occursin("unexpected", sprint(showerror, Prometheus.UnreachableError()))
@test occursin("unexpected", sprint(showerror, Prometheus.AssertionError()))
@test occursin( @test occursin(
"Prometheus.ArgumentError: err", "Prometheus.ArgumentError: err",
sprint(showerror, Prometheus.ArgumentError("err")), sprint(showerror, Prometheus.ArgumentError("err")),
) )
@test_throws Prometheus.UnreachableError Prometheus.prometheus_type(Int)
end end

Loading…
Cancel
Save