refactor(eventsub): enforce static error categories (#5903)

This commit is contained in:
nerix
2025-02-07 16:37:53 +01:00
committed by GitHub
parent 9260e72a0b
commit f37676fe1c
20 changed files with 374 additions and 1049 deletions
@@ -0,0 +1,9 @@
#pragma once
#include "twitch-eventsub-ws/errors.hpp"
#define EVENTSUB_BAIL_HERE(kind) \
{ \
static constexpr boost::source_location loc = BOOST_CURRENT_LOCATION; \
return ::chatterino::eventsub::lib::error::makeCode(kind, &loc); \
}
@@ -6,32 +6,35 @@
namespace chatterino::eventsub::lib::error {
// NOLINTNEXTLINE(performance-enum-size)
enum class Kind : int {
FieldMissing = 1,
ExpectedObject,
ExpectedString,
UnknownEnumValue,
InnerRootMissing,
NoMessageHandler
};
class ApplicationErrorCategory final : public boost::system::error_category
{
const std::string innerMessage;
public:
ApplicationErrorCategory(const char *_innerMessage)
: innerMessage(_innerMessage)
{
}
explicit ApplicationErrorCategory(std::string _innerMessage)
: innerMessage(std::move(_innerMessage))
{
}
consteval ApplicationErrorCategory() = default;
const char *name() const noexcept override
{
return "Application JSON error";
}
std::string message(int /*ev*/) const override
{
return this->innerMessage;
return "JSON deserialization error";
}
std::string message(int ev) const override;
};
const ApplicationErrorCategory EXPECTED_OBJECT{"Expected object"};
const ApplicationErrorCategory MISSING_KEY{"Missing key"};
inline constexpr ApplicationErrorCategory CATEGORY;
inline boost::system::error_code makeCode(Kind kind,
const boost::source_location *loc)
{
return {static_cast<int>(kind), CATEGORY, loc};
}
} // namespace chatterino::eventsub::lib::error