Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/kafka/KafkaException.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <chrono>
#include <exception>
#include <string>
#include <source_location>


namespace KAFKA_API {
Expand All @@ -21,10 +22,10 @@ namespace KAFKA_API {
class KafkaException: public std::exception
{
public:
KafkaException(const char* filename, std::size_t lineno, const Error& error)
KafkaException(const std::source_location location = std::source_location::current(), const Error& error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the std::source_location is quit an elegant choice, but it's not supported until c++20. (the project has to be compatible with c++14/17)
  • default parameter values must be specified starting from the rightmost parameter and moving leftwards
  • if we're using std::source_location as the default parameter, we could get rid of the macro KAFKA THROW_ERROR as well -- just throw KafkaException(error) at where we need it.

Copy link
Author

@downfa11 downfa11 Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

As far as I know, source_location was originally provided in Boost before being adopted into C++20.

Since project already depends on Boost (e.g., for boost::optional), using boost::source_location will not introduce any new external dependencies.

I also plan to update the code so that exceptions can be thrown directly via KafkaException(error) and remove the KAFKA_THROW_ERROR macro in the process.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the boost::source_location solution is certainly elegant, I'm wondering if introducing a new Boost dependency might be a concern for the project.

I'd like to make sure we aren't adding complexity where we're trying to remove it.

: _when(std::chrono::system_clock::now()),
_filename(filename),
_lineno(lineno),
_filename(location.file_name()),
_lineno(location.line()),
_error(std::make_shared<Error>(error))
{}

Expand Down Expand Up @@ -53,7 +54,7 @@ class KafkaException: public std::exception
};


#define KAFKA_THROW_ERROR(error) throw KafkaException(__FILE__, __LINE__, error)
#define KAFKA_THROW_ERROR(error) throw KafkaException(error)
#define KAFKA_THROW_IF_WITH_ERROR(error) if (error) KAFKA_THROW_ERROR(error)

} // end of KAFKA_API
Expand Down