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

Go to the SVN repository for this file.

1 /* $Id: ncbierror.cpp 85240 2019-01-28 16:27:18Z grichenk $
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:
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 #include <corelib/ncbithr.hpp>
32 #include <corelib/ncbierror.hpp>
33 #if defined(NCBI_OS_MSWIN)
34 # include <Winsock2.h>
35 #endif
36 
37 #define NCBI_USE_ERRCODE_X Corelib_Diag
38 
40 
41 
42 /////////////////////////////////////////////////////////////////////////////
43 
44 class CNcbiError_Int : public CNcbiError
45 {
46 public:
48  }
49 };
50 
51 static
53 
54 static
56 {
57  CNcbiError* e = s_Last.GetValue();
58  if (!e) {
59  s_Last.SetValue(e = new CNcbiError_Int(), CTlsBase::DefaultCleanup<CNcbiError>);
60  }
61  return e;
62 }
63 
64 /////////////////////////////////////////////////////////////////////////////
65 
67  : m_Code(eSuccess), m_Category(eGeneric), m_Native(0)
68 {
69 }
70 
71 
73 {
74  return *NcbiError_GetOrCreate();
75 }
76 
78 {
79 #if defined(NCBI_OS_MSWIN)
80  if (m_Code == eNotSet) {
81 // if (e->m_Category == eMsWindows)
82  switch (m_Native) {
83  case ERROR_ACCESS_DENIED: m_Code = ePermissionDenied; break;
84  case ERROR_ALREADY_EXISTS: m_Code = eFileExists; break;
85  case ERROR_BAD_ARGUMENTS: m_Code = eInvalidArgument; break;
86  case ERROR_BAD_EXE_FORMAT: m_Code = eExecutableFormatError; break;
87  case ERROR_BROKEN_PIPE: m_Code = eBrokenPipe; break;
88  case ERROR_DIR_NOT_EMPTY: m_Code = eDirectoryNotEmpty; break;
89  case ERROR_DISK_FULL: m_Code = eNoSpaceOnDevice; break;
90  case ERROR_FILE_EXISTS: m_Code = eFileExists; break;
91  case ERROR_FILE_NOT_FOUND: m_Code = eNoSuchFileOrDirectory; break;
92  case ERROR_NOT_ENOUGH_MEMORY: m_Code = eNotEnoughMemory; break;
93  case ERROR_NOT_SUPPORTED: m_Code = eNotSupported; break;
94  case ERROR_PATH_NOT_FOUND: m_Code = eNoSuchFileOrDirectory; break;
95  case ERROR_TOO_MANY_OPEN_FILES: m_Code = eTooManyFilesOpen; break;
96 
97  case WSAEADDRINUSE: m_Code = eAddressInUse; break; //EADDRINUSE
98  case WSAEADDRNOTAVAIL: m_Code = eAddressNotAvailable; break; //ADDRNOTAVAIL
99  case WSAEAFNOSUPPORT: m_Code = eAddressFamilyNotSupported; break; //EAFNOSUPPORT
100  case WSAEALREADY: m_Code = eConnectionAlreadyInProgress; break; //EALREADY
101  case WSAECONNABORTED: m_Code = eConnectionAborted; break; //ECONNABORTED
102  case WSAECONNREFUSED: m_Code = eConnectionRefused; break; //ECONNREFUSED
103  case WSAECONNRESET: m_Code = eConnectionReset; break; //ECONNRESET
104  case WSAEDESTADDRREQ: m_Code = eDestinationAddressRequired; break; //EDESTADDRREQ
105  case WSAEHOSTUNREACH: m_Code = eHostUnreachable; break; //EHOSTUNREACH
106  case WSAEINPROGRESS: m_Code = eOperationInProgress; break; //EINPROGRESS
107  case WSAEINTR: m_Code = eInterrupted; break; //EINTR
108  case WSAEISCONN: m_Code = eAlreadyConnected; break; //EISCONN
109  case WSAELOOP: m_Code = eTooManySymbolicLinkLevels; break; //ELOOP
110  case WSAEMSGSIZE: m_Code = eMessageSize; break; //EMSGSIZE
111  case WSAENAMETOOLONG: m_Code = eFilenameTooLong; break; //ENAMETOOLONG
112  case WSAENETDOWN: m_Code = eNetworkDown; break; //ENETDOWN
113  case WSAENETRESET: m_Code = eNetworkReset; break; //ENETRESET
114  case WSAENETUNREACH: m_Code = eNetworkUnreachable; break; //ENETUNREACH
115  case WSAENOBUFS: m_Code = eNoBufferSpace; break; //ENOBUFS
116  case WSAENOPROTOOPT: m_Code = eNoProtocolOption; break; //ENOPROTOOPT
117  case WSAENOTCONN: m_Code = eNotConnected; break; //ENOTCONN
118  case WSAENOTEMPTY: m_Code = eDirectoryNotEmpty; break; //ENOTEMPTY
119  case WSAENOTSOCK: m_Code = eNotASocket; break; //ENOTSOCK
120  case WSAEOPNOTSUPP: m_Code = eOperationNotSupported; break; //EOPNOTSUPP
121  case WSAEPROTONOSUPPORT: m_Code = eProtocolNotSupported; break; //EPROTONOSUPPORT
122  case WSAEPROTOTYPE: m_Code = eWrongProtocolType; break; //EPROTOTYPE
123  case WSAESHUTDOWN: m_Code = eBrokenPipe; break; // EPIPE ?
124  case WSAETIMEDOUT: m_Code = eTimedOut; break; //ETIMEDOUT
125  case WSAEWOULDBLOCK: m_Code = eOperationWouldBlock; break; //EWOULDBLOCK
126 
127  default:
128  m_Code = eUnknown;
129  break;
130  }
131  }
132 #endif
133  return m_Code;
134 }
135 
136 
138 {
140  e->m_Code = ECode(err_code);
141  e->m_Category = err_code < eUnknown ? eGeneric : eNcbi;
142  e->m_Native = err_code;
143  e->m_Extra.clear();
144  return e;
145 }
146 
147 
148 template<class Ty>
149 CNcbiError* CNcbiError::x_Init(int err_code, Ty extra)
150 {
152  e->m_Code = ECode(err_code);
153  e->m_Category = err_code < eUnknown ? eGeneric : eNcbi;
154  e->m_Native = err_code;
155  e->m_Extra = extra;
156  return e;
157 }
158 
159 
161 {
162  x_Init((int)code);
163 }
164 
165 
167 {
168  x_Init((int)code, extra);
169 }
170 
171 
172 void CNcbiError::Set(ECode code, string&& extra)
173 {
174  x_Init((int)code, extra);
175 }
176 
177 
178 void CNcbiError::Set(ECode code, const string& extra)
179 {
180  x_Init((int)code, extra);
181 }
182 
183 
184 void CNcbiError::Set(ECode code, const char* extra)
185 {
186  x_Init((int)code, extra);
187 }
188 
189 
190 void CNcbiError::SetErrno(int native_err_code)
191 {
192  x_Init(native_err_code);
193 }
194 
195 
196 void CNcbiError::SetErrno(int native_err_code, const CTempString extra)
197 {
198  x_Init(native_err_code, extra);
199 }
200 
201 
202 void CNcbiError::SetErrno(int native_err_code, string&& extra)
203 {
204  x_Init(native_err_code, extra);
205 }
206 
207 
208 void CNcbiError::SetErrno(int native_err_code, const string& extra)
209 {
210  x_Init(native_err_code, extra);
211 }
212 
213 
214 void CNcbiError::SetErrno(int native_err_code, const char* extra)
215 {
216  x_Init(native_err_code, extra);
217 }
218 
219 
221 {
222  SetErrno(errno);
223 }
224 
225 
227 {
228  SetErrno(errno, extra);
229 }
230 
231 
232 void CNcbiError::SetFromErrno(string&& extra)
233 {
234  SetErrno(errno, extra);
235 }
236 
237 
238 void CNcbiError::SetFromErrno(const string& extra)
239 {
240  SetErrno(errno, extra);
241 }
242 
243 
244 void CNcbiError::SetFromErrno(const char* extra)
245 {
246  SetErrno(errno, extra);
247 }
248 
249 
250 
251 #if defined(NCBI_OS_MSWIN)
252 
254 {
255  e->m_Code = eNotSet;
256  e->m_Category = eMsWindows;
257 }
258 
259 
260 void CNcbiError::SetWindowsError(int native_err_code)
261 {
262  x_SetWindowsCodeCategory( x_Init(native_err_code) );
263 }
264 
265 
266 void CNcbiError::SetWindowsError(int native_err_code, const CTempString extra)
267 {
268  x_SetWindowsCodeCategory( x_Init(native_err_code, extra) );
269 }
270 
271 
272 void CNcbiError::SetWindowsError(int native_err_code, string&& extra)
273 {
274  x_SetWindowsCodeCategory( x_Init(native_err_code, extra) );
275 }
276 
277 
278 void CNcbiError::SetWindowsError(int native_err_code, const string& extra)
279 {
280  x_SetWindowsCodeCategory( x_Init(native_err_code, extra) );
281 }
282 
283 
284 void CNcbiError::SetWindowsError(int native_err_code, const char* extra)
285 {
286  x_SetWindowsCodeCategory( x_Init(native_err_code, extra) );
287 }
288 
289 
291 {
292  SetWindowsError( GetLastError() );
293 }
294 
295 
297 {
298  SetWindowsError( GetLastError(), extra );
299 }
300 
301 
303 {
304  SetWindowsError( GetLastError(), extra );
305 }
306 
307 
308 void CNcbiError::SetFromWindowsError(const string& extra)
309 {
310  SetWindowsError( GetLastError(), extra );
311 }
312 
313 
314 void CNcbiError::SetFromWindowsError(const char* extra)
315 {
316  SetWindowsError( GetLastError(), extra );
317 }
318 
319 #endif
320 
321 
322 
323 /////////////////////////////////////////////////////////////////////////////
324 
326 {
327  if (err.Category() == CNcbiError::eGeneric) {
328  str << err.Code() << ": " << Ncbi_strerror(err.Code());
329  }
330 #if NCBI_OS_MSWIN
331  else if (err.Category() == CNcbiError::eMsWindows) {
332  str << err.Native() << ": " << CLastErrorAdapt::GetErrCodeString(err.Native());
333  }
334 #endif
335  else {
336  str << err.Code();
337  }
338  if (!err.Extra().empty()) {
339  str << ": " << err.Extra();
340  }
341  return str;
342 }
343 
344 
CNcbiError_Int(void)
Definition: ncbierror.cpp:47
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
static const char * str(char *buf, int n)
Definition: stats.c:84
CNcbiError(void)
Definition: ncbierror.cpp:66
static void SetFromWindowsError(void)
Set last error on MS Windows using GetLastError()
Definition: ncbierror.cpp:290
const char * Ncbi_strerror(int errnum)
Definition: ncbiexpt.cpp:793
static CNcbiError * x_Init(int err_code)
Definition: ncbierror.cpp:137
static const char * GetErrCodeString(int errnum)
Definition: ncbiexpt.cpp:826
ECategory Category(void) const
Get error code category.
Definition: ncbierror.hpp:154
const string & Extra(void) const
Get string information provided when this error was set.
Definition: ncbierror.hpp:164
static void SetFromErrno(void)
Set last error using current "errno" code.
Definition: ncbierror.cpp:220
string m_Extra
Definition: ncbierror.hpp:300
ECode Code(void) const
Get error code.
Definition: ncbierror.cpp:77
static void Set(ECode code)
Set last error using native error code enum.
Definition: ncbierror.cpp:160
int Native(void) const
Get native numeric value of the error.
Definition: ncbierror.hpp:159
static const CNcbiError & GetLast(void)
Get the error that was last set (in the current thread)
Definition: ncbierror.cpp:72
ECode m_Code
Definition: ncbierror.hpp:297
static void SetWindowsError(int native_err_code)
Set last error using Windows-specific error code.
Definition: ncbierror.cpp:260
static void x_SetWindowsCodeCategory(CNcbiError *e)
Definition: ncbierror.cpp:253
ECategory m_Category
Definition: ncbierror.hpp:298
static void SetErrno(int errno_code)
Set last error using errno code.
Definition: ncbierror.cpp:190
CNcbiOstream & operator<<(CNcbiOstream &str, const CNcbiError &err)
Serialize error code + description provided by OS + extra string data.
Definition: ncbierror.cpp:325
@ eFilenameTooLong
Definition: ncbierror.hpp:79
@ eNoSuchFileOrDirectory
Definition: ncbierror.hpp:105
@ eHostUnreachable
Definition: ncbierror.hpp:81
@ eNoProtocolOption
Definition: ncbierror.hpp:100
@ eNetworkUnreachable
Definition: ncbierror.hpp:93
@ eNotEnoughMemory
Definition: ncbierror.hpp:111
@ eTooManyFilesOpen
Definition: ncbierror.hpp:131
@ eProtocolNotSupported
Definition: ncbierror.hpp:121
@ eWrongProtocolType
Definition: ncbierror.hpp:135
@ eExecutableFormatError
Definition: ncbierror.hpp:76
@ eNoSpaceOnDevice
Definition: ncbierror.hpp:101
@ eConnectionAlreadyInProgress
Definition: ncbierror.hpp:69
@ eTooManySymbolicLinkLevels
Definition: ncbierror.hpp:133
@ eOperationNotSupported
Definition: ncbierror.hpp:116
@ eConnectionReset
Definition: ncbierror.hpp:71
@ eInvalidArgument
Definition: ncbierror.hpp:86
@ eAddressFamilyNotSupported
Generic error codes are set based on errno error codes.
Definition: ncbierror.hpp:58
@ eDirectoryNotEmpty
Definition: ncbierror.hpp:75
@ eOperationInProgress
Definition: ncbierror.hpp:114
@ eAddressNotAvailable
Definition: ncbierror.hpp:60
@ eDestinationAddressRequired
Definition: ncbierror.hpp:73
@ eConnectionAborted
Definition: ncbierror.hpp:68
@ ePermissionDenied
Definition: ncbierror.hpp:119
@ eOperationWouldBlock
Definition: ncbierror.hpp:117
@ eConnectionRefused
Definition: ncbierror.hpp:70
@ eNoBufferSpace
Definition: ncbierror.hpp:94
@ eAlreadyConnected
Definition: ncbierror.hpp:61
@ eUnknown
Unknown error.
Definition: ncbierror.hpp:138
#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
@ eSuccess
Definition: utf8.hpp:65
static CNcbiError * NcbiError_GetOrCreate(void)
Definition: ncbierror.cpp:55
static CStaticTls< CNcbiError > s_Last
Definition: ncbierror.cpp:52
Defines NCBI C++ Toolkit portable error codes.
Multi-threading – classes, functions, and features.
Definition: inftrees.h:24
Modified on Sun Apr 21 03:42:44 2024 by modify_doxy.py rev. 669887