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

Go to the SVN repository for this file.

1 #ifndef CGI___CGI_EXCEPTION__HPP
2 #define CGI___CGI_EXCEPTION__HPP
3 
4 /* $Id: cgi_exception.hpp 102662 2024-06-24 13:10:46Z satskyse $
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: Andrei Gourianov, Denis Vakatov
30 *
31 */
32 
33 /// @file cgi_exception.hpp
34 /// Exception classes used by the NCBI CGI framework
35 ///
36 
37 
38 #include <corelib/ncbiexpt.hpp>
39 #include <corelib/ncbistr.hpp>
41 #include <util/ncbi_url.hpp>
42 
43 
44 /** @addtogroup CGIExcep
45  *
46  * @{
47  */
48 
49 
51 
52 
53 /////////////////////////////////////////////////////////////////////////////
54 ///
55 /// CCgiException --
56 ///
57 /// Base class for the exceptions used by CGI framework
58 
59 struct SCgiStatus;
60 
62 {
63 public:
64  /// HTTP status codes
65  enum EStatusCode {
66  eStatusNotSet = 0, ///< Internal value - code not set
67 
69  e101_SwitchingProtocols = CRequestStatus::e101_SwitchingProtocols,
70 
74  e203_NonAuthInformation = CRequestStatus::e203_NonAuthInformation,
76  e205_ResetContent = CRequestStatus::e205_ResetContent,
77  e206_PartialContent = CRequestStatus::e206_PartialContent,
78 
79  e300_MultipleChoices = CRequestStatus::e300_MultipleChoices,
80  e301_MovedPermanently = CRequestStatus::e301_MovedPermanently,
85  e307_TemporaryRedirect = CRequestStatus::e307_TemporaryRedirect,
86 
88  e401_Unauthorized = CRequestStatus::e401_Unauthorized,
89  e402_PaymentRequired = CRequestStatus::e402_PaymentRequired,
92  e405_MethodNotAllowed = CRequestStatus::e405_MethodNotAllowed,
93  e406_NotAcceptable = CRequestStatus::e406_NotAcceptable,
94  e407_ProxyAuthRequired = CRequestStatus::e407_ProxyAuthRequired,
95  e408_RequestTimeout = CRequestStatus::e408_RequestTimeout,
98  e411_LengthRequired = CRequestStatus::e411_LengthRequired,
99  e412_PreconditionFailed = CRequestStatus::e412_PreconditionFailed,
100  e413_RequestEntityTooLarge = CRequestStatus::e413_RequestEntityTooLarge,
101  e414_RequestURITooLong = CRequestStatus::e414_RequestURITooLong,
102  e415_UnsupportedMediaType = CRequestStatus::e415_UnsupportedMediaType,
103  e416_RangeNotSatisfiable = CRequestStatus::e416_RangeNotSatisfiable,
104  e417_ExpectationFailed = CRequestStatus::e417_ExpectationFailed,
105  e422_UnprocessableEntity = CRequestStatus::e422_UnprocessableEntity,
106  e429_Too_Many_Requests = CRequestStatus::e429_Too_Many_Requests,
107  e451_Unavailable_For_Legal_Reasons = CRequestStatus::e451_Unavailable_For_Legal_Reasons,
108 
109  e500_InternalServerError = CRequestStatus::e500_InternalServerError,
110  e501_NotImplemented = CRequestStatus::e501_NotImplemented,
112  e503_ServiceUnavailable = CRequestStatus::e503_ServiceUnavailable,
113  e504_GatewayTimeout = CRequestStatus::e504_GatewayTimeout,
114  e505_HTTPVerNotSupported = CRequestStatus::e505_HTTPVerNotSupported
115  };
116 
117  CCgiException& SetStatus(const SCgiStatus& status);
118 
120  {
121  return m_StatusCode;
122  }
123  string GetStatusMessage(void) const
124  {
125  return m_StatusMessage.empty() ?
126  GetStdStatusMessage(m_StatusCode) : m_StatusMessage;
127  }
128 
129  static string GetStdStatusMessage(EStatusCode code);
130 
132 
133 protected:
134  /// Override method for initializing exception data.
135  virtual void x_Init(const CDiagCompileInfo& info,
136  const string& message,
137  const CException* prev_exception,
138  EDiagSev severity) override;
139 
140  /// Override method for copying exception data.
141  virtual void x_Assign(const CException& src) override;
142 
143 private:
146 };
147 
148 
149 struct SCgiStatus {
151  const string& message = kEmptyStr)
152  : m_Code(code), m_Message(message) {}
154  string m_Message;
155 };
156 
157 
158 /////////////////////////////////////////////////////////////////////////////
159 ///
160 /// CCgiCookieException --
161 ///
162 /// Exceptions used by CCgiCookie and CCgiCookies classes
163 
164 class CCgiCookieException : public CParseTemplException<CCgiException>
165 {
166 public:
167  enum EErrCode {
168  eValue, //< Bad cookie value
169  eString //< Bad cookie string (Set-Cookie:) format
170  };
171  virtual const char* GetErrCodeString(void) const override
172  {
173  switch (GetErrCode()) {
174  case eValue: return "Bad cookie";
175  case eString: return "Bad cookie string format";
176  default: return CException::GetErrCodeString();
177  }
178  }
179 
182  std::string::size_type);
183 };
184 
185 
186 
187 /////////////////////////////////////////////////////////////////////////////
188 ///
189 /// CCgiRequestException --
190 ///
191 ///
192 /// Exceptions to be used by CGI framework itself (see CCgiParseException)
193 /// or the CGI application's request processing code in the cases when there
194 /// is a problem is in the HTTP request itself (its header and/or body).
195 /// The problem can be in the syntax as well as in the content.
196 
198 {
199 public:
200  /// Bad (malformed or missing) HTTP request components
201  enum EErrCode {
202  eCookie, //< Cookie
203  eRead, //< Error in reading raw content of HTTP request
204  eIndex, //< ISINDEX
205  eEntry, //< Entry value
206  eAttribute, //< Entry attribute
207  eFormat, //< Format or encoding
208  eData //< Syntaxically correct but contains odd data (from the
209  //< point of view of particular CGI application)
210  };
211  virtual const char* GetErrCodeString(void) const override
212  {
213  switch ( GetErrCode() ) {
214  case eCookie: return "Malformed HTTP Cookie";
215  case eRead: return "Error in receiving HTTP request";
216  case eIndex: return "Error in parsing ISINDEX-type CGI arguments";
217  case eEntry: return "Error in parsing CGI arguments";
218  case eAttribute: return "Bad part attribute in multipart HTTP request";
219  case eFormat: return "Misformatted data in HTTP request";
220  case eData: return "Unexpected or inconsistent HTTP request";
221  default: return CException::GetErrCodeString();
222  }
223  }
224 
226 };
227 
228 
229 
230 /////////////////////////////////////////////////////////////////////////////
231 ///
232 /// CCgiParseException --
233 ///
234 /// Exceptions used by CGI framework when the error has occured while
235 /// parsing the contents (header and/or body) of the HTTP request
236 
237 class CCgiParseException : public CParseTemplException<CCgiRequestException>
238 {
239 public:
240  /// @sa CCgiRequestException
241  enum EErrCode {
247  // WARNING: no enums not listed in "CCgiRequestException::EErrCode"
248  // can be here -- unless you re-implement GetErrCodeString()
249  };
250 
253  std::string::size_type);
254 };
255 
256 
257 /////////////////////////////////////////////////////////////////////////////
258 ///
259 /// CCgiErrnoException --
260 ///
261 /// Exceptions used by CGI framework when the error is more system-related
262 /// and there is an "errno" status from the system call that can be obtained
263 
264 class CCgiErrnoException : public CErrnoTemplException<CCgiException>
265 {
266 public:
267  enum EErrCode {
268  eErrno, //< Generic system call failure
269  eModTime //< File modification time cannot be obtained
270  };
271  virtual const char* GetErrCodeString(void) const override
272  {
273  switch (GetErrCode()) {
274  case eErrno: return "System error";
275  case eModTime: return "File system error";
276  default: return CException::GetErrCodeString();
277  }
278  }
279 
282 };
283 
284 
285 /////////////////////////////////////////////////////////////////////////////
286 ///
287 /// CCgiResponseException --
288 ///
289 /// Exceptions used by CGI response
290 
292 {
293 public:
294  enum EErrCode {
295  eDoubleHeader, ///< Header has already been written
296  eBadHeaderValue ///< Invalid header value
297  };
298  virtual const char* GetErrCodeString(void) const override
299  {
300  switch ( GetErrCode() ) {
301  case eDoubleHeader: return "Header has already been written";
302  case eBadHeaderValue: return "Invalid header value";
303  default: return CException::GetErrCodeString();
304  }
305  }
306 
308 };
309 
310 
311 /////////////////////////////////////////////////////////////////////////////
312 ///
313 /// CCgiHeadException --
314 ///
315 /// Exceptions used to stop processing request after sending header when
316 /// the request method is HEAD. This does not indicate an error, it's
317 /// just a way to exit from ProcessRequest.
318 
320 {
321 public:
322  enum EErrCode {
323  eHeaderSent ///< Header has been written
324  };
325 
326  virtual const char* GetErrCodeString(void) const override
327  {
328  switch ( GetErrCode() ) {
329  case eHeaderSent: return "Header has been written";
330  default: return CException::GetErrCodeString();
331  }
332  }
333 
335 };
336 
337 
338 /////////////////////////////////////////////////////////////////////////////
339 ///
340 /// CCgiSessionException --
341 ///
342 /// Exceptions used by CGI session
343 
345 {
346 public:
347  enum EErrCode {
348  eSessionId, ///< SessionId not specified
349  eImplNotSet, ///< Session implementation not set
350  eDeleted, ///< Session has been deleted
351  eSessionDoesnotExist, ///< Session does not exist
352  eImplException, ///< Implementation exception
353  eAttrNotFound, ///< Attribute not found
354  eNotLoaded ///< Session not loaded
355  };
356  virtual const char* GetErrCodeString(void) const override
357  {
358  switch ( GetErrCode() ) {
359  case eSessionId: return "SessionId not specified";
360  case eImplNotSet: return "Session implementation not set";
361  case eDeleted: return "Session has been deleted";
362  case eSessionDoesnotExist: return "Session does not exist";
363  case eImplException: return "Implementation exception";
364  case eAttrNotFound: return "Attribute not found";
365  case eNotLoaded: return "Session not loaded";
366  default: return CException::GetErrCodeString();
367  }
368  }
369 
371 };
372 
373 
374 /////////////////////////////////////////////////////////////////////////////
375 ///
376 /// CCgiAppException --
377 ///
378 /// Other CCgiApplication exceptions.
379 
381 {
382 public:
383  enum EErrCode {
384  eApp //< Other error
385  };
386 
387  virtual const char* GetErrCodeString(void) const override
388  {
389  switch (GetErrCode()) {
390  case eApp: return "CGI application error";
391  default: return CException::GetErrCodeString();
392  }
393  }
394 
396 };
397 
398 
399 #define NCBI_CGI_THROW_WITH_STATUS(exception, err_code, message, status) \
400  { \
401  NCBI_EXCEPTION_VAR(cgi_exception, exception, err_code, message); \
402  cgi_exception.SetStatus( (status) ); \
403  NCBI_EXCEPTION_THROW(cgi_exception); \
404  }
405 
406 #define NCBI_CGI_THROW2_WITH_STATUS(exception, err_code, \
407  message, extra, status) \
408  { \
409  NCBI_EXCEPTION2_VAR(cgi_exception, exception, \
410  err_code, message, extra); \
411  cgi_exception.SetStatus( (status) ); \
412  NCBI_EXCEPTION_THROW(cgi_exception); \
413  }
414 
415 
416 inline
418 {
419  m_StatusCode = status.m_Code;
420  m_StatusMessage = status.m_Message;
421  return *this;
422 }
423 
424 
425 /// @deprecated Use CUrlException
428 
429 /// @deprecated Use CUrlParserException
432 
433 
435 
436 
437 /* @} */
438 
439 #endif // CGI___CGI_EXCEPTION__HPP
CCgiAppException –.
CCgiCookieException –.
CCgiErrnoException –.
CCgiHeadException –.
CCgiParseException –.
CCgiRequestException –.
CCgiResponseException –.
CCgiSessionException –.
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
CErrnoTemplException –.
Definition: ncbiexpt.hpp:1681
CParseTemplException –.
Definition: ncbistr.hpp:4391
CUrlException –.
Definition: ncbi_url.hpp:511
CUrlParserException –.
Definition: ncbi_url.hpp:539
The NCBI C++ standard methods for dealing with std::string.
NCBI_EXCEPTION_DEFAULT(CCgiException, CException)
NCBI_EXCEPTION_DEFAULT(CCgiSessionException, CCgiException)
virtual const char * GetErrCodeString(void) const override
Translate from the error code value to its string representation.
virtual const char * GetErrCodeString(void) const override
virtual const char * GetErrCodeString(void) const override
Translate from the error code value to its string representation.
virtual const char * GetErrCodeString(void) const override
virtual const char * GetErrCodeString(void) const override
string m_Message
EStatusCode GetStatusCode(void) const
NCBI_EXCEPTION_DEFAULT2(CCgiCookieException, CParseTemplException< CCgiException >, std::string::size_type)
string m_StatusMessage
CUrlException CCgiArgsException
virtual const char * GetErrCodeString(void) const override
EErrCode
Bad (malformed or missing) HTTP request components.
NCBI_EXCEPTION_DEFAULT(CCgiAppException, CCgiException)
CCgiException & SetStatus(const SCgiStatus &status)
NCBI_EXCEPTION_DEFAULT(CCgiErrnoException, CErrnoTemplException< CCgiException >)
NCBI_EXCEPTION_DEFAULT(CCgiHeadException, CCgiException)
virtual const char * GetErrCodeString(void) const override
NCBI_EXCEPTION_DEFAULT(CCgiResponseException, CCgiException)
SCgiStatus(CCgiException::EStatusCode code, const string &message=kEmptyStr)
CCgiException::EStatusCode m_Code
EStatusCode m_StatusCode
NCBI_EXCEPTION_DEFAULT2(CCgiParseException, CParseTemplException< CCgiRequestException >, std::string::size_type)
string GetStatusMessage(void) const
CUrlParserException CCgiArgsParserException
NCBI_EXCEPTION_DEFAULT(CCgiRequestException, CCgiException)
EStatusCode
HTTP status codes.
@ eHeaderSent
Header has been written.
@ eSessionId
SessionId not specified.
@ eSessionDoesnotExist
Session does not exist.
@ eDeleted
Session has been deleted.
@ eAttrNotFound
Attribute not found.
@ eImplNotSet
Session implementation not set.
@ eImplException
Implementation exception.
@ eNotLoaded
Session not loaded.
@ eDoubleHeader
Header has already been written.
@ eBadHeaderValue
Invalid header value.
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
@ e451_Unavailable_For_Legal_Reasons
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_Assign(const CException &src)
Helper method for copying exception data.
Definition: ncbiexpt.cpp:536
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
#define NCBI_DEPRECATED
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbistr.hpp:4449
#define NCBI_XCGI_EXPORT
Definition: ncbi_export.h:1097
static MDB_envinfo info
Definition: mdb_load.c:37
Defines NCBI C++ exception handling.
Defines CRequestStatus class for NCBI C++ diagnostic API.
Definition: inftrees.h:24
Modified on Fri Sep 20 14:58:08 2024 by modify_doxy.py rev. 669887