Skip to content

Commit

Permalink
kafka: Move make_error_code(kafka::error_code)
Browse files Browse the repository at this point in the history
Move it from the pandaproxy project to kafka.

There are two ways to map to an `error_condition`
1) Override `error_category::default_error_condition`
2) Override `error_category::equivalent`

1) requires a global error_condition, but `reply_error_code`
   is specific to pandaproxy, and the dependency works the
   wrong way.
2) Could work, but only for comparing a `std::error_code` with
   a reply_error_code-based error_condition, not for converting
   it

To break the dependency, overload `make_error_condition` that
takes a `std::error_code`, and map from kafka::error_code to
reply_error_condition as before.

Signed-off-by: Ben Pope <ben@redpanda.com>
  • Loading branch information
BenPope committed May 12, 2022
1 parent ed76668 commit 3c044bc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
14 changes: 14 additions & 0 deletions src/v/kafka/protocol/errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,18 @@ std::ostream& operator<<(std::ostream& o, error_code code) {
return o;
}

struct error_category final : std::error_category {
const char* name() const noexcept override { return "kafka"; }
std::string message(int ec) const override {
return std::string(
kafka::error_code_to_str(static_cast<kafka::error_code>(ec)));
}
};

const error_category error_category{};

std::error_code make_error_code(kafka::error_code ec) {
return {static_cast<int>(ec), error_category};
}

} // namespace kafka
9 changes: 9 additions & 0 deletions src/v/kafka/protocol/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <cstdint>
#include <iosfwd>
#include <string_view>
#include <system_error>

namespace kafka {

Expand Down Expand Up @@ -224,5 +225,13 @@ enum class error_code : int16_t {

std::ostream& operator<<(std::ostream&, error_code);
std::string_view error_code_to_str(error_code error);
std::error_code make_error_code(error_code);

} // namespace kafka

namespace std {

template<>
struct is_error_code_enum<kafka::error_code> : true_type {};

} // namespace std
28 changes: 7 additions & 21 deletions src/v/pandaproxy/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,14 @@ struct reply_error_category final : std::error_category {

const reply_error_category reply_error_category{};

struct kafka_error_category final : std::error_category {
const char* name() const noexcept override { return "kafka"; }
std::string message(int ec) const override {
return std::string(
kafka::error_code_to_str(static_cast<kafka::error_code>(ec)));
}
std::error_condition
default_error_condition(int ec) const noexcept override {
}; // namespace

std::error_condition make_error_condition(std::error_code ec) {
if (ec.category() == make_error_code(kafka::error_code::none).category()) {
using rec = reply_error_code;
using kec = kafka::error_code;

switch (static_cast<kec>(ec)) {
switch (static_cast<kec>(ec.value())) {
case kec::none:
return {};
case kec::offset_out_of_range:
Expand Down Expand Up @@ -191,11 +187,8 @@ struct kafka_error_category final : std::error_category {
}
return {}; // keep gcc happy
}
};

const kafka_error_category kafka_error_category{};

}; // namespace
return ec.default_error_condition();
}

std::error_condition make_error_condition(reply_error_code ec) {
return {static_cast<int>(ec), reply_error_category};
Expand All @@ -206,10 +199,3 @@ const std::error_category& reply_category() noexcept {
}

} // namespace pandaproxy

namespace kafka {
std::error_code make_error_code(kafka::error_code ec) {
return {static_cast<int>(ec), pandaproxy::kafka_error_category};
}

} // namespace kafka
10 changes: 1 addition & 9 deletions src/v/pandaproxy/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,14 @@ enum class reply_error_code : uint16_t {
};

std::error_condition make_error_condition(reply_error_code);
std::error_condition make_error_condition(std::error_code ec);
const std::error_category& reply_category() noexcept;

} // namespace pandaproxy

namespace kafka {

std::error_code make_error_code(error_code);

}

namespace std {

template<>
struct is_error_condition_enum<pandaproxy::reply_error_code> : true_type {};

template<>
struct is_error_code_enum<kafka::error_code> : true_type {};

} // namespace std
2 changes: 1 addition & 1 deletion src/v/pandaproxy/reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ errored_body(std::error_condition ec, ss::sstring msg) {

inline std::unique_ptr<ss::httpd::reply>
errored_body(std::error_code ec, ss::sstring msg) {
return errored_body(ec.default_error_condition(), std::move(msg));
return errored_body(make_error_condition(ec), std::move(msg));
}

inline std::unique_ptr<ss::httpd::reply> unprocessable_entity(ss::sstring msg) {
Expand Down

0 comments on commit 3c044bc

Please sign in to comment.