Skip to content

Conversation

downfa11
Copy link

@downfa11 downfa11 commented Sep 20, 2024

KafkaException(const char* filename, std::size_t lineno, const Error& error)
        : _when(std::chrono::system_clock::now()),
          _filename(filename),
          _lineno(lineno),
          _error(std::make_shared<Error>(error))
    {}

In the existing method, filename and lineno are expressed as follows, but in C++20, you can use the source_location feature.

KafkaException(const std::source_location location = std::source_location::current(), const Error& error)
        : _when(std::chrono::system_clock::now()),
          _filename(location.file_name()),
          _lineno(location.line()),
          _error(std::make_shared<Error>(error))
    {}


It may not be very significant, but I believe it is meaningful as it aligns with modern C++ syntax for this project.

@alex-q-chen sorry. I'm still learning and made some mistakes

@downfa11 downfa11 marked this pull request as draft September 23, 2024 12:25
@downfa11 downfa11 marked this pull request as ready for review September 23, 2024 12:26
@downfa11 downfa11 marked this pull request as draft September 23, 2024 12:27
@downfa11 downfa11 marked this pull request as ready for review September 23, 2024 12:28
@downfa11 downfa11 marked this pull request as draft September 23, 2024 12:32
@downfa11 downfa11 marked this pull request as ready for review September 23, 2024 12:37
@downfa11
Copy link
Author

@mimiflynn could you please review this?

{
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants