NCBI C++ ToolKit
cass_exception.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CASS_EXCEPTION__HPP
2 #define CASS_EXCEPTION__HPP
3 
4 /* $Id: cass_exception.hpp 99314 2023-03-08 19:42:54Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Dmitri Dmitrienko
30  *
31  * File Description:
32  *
33  * Exceptions used in the cassandra driver
34  *
35  */
36 
37 #include <cassandra.h>
38 #include <corelib/ncbistre.hpp>
39 #include <corelib/ncbistr.hpp>
40 #include <corelib/ncbiexpt.hpp>
41 
42 #include "IdCassScope.hpp"
43 
46 
47 
49 {
50  public:
51  enum EErrCode {
52  eUnknown = 2000,
66 
67  // Came from class EError (IdLogUtl.hpp)
73  };
74 
75  static CCassandraException s_ProduceException(CassFuture * future, CCassandraException::EErrCode error_code) {
76  const char *message;
77  size_t message_length;
78  CassError rc = cass_future_error_code(future);
79  cass_future_error_message(future, &message, &message_length);
80  string msg(message, message_length);
81  msg.append(": ").append(NStr::NumericToString(static_cast<int>(rc), 0, 16));
83  result.SetErrorCode(error_code);
84  return result;
85  }
86 
87  void SetErrorCode(EErrCode error_code)
88  {
89  x_InitErrCode(static_cast<CException::EErrCode>(error_code));
90  }
91 
92  const char* GetErrCodeString(void) const override
93  {
94  switch (GetErrCode()) {
95  case eUnknown: return "eUnknown";
96  case eRsrcFailed: return "eRsrcFailed";
97  case eFailedToConn: return "eFailedToConn";
98  case eConnTimeout: return "eConnTimeout";
99  case eQueryFailedRestartable: return "eQueryFailedRestartable";
100  case eQueryFailed: return "eQueryFailed";
101  case eBindFailed: return "eBindFailed";
102  case eQueryTimeout: return "eQueryTimeout";
103  case eFetchFailed: return "eFetchFailed";
104  case eExtraFetch: return "eExtraFetch";
105  case eConvFailed: return "eConvFailed";
106  case eMissData: return "eMissData";
107  case eInconsistentData: return "eInconsistentData";
108  case eNotFound: return "eNotFound";
109  case eSeqFailed: return "eSeqFailed";
110  case eFatal: return "eFatal";
111  case eGeneric: return "eGeneric";
112  case eMemory: return "eMemory";
113  case eUserCancelled: return "eUserCancelled";
114  default: return CException::GetErrCodeString();
115  }
116  }
117 
118  void SetOpTime(int64_t optimeMS)
119  {
120  m_OpTimeMs = optimeMS;
121  }
122 
123  int64_t GetOpTime(void) const
124  {
125  return m_OpTimeMs;
126  }
127 
128  string TimeoutMsg(void) const
129  {
130  return "Failed to perform query in " +
131  NStr::NumericToString(m_OpTimeMs) + "ms, timed out";
132  }
133 
135 
136  protected:
138  const string & message,
139  const CException * prev_exception,
140  EDiagSev severity) override
141  {
142  m_OpTimeMs = 0;
143  ERR_POST(Info << "CCassandraException: " << message);
144  CException::x_Init(info, message, prev_exception, severity);
145  }
146 
147  void x_Assign(const CException & src) override
148  {
149  const CCassandraException* _src = dynamic_cast<const CCassandraException*>(&src);
150  if (_src) {
151  m_OpTimeMs = _src->m_OpTimeMs;
152  }
154  }
155 
157 };
158 
159 
160 
161 #define RAISE_CASS_ERROR(errc, dberr, comm) \
162  do { \
163  string __msg = comm; \
164  string __c = NStr::NumericToString(static_cast<int>(errc), 0, 16); \
165  __msg.append(" Cassandra error - (code: " + __c); \
166  __msg.append(string(", description: '") + \
167  cass_error_desc(errc) + "')"); \
168  NCBI_THROW(CCassandraException, dberr, __msg.c_str()); \
169  } while (0)
170 
171 #define RAISE_CASS_FUT_ERROR(future, errc, comm) \
172  do { \
173  const char *__message; \
174  size_t __msglen; \
175  cass_future_error_message(future, &__message, &__msglen); \
176  CassError rc = cass_future_error_code(future); \
177  string __msg; \
178  __msg.assign(__message); \
179  __msg.append(string(": ") + \
180  NStr::NumericToString(static_cast<int>(rc), 0, 16)); \
181  if (comm.empty()) \
182  NCBI_THROW(CCassandraException, errc, __msg); \
183  else \
184  NCBI_THROW(CCassandraException, errc, __msg + ": " + comm); \
185  } while (0)
186 
187 #define RAISE_CASS_QRY_ERROR(future, comm) \
188  do { \
189  CassError rc = cass_future_error_code(future); \
190  if (rc == CASS_ERROR_SERVER_UNAVAILABLE || \
191  rc == CASS_ERROR_LIB_REQUEST_QUEUE_FULL || \
192  rc == CASS_ERROR_LIB_NO_HOSTS_AVAILABLE) \
193  RAISE_CASS_FUT_ERROR(future, eQueryFailedRestartable, comm); \
194  else if (rc == CASS_ERROR_LIB_REQUEST_TIMED_OUT || \
195  rc == CASS_ERROR_SERVER_WRITE_TIMEOUT || \
196  rc == CASS_ERROR_SERVER_READ_TIMEOUT) \
197  RAISE_CASS_FUT_ERROR(future, eQueryTimeout, comm); \
198  else \
199  RAISE_CASS_FUT_ERROR(future, eQueryFailed, comm); \
200  } while (0)
201 
202 #define RAISE_CASS_CONN_ERROR(future, comm) \
203  RAISE_CASS_FUT_ERROR(future, eFailedToConn, comm)
204 
205 #define RAISE_DB_ERROR(errc, comm) \
206  do { \
207  string ___msg = comm; \
208  NCBI_THROW(CCassandraException, errc, comm); \
209  } while(0)
210 
211 #define RAISE_DB_QRY_TIMEOUT(tspent, tmargin, msg) \
212  do { \
213  string ___msg = msg; \
214  if ((tmargin) != 0) \
215  ___msg.append(string(", timeout ") + \
216  NStr::NumericToString(tmargin) + \
217  "ms (spent: " + NStr::NumericToString(tspent) + \
218  "ms)"); \
219  NCBI_EXCEPTION_VAR(db_exception, CCassandraException, eQueryTimeout, ___msg); \
220  db_exception.SetOpTime( (tspent) ); \
221  NCBI_EXCEPTION_THROW(db_exception); \
222  } while(0)
223 
224 
225 
226 
228 
229 #endif
#define END_IDBLOB_SCOPE
Definition: IdCassScope.hpp:40
#define BEGIN_IDBLOB_SCOPE
Definition: IdCassScope.hpp:39
USING_NCBI_SCOPE
void SetOpTime(int64_t optimeMS)
NCBI_EXCEPTION_DEFAULT(CCassandraException, CException)
void x_Assign(const CException &src) override
Helper method for copying exception data.
const char * GetErrCodeString(void) const override
Get error code interpreted as text.
int64_t GetOpTime(void) const
string TimeoutMsg(void) const
void x_Init(const CDiagCompileInfo &info, const string &message, const CException *prev_exception, EDiagSev severity) override
Helper method for initializing exception data.
void SetErrorCode(EErrCode error_code)
static CCassandraException s_ProduceException(CassFuture *future, CCassandraException::EErrCode error_code)
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
The NCBI C++ standard methods for dealing with std::string.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
virtual void x_Init(const CDiagCompileInfo &info, const string &message, const CException *prev_exception, EDiagSev severity)
Helper method for initializing exception data.
Definition: ncbiexpt.cpp:509
virtual void x_InitErrCode(CException::EErrCode err_code)
Helper method for initializing error code.
Definition: ncbiexpt.cpp:567
virtual void x_Assign(const CException &src)
Helper method for copying exception data.
Definition: ncbiexpt.cpp:536
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
#define NCBI_EXCEPTION(exception_class, err_code, message)
Generic macro to make an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:695
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static MDB_envinfo info
Definition: mdb_load.c:37
Defines NCBI C++ exception handling.
NCBI C++ stream class wrappers for triggering between "new" and "old" C++ stream libraries.
signed __int64 int64_t
Definition: stdint.h:135
else result
Definition: token2.c:20
Modified on Sat Dec 02 09:21:54 2023 by modify_doxy.py rev. 669887