refactor(eventsub): enforce static error categories (#5903)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user