prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1#pragma once
2
14
15#include <cstdint>
16#include <expected>
17#include <stdexcept>
18#include <string>
19#include <string_view>
20#include <utility>
21
22namespace prom {
23
34
36[[nodiscard]] constexpr std::string_view to_string(const ErrorCode code) noexcept {
37 switch (code) {
39 return "invalid metric name";
41 return "invalid label name";
43 return "empty help text";
45 return "invalid histogram buckets";
47 return "invalid summary quantiles";
49 return "empty state set";
51 return "metric registration failed";
52 }
53 return "unknown error";
54}
55
57struct Error {
59 std::string message;
60
61 bool operator==(const Error&) const = default;
62};
63
65template <class T>
66using expected = std::expected<T, Error>;
67
69class Exception : public std::runtime_error {
70public:
72 : std::runtime_error(std::string(to_string(error.code)) + ": " + error.message),
73 error_(std::move(error)) {}
74
76 [[nodiscard]] const Error& error() const noexcept {
77 return error_;
78 }
79
80private:
81 Error error_;
82};
83
84} // namespace prom
Thrown by the throwing Registry factories when a spec fails validation.
Definition error.hpp:69
Exception(Error error)
Definition error.hpp:71
const Error & error() const noexcept
The structured error that triggered this exception.
Definition error.hpp:76
Definition adapter.hpp:24
ErrorCode
Why a metric definition or registration was rejected.
Definition error.hpp:25
@ EmptyStateSet
A state set was declared with no states.
@ InvalidLabelName
A label name is invalid or uses the reserved __ prefix.
@ InvalidMetricName
Name violates [a-zA-Z_][a-zA-Z0-9_]*.
@ EmptyHelp
Help text is required but was empty.
@ RegistrationFailed
The backend refused to register the metric.
@ InvalidQuantiles
Summary quantiles fall outside the open interval (0, 1).
@ InvalidBuckets
Histogram buckets are unsorted, empty, or non-finite.
constexpr std::string_view to_string(const ErrorCode code) noexcept
Human-readable spelling of an ErrorCode.
Definition error.hpp:36
std::expected< T, Error > expected
std::expected<T, Error> — the result type of the Registry::try_* family.
Definition error.hpp:66
A validation failure: a machine code plus a human message.
Definition error.hpp:57
std::string message
Context (e.g. the offending name).
Definition error.hpp:59
ErrorCode code
Machine-readable reason.
Definition error.hpp:58
bool operator==(const Error &) const =default