Hi all,

Currently, the C++ client SDK exposes an enum error code (`Result`).
For detailed errors, the message could only be printed in logs. For
example, a producer creation looks like:

```c++
Producer producer;
if (auto result = client.createProducer(topic, producer); result != ResultOk) {
    std::cerr << "failed: " << result << "\n";
}
```

When a result is `AuthorizationError`, the output will always be
`AuthorizationError`. The true reason is only included in logs.

This is inconvenient for many parts. Some applications might disable
the built-in logs, and it's hard to collect these logs into other
places. So I'm going to add a series of v2 APIs, which have method
signature like:

```c++
struct Error {
    Result result; // the original error code
    std::string message; // the additionally error message in v2 APIs
    // TODO: it can be extended to have a map attached in future
};
using CreateProducerV2Callback =
std::function<void(std::variant<Error, Producer>)>;
```

```c++
class Client {
   void createProducerAsyncV2(const std::string& topic, const
ProducerConfiguration& conf,
                               CreateProducerV2Callback callback);
```

It's a modern C++ design to use `std::variant` because the result is
either a Producer or an error. It's even possible to write a pattern
matching style code [1] like:

```c++
std::visit(overloaded {
    [](Producer producer) { runSendLoop(std::move(producer)) },
    [](const Error& error) { handleError(error); },
}, client.createProducerV2(topic));
```

I drafted a PR [2] to add support v2 API when creating a producer, and
will support other v2 APIs later.

Feel free to share any thought.

[1] https://en.cppreference.com/cpp/utility/variant/visit2
[2] https://github.com/apache/pulsar-client-cpp/pull/579/changes

Thanks,
Yunze

Reply via email to