Hyper API for C++  0.0.17002
Hyper client library for C++ applications
HyperException.hpp
Go to the documentation of this file.
1 
5 #ifndef TABLEAU_HYPER_HYPEREXCEPTION_HPP
6 #define TABLEAU_HYPER_HYPEREXCEPTION_HPP
7 
8 #include <hyperapi/impl/infra.hpp>
9 #include <hyperapi/optional.hpp>
10 #include <hyperapi/hyperapi.h>
11 
12 #include <cassert>
13 #include <memory>
14 #include <string>
15 #include <system_error>
16 
17 namespace hyperapi {
18 
24 class ContextId final {
25  const uint32_t value;
26 
27  public:
28  constexpr explicit ContextId(uint32_t value) noexcept
29  : value(value) {
30  }
31  constexpr uint32_t getValue() const noexcept { return value; }
32 };
33 
34 namespace internal {
35 HyperException makeHyperException(const std::string&, const std::string&, ContextId);
36 }
37 
41 class HyperException final : public std::exception {
42  public:
44  HyperException& operator=(const HyperException& other) noexcept;
46  HyperException(const HyperException& other) noexcept;
48  HyperException& operator=(HyperException&& other) = default;
50  HyperException(HyperException&& other) = default;
51 
55  const char* getMainMessage() const noexcept { return getStringField(HYPER_ERROR_FIELD_MESSAGE); }
56 
60  const char* getHint() const noexcept { return getStringField(HYPER_ERROR_FIELD_HINT_MESSAGE); }
61 
67  HYPER_DEPRECATED("Use getMainMessage() instead. This function will be removed in the future.")
68  const char*
69  getMessage() const noexcept {
70  return getMainMessage();
71  }
72 
78  HYPER_DEPRECATED("Use getHint() instead. This function will be removed in the future.")
79  const char*
80  getHintMessage() const noexcept {
81  return getHint();
82  }
83 
89  HYPER_DEPRECATED("Use getCause().has_value() instead. This function will be removed in the future.")
90  bool hasCause() const noexcept { return (getCauseHandle() != nullptr); }
91 
95  optional<HyperException> getCause() const noexcept;
96 
100  ContextId getContextId() const noexcept { return ContextId(getUIntField(HYPER_ERROR_FIELD_CONTEXT_ID)); }
101 
105  std::string toString() const;
106 
110  const char* what() const noexcept override;
111 
112  private:
120  explicit HyperException(hyper_error_t* error);
121 
129  explicit HyperException(hyper_error_t* error, std::shared_ptr<const hyper_error_t> top_level_error);
130 
136  const char* getStringField(hyper_error_field_key key) const noexcept;
137 
143  int getIntField(hyper_error_field_key key) const noexcept;
144 
150  uint32_t getUIntField(hyper_error_field_key key) const noexcept;
151 
155  hyper_error_t* getCauseHandle() const noexcept;
156 
158  std::shared_ptr<const hyper_error_t> m_error;
159 
161  mutable std::string stringRepresentation;
162 
163  friend const char* internal::getSqlState(const hyperapi::HyperException&);
164  friend const char* internal::getDetail(const hyperapi::HyperException&);
165  friend int internal::getErrorCode(const hyperapi::HyperException&);
166  friend int internal::getErrorCategory(const hyperapi::HyperException&);
167  friend hyper_error_source_t internal::getErrorSource(const hyperapi::HyperException&);
168  friend int internal::getErrorGrpcStatusCode(const hyperapi::HyperException&);
169  friend HyperException internal::makeHyperException(const std::string&, const std::string&, ContextId);
170  friend HyperException internal::makeHyperException(hyper_error_t*);
171  friend class HyperExceptionTest;
172 };
173 }
174 
175 #include <hyperapi/impl/HyperException.impl.hpp>
176 
177 #endif
hyperapi::HyperException::getMainMessage
const char * getMainMessage() const noexcept
Returns the message stored in the exception object, or "" if there is none.
Definition: HyperException.hpp:55
hyperapi::HyperException::HYPER_DEPRECATED
HYPER_DEPRECATED("Use getCause().has_value() instead. This function will be removed in the future.") bool hasCause() const noexcept
Returns whether this error has an underlying cause stored.
Definition: HyperException.hpp:89
hyperapi::HyperException::toString
std::string toString() const
Returns a formatted string containing the message and hint of the error and all causes.
hyperapi::ContextId
A context id.
Definition: HyperException.hpp:24
hyperapi::HyperException::getMessage
const char * getMessage() const noexcept
Returns the message stored in the exception object, or "" if there is none.
Definition: HyperException.hpp:69
hyperapi::HyperException::what
const char * what() const noexcept override
Returns a null terminated character sequence that may be used to identify the exception.
hyperapi::optional
Surrogate for C++17 std::optional
Definition: optional.hpp:40
hyperapi::HyperException
Defines an exception object that is thrown on failure by the functions in the Hyper API C++ library.
Definition: HyperException.hpp:41
hyperapi::HyperException::getContextId
ContextId getContextId() const noexcept
Returns the context id.
Definition: HyperException.hpp:100
hyperapi::HyperException::operator=
HyperException & operator=(const HyperException &other) noexcept
Copy assignment operator.
hyperapi
The primary namespace of the Hyper API for C++.
Definition: ByteSpan.hpp:15
hyperapi::HyperException::getHintMessage
const char * getHintMessage() const noexcept
Returns the hint message stored in the exception object, or "" if there is none.
Definition: HyperException.hpp:80
hyperapi::HyperException::getHint
const char * getHint() const noexcept
Returns the hint message stored in the exception object, or "" if there is none.
Definition: HyperException.hpp:60
hyperapi::HyperException::HyperException
HyperException(const HyperException &other) noexcept
Copy constructor.
hyperapi::HyperException::getCause
optional< HyperException > getCause() const noexcept
Returns the cause stored in the exception object.
optional.hpp