11 #include <curl/curl.h>
12 #include <nlohmann/json.hpp>
17 using json = nlohmann::json;
19 namespace pinecone::util
70 explicit constexpr
failure_reason(curl_result::error_type err) noexcept : _curl_err(err) {}
81 curl_result::error_type _curl_err;
83 using request_rejected = failure_reason<failure::request_rejected>;
91 : _response_code(code), _body(
std::move(body))
98 [[nodiscard]] constexpr
auto response_code() const noexcept -> int64_t {
return _response_code; }
103 [[nodiscard]] constexpr
auto body() const noexcept ->
std::
string const& {
return _body; }
110 [[nodiscard]]
auto api_error() const noexcept ->
std::variant<types::api_error, json::exception>
115 }
catch (json::exception
const& ex) {
121 int64_t _response_code;
124 using request_failed = failure_reason<failure::request_failed>;
134 explicit failure_reason(json::exception
const& ex) noexcept : _message(ex.what()) {}
135 explicit failure_reason(
char const* message) noexcept : _message(message) {}
140 [[nodiscard]] constexpr
auto message() const noexcept ->
char const* {
return _message; }
143 const char* _message;
145 using parsing_failed = failure_reason<failure::parsing_failed>;
156 template <
typename T>
158 using error_type = std::variant<request_rejected, request_failed, parsing_failed>;
159 using value_type = std::variant<T, error_type>;
161 result() noexcept =
default;
180 if (_value.index() == 0) {
181 return failure::none;
184 auto const& err = std::get<error_type>(_value);
185 switch (err.index()) {
187 return failure::request_rejected;
189 return failure::request_failed;
191 return failure::parsing_failed;
205 if (_value.index() == 0) {
210 auto const& err = std::get<error_type>(_value);
211 switch (err.index()) {
213 oss <<
"Request rejected. CURL error code: "
217 oss <<
"Request failed. HTTP response code: "
218 << std::get<request_failed>(err).response_code() <<
" "
219 << std::get<request_failed>(err).body();
222 oss <<
"Parsing failed. Exception message: " << std::get<parsing_failed>(err).message();
225 return "Unknown. This is a bug, please report it!";
236 return _value.index() == 0;
242 [[nodiscard]] constexpr
auto is_failed() const noexcept ->
bool {
return !is_successful(); }
251 template <
typename U>
255 return propagate<U>();
258 return func(std::get<T>(_value));
264 [[nodiscard]] constexpr
auto operator->() noexcept -> T* {
return &std::get<T>(_value); }
269 [[nodiscard]] constexpr
auto operator*() noexcept -> T& {
return std::get<T>(_value); }
274 [[nodiscard]] constexpr
auto value() noexcept -> value_type& {
return _value; }
277 template <
typename U>
278 [[nodiscard]] constexpr
auto propagate() noexcept ->
result<U>
280 return {
std::move(std::get<error_type>(_value))};
Wraps CURL results for type and exception safety.
Pinecone's custom error format.
failure
The possible ways in which a Pinecone API call can fail.
An error response returned by the Pinecone API.
Models the possibly of failure for remote HTTP operations.
auto to_string() const noexcept -> std::string
Retrieves the string representation of the current instance.
A request that succeeded on the remote server, but whose response could not be parsed.
constexpr auto message() const noexcept -> char const *
A request that was processed by the remote API, but failed during processing.
auto api_error() const noexcept -> std::variant< types::api_error, json::exception >
Attempts to parse the body as a structured API error.
constexpr auto body() const noexcept -> std::string const &
constexpr auto response_code() const noexcept -> int64_t
A request that was rejected by the remote API.
constexpr auto curl_error() const noexcept -> curl_result::error_type
Data associated with the possible failure modes for Cppinecone operations.
Models the possibility of failure for all Cppinecone public API operations.
constexpr auto is_successful() const noexcept -> bool
constexpr auto operator->() noexcept -> T *
Can be used only on successful results.
constexpr auto operator*() noexcept -> T &
Can be used only on successful results.
constexpr auto and_then(std::function< result< U >(T &)> const &func) noexcept -> result< U >
Runs a transformation function on a successful result; does nothing on a failed result.
constexpr auto value() noexcept -> value_type &
constexpr auto is_failed() const noexcept -> bool
constexpr auto failure_reason() const noexcept -> failure
auto to_string() const noexcept -> std::string
Retrieves a string representation of the operation result.