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
+1 -1
View File
@@ -17,7 +17,7 @@ def _generate_implementation(header_path: str, items: list[Struct | Enum]) -> st
doc = f"""// WARNING: This file is automatically generated. Any changes will be lost.
#include "{header_path}"
#include "twitch-eventsub-ws/chrono.hpp" // IWYU pragma: keep
#include "twitch-eventsub-ws/errors.hpp"
#include "twitch-eventsub-ws/detail/errors.hpp"
#include <boost/json.hpp>
"""
@@ -3,8 +3,7 @@ boost::json::result_for<{{enum.full_name}}, boost::json::value>::type tag_invoke
{
if (!jvRoot.is_string())
{
static const error::ApplicationErrorCategory errorMustBeString{"{{enum.full_name}} must be a string"};
return boost::system::error_code{129, errorMustBeString};
EVENTSUB_BAIL_HERE(error::Kind::ExpectedString);
}
std::string_view eString(jvRoot.get_string());
@@ -16,6 +15,5 @@ boost::json::result_for<{{enum.full_name}}, boost::json::value>::type tag_invoke
}
{%- endfor %}
static const error::ApplicationErrorCategory errorEnumNameDidNotExist{"{{enum.full_name}} did not have a constant that matched this string"};
return boost::system::error_code{129, errorEnumNameDidNotExist};
EVENTSUB_BAIL_HERE(error::Kind::UnknownEnumValue);
}
@@ -1,2 +1 @@
static const error::ApplicationErrorCategory error_missing_field_{{field.name}}{"Missing required key {{field.json_name}}"};
return boost::system::error_code{129, error_missing_field_{{field.name}}};
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
@@ -4,28 +4,24 @@ boost::json::result_for<{{struct.full_name}}, boost::json::value>::type tag_invo
{% if struct.inner_root %}
if (!jvRoot.is_object())
{
static const error::ApplicationErrorCategory errorMustBeObject{"{{struct.full_name}} must be an object"};
return boost::system::error_code{129, errorMustBeObject};
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &outerRoot = jvRoot.get_object();
const auto *jvInnerRoot = outerRoot.if_contains("{{struct.inner_root}}");
if (jvInnerRoot == nullptr)
{
static const error::ApplicationErrorCategory errorMissing{"{{struct.full_name}}'s key {{struct.inner_root}} is missing"};
return boost::system::error_code{129, errorMissing};
EVENTSUB_BAIL_HERE(error::Kind::InnerRootMissing);
}
if (!jvInnerRoot->is_object())
{
static const error::ApplicationErrorCategory errorMustBeObject{"{{struct.full_name}}'s {{struct.inner_root}} must be an object"};
return boost::system::error_code{129, errorMustBeObject};
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &root = jvInnerRoot->get_object();
{% else %}
if (!jvRoot.is_object())
{
static const error::ApplicationErrorCategory errorMustBeObject{"{{struct.full_name}} must be an object"};
return boost::system::error_code{129, errorMustBeObject};
EVENTSUB_BAIL_HERE(error::Kind::ExpectedObject);
}
const auto &root = jvRoot.get_object();
{% endif %}