NCBI C++ ToolKit
exception.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: exception.cpp 79822 2017-10-16 17:40:32Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * Standard serialization exceptions
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <serial/exception.hpp>
34 #include <serial/impl/choice.hpp>
35 #include <serial/serialbase.hpp>
36 
38 
40 {
42 
43  const CSerialException& other = dynamic_cast<const CSerialException&>(src);
44  m_FrameStack = other.m_FrameStack;
45 }
46 
47 void CSerialException::AddFrameInfo(string frame_info)
48 {
49  if (!frame_info.empty() && !m_FrameStack.empty()) {
50  m_FrameStack.insert(0,1,'.');
51  }
52  m_FrameStack = frame_info + m_FrameStack;
53 }
54 
55 void CSerialException::ReportExtra(ostream& out) const
56 {
57  if ( !m_FrameStack.empty() ) {
58  out << " at " << m_FrameStack;
59  }
60 }
61 
63 {
64  switch ( GetErrCode() ) {
65  case eFail: return "eFail";
66  default: return CException::GetErrCodeString();
67  }
68 }
69 
71  size_t index, const char* const names[], size_t namesCount)
72 {
73  if ( index > namesCount )
74  return "?unknown?";
75  return names[index];
76 
77 }
78 
80 
82 {
85 };
87 {
89  SIZE_TYPE e_pos = NPOS;
90  if ( size > 2 && NStr::EndsWith(s.m_String, ".E") ) {
91  e_pos = s.m_String.rfind('.', size-3);
92  if ( e_pos != NPOS ) {
93  size -= 2;
94  }
95  }
96  bool capitalize = true;
97  for ( SIZE_TYPE i = 0; i < size; ++i ) {
98  char c = s.m_String[i];
99  if ( c == '.' ) {
100  out << "::C_";
101  if ( i == e_pos ) {
102  out << "E_";
103  }
104  capitalize = true;
105  }
106  else {
107  if ( c == '-' ) {
108  c = '_';
109  }
110  if ( capitalize ) {
111  c = (char)toupper((unsigned char)c);
112  capitalize = false;
113  }
114  out << c;
115  }
116  }
117  return out;
118 }
119 
121 
123  const CDiagCompileInfo& diag_info,
124  const CSerialObject* object,
125  size_t currentIndex, size_t mustBeIndex,
126  const char* const names[], size_t namesCount,
127  EDiagSev severity)
128  : CSerialException(diag_info, 0, "", severity, 0)
129 {
130  CNcbiOstrstream msg;
131  const CChoiceTypeInfo* type = 0;
132  if ( object ) {
133  type = dynamic_cast<const CChoiceTypeInfo*>(object->GetThisTypeInfo());
134  }
135  const char* cur_name = GetName(currentIndex, names, namesCount);
136  const char* req_name = GetName(mustBeIndex, names, namesCount);
137  if ( type ) {
138  msg << "C" << SPrintIdentifier(type->GetAccessName())
139  << "::Get" << SPrintIdentifier(req_name) << "()";
140  msg << ": Invalid choice selection: "
141  << type->GetAccessModuleName() <<"::"<< type->GetAccessName()
142  << '.' << cur_name;
143  }
144  else {
145  msg << "Invalid choice selection: "
146  << cur_name << ". Expected: " << req_name;
147  }
148  x_Init(diag_info, CNcbiOstrstreamToString(msg), 0, severity);
150 }
151 
153  const CDiagCompileInfo& diag_info,
154  size_t currentIndex, size_t mustBeIndex,
155  const char* const names[], size_t namesCount,
156  EDiagSev severity)
157  : CSerialException(diag_info, 0, "", severity, 0)
158 {
159  x_Init(diag_info,
160  string("Invalid choice selection: ")+
161  GetName(currentIndex, names, namesCount)+". "
162  "Expected: "+
163  GetName(mustBeIndex, names, namesCount),0, severity);
165 }
166 
168  const char* file, int line,
169  size_t currentIndex, size_t mustBeIndex,
170  const char* const names[], size_t namesCount,
171  EDiagSev severity)
172  : CSerialException(CDiagCompileInfo(file, line), 0, "", severity, 0)
173 {
175  string("Invalid choice selection: ")+
176  GetName(currentIndex, names, namesCount)+". "
177  "Expected: "+
178  GetName(mustBeIndex, names, namesCount),0, severity);
180 }
181 
183  size_t currentIndex, size_t mustBeIndex,
184  const char* const names[], size_t namesCount,
185  EDiagSev severity)
186  : CSerialException(CDiagCompileInfo("unknown", 0), 0, "", severity, 0)
187 {
188  x_Init(CDiagCompileInfo("unknown", 0),
189  string("Invalid choice selection: ")+
190  GetName(currentIndex, names, namesCount)+". "
191  "Expected: "+
192  GetName(mustBeIndex, names, namesCount),0, severity);
194 }
195 
197  const CInvalidChoiceSelection& other)
198  : CSerialException(other)
199 {
200  x_Assign(other);
201 }
202 
204 {
205 }
206 
207 const char* CInvalidChoiceSelection::GetType(void) const
208 {
209  return "CInvalidChoiceSelection";
210 }
211 
213 {
214  return typeid(*this) == typeid(CInvalidChoiceSelection) ?
217 }
218 
220 {
221 }
222 
224 {
225  return new CInvalidChoiceSelection(*this);
226 }
227 
228 const char* CSerialException::GetErrCodeString(void) const
229 {
230  switch ( GetErrCode() ) {
231  case eNotImplemented: return "eNotImplemented";
232  case eEOF: return "eEOF";
233  case eIoError: return "eIoError";
234  case eFormatError: return "eFormatError";
235  case eOverflow: return "eOverflow";
236  case eInvalidData: return "eInvalidData";
237  case eIllegalCall: return "eIllegalCall";
238  case eFail: return "eFail";
239  case eNotOpen: return "eNotOpen";
240  case eMissingValue: return "eMissingValue";
241  case eNullValue: return "eNullValue";
242  default: return CException::GetErrCodeString();
243  }
244 }
245 
246 const char* CUnassignedMember::GetErrCodeString(void) const
247 {
248 #if 0
249  switch ( GetErrCode() ) {
250  case eGet: return "eGet";
251  case eWrite: return "eWrite";
252  case eUnknownMember: return "eUnknownMember";
253  default: return CException::GetErrCodeString();
254  }
255 #else
256  // At least with ICC 9.0 on 64-bit Linux in Debug and MT mode
257  // there is an apparent bug that causes the above "switch" based
258  // variant of this function to misbehave and crash with SEGV...
259  TErrCode e = GetErrCode();
260  if (e == eGet) {return "eGet";}
261  if (e == eWrite) {return "eWrite";}
262  if (e == eUnknownMember) {return "eUnknownMember";}
264 #endif
265 }
266 
268 {
269  switch ( GetErrCode() ) {
270  case eMinLength: return "eMinLength";
271  case eMaxLength: return "eMaxLength";
272  case eLength: return "eLength";
273  case ePattern: return "ePattern";
274  case eInclusiveMinimum: return "eInclusiveMinimum";
275  case eExclusiveMinimum: return "eExclusiveMinimum";
276  case eInclusiveMaximum: return "eInclusiveMaximum";
277  case eExclusiveMaximum: return "eExclusiveMaximum";
278  case eMultipleOf: return "eMultipleOf";
279  case eMinItems: return "eMinItems";
280  case eMaxItems: return "eMaxItems";
281  case eUniqueItems: return "eUniqueItems";
282  default: return CException::GetErrCodeString();
283  }
284 };
285 
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
Thrown on an attempt to access wrong choice variant.
Definition: exception.hpp:102
CNcbiOstrstreamToString class helps convert CNcbiOstrstream to a string Sample usage:
Definition: ncbistre.hpp:802
Root class for all serialization exceptions.
Definition: exception.hpp:50
Base class for all serializable objects.
Definition: serialbase.hpp:150
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
std::ofstream out("events_result.xml")
main entry point for tests
static const struct name_t names[]
ostream & operator<<(ostream &os, const CDB_Exception::SContext &ctx)
Definition: exception.cpp:75
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
virtual int x_GetErrCode(void) const
Helper method for getting error code.
Definition: ncbiexpt.hpp:1105
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
int TErrCode
Definition: ncbiexpt.hpp:889
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
@ eInvalid
To be used ONLY as a return value; please, NEVER throw an exception with this code.
Definition: ncbiexpt.hpp:885
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: exception.cpp:228
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: exception.cpp:267
virtual const char * GetType(void) const override
Get class name as a string.
Definition: exception.cpp:207
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: exception.cpp:62
virtual void x_Assign(const CException &src) override
Helper method for copying exception data.
Definition: exception.cpp:39
static const char * GetName(size_t index, const char *const names[], size_t namesCount)
Definition: exception.cpp:70
virtual const CException * x_Clone(void) const override
Helper method for cloning the exception.
Definition: exception.cpp:223
string m_FrameStack
Definition: exception.hpp:78
virtual void ReportExtra(ostream &out) const override
Report "non-standard" attributes.
Definition: exception.cpp:55
void AddFrameInfo(string frame_info)
Definition: exception.cpp:47
virtual ~CInvalidChoiceSelection(void) noexcept
Definition: exception.cpp:203
TErrCode GetErrCode(void) const
Definition: exception.cpp:212
virtual const char * GetErrCodeString(void) const override
Get error code interpreted as text.
Definition: exception.cpp:246
@ eFormatError
Malformed input data.
Definition: exception.hpp:57
@ eIoError
An unknown error during serialization.
Definition: exception.hpp:56
@ eInvalidData
Data is incorrect.
Definition: exception.hpp:59
@ eMissingValue
Mandatory value was missing in the input.
Definition: exception.hpp:63
@ eOverflow
Data is beyond the allowed limits.
Definition: exception.hpp:58
@ eNotOpen
No input or output file.
Definition: exception.hpp:62
@ eNullValue
Data value is null.
Definition: exception.hpp:64
@ eFail
Internal error, the real reason is unclear.
Definition: exception.hpp:61
@ eEOF
Unexpected end-of-file.
Definition: exception.hpp:55
@ eNotImplemented
Attempt to use unimplemented funtionality.
Definition: exception.hpp:54
@ eIllegalCall
Illegal in a given context function call.
Definition: exception.hpp:60
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
size_type rfind(const CTempString match, size_type pos=npos) const
Find the first instance of the entire matching string within the current string in a backward directi...
Definition: tempstr.hpp:705
static bool EndsWith(const CTempString str, const CTempString end, ECase use_case=eCase)
Check if a string ends with a specified suffix value.
Definition: ncbistr.hpp:5430
#define NPOS
Definition: ncbistr.hpp:133
size_type size(void) const
Return the length of the represented array.
Definition: tempstr.hpp:327
FILE * file
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
int toupper(Uchar c)
Definition: ncbictype.hpp:73
BEGIN_LOCAL_NAMESPACE
Definition: exception.cpp:79
END_LOCAL_NAMESPACE
Definition: exception.cpp:120
CTempString m_String
Definition: exception.cpp:84
SPrintIdentifier(const CTempString &s)
Definition: exception.cpp:83
Definition: type.c:6
Modified on Tue Apr 16 20:08:55 2024 by modify_doxy.py rev. 669887