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 101081 2023-10-26 18:49:55Z sadyrovr $
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  e451_Unavailable_For_Legal_Reasons = CRequestStatus::e451_Unavailable_For_Legal_Reasons,
107 
108  e500_InternalServerError = CRequestStatus::e500_InternalServerError,
109  e501_NotImplemented = CRequestStatus::e501_NotImplemented,
111  e503_ServiceUnavailable = CRequestStatus::e503_ServiceUnavailable,
112  e504_GatewayTimeout = CRequestStatus::e504_GatewayTimeout,
113  e505_HTTPVerNotSupported = CRequestStatus::e505_HTTPVerNotSupported
114  };
115 
116  CCgiException& SetStatus(const SCgiStatus& status);
117 
119  {
120  return m_StatusCode;
121  }
122  string GetStatusMessage(void) const
123  {
124  return m_StatusMessage.empty() ?
125  GetStdStatusMessage(m_StatusCode) : m_StatusMessage;
126  }
127 
128  static string GetStdStatusMessage(EStatusCode code);
129 
131 
132 protected:
133  /// Override method for initializing exception data.
134  virtual void x_Init(const CDiagCompileInfo& info,
135  const string& message,
136  const CException* prev_exception,
137  EDiagSev severity) override;
138 
139  /// Override method for copying exception data.
140  virtual void x_Assign(const CException& src) override;
141 
142 private:
145 };
146 
147 
148 struct SCgiStatus {
150  const string& message = kEmptyStr)
151  : m_Code(code), m_Message(message) {}
153  string m_Message;
154 };
155 
156 
157 /////////////////////////////////////////////////////////////////////////////
158 ///
159 /// CCgiCookieException --
160 ///
161 /// Exceptions used by CCgiCookie and CCgiCookies classes
162 
163 class CCgiCookieException : public CParseTemplException<CCgiException>
164 {
165 public:
166  enum EErrCode {
167  eValue, //< Bad cookie value
168  eString //< Bad cookie string (Set-Cookie:) format
169  };
170  virtual const char* GetErrCodeString(void) const override
171  {
172  switch (GetErrCode()) {
173  case eValue: return "Bad cookie";
174  case eString: return "Bad cookie string format";
175  default: return CException::GetErrCodeString();
176  }
177  }
178 
181  std::string::size_type);
182 };
183 
184 
185 
186 /////////////////////////////////////////////////////////////////////////////
187 ///
188 /// CCgiRequestException --
189 ///
190 ///
191 /// Exceptions to be used by CGI framework itself (see CCgiParseException)
192 /// or the CGI application's request processing code in the cases when there
193 /// is a problem is in the HTTP request itself (its header and/or body).
194 /// The problem can be in the syntax as well as in the content.
195 
197 {
198 public:
199  /// Bad (malformed or missing) HTTP request components
200  enum EErrCode {
201  eCookie, //< Cookie
202  eRead, //< Error in reading raw content of HTTP request
203  eIndex, //< ISINDEX
204  eEntry, //< Entry value
205  eAttribute, //< Entry attribute
206  eFormat, //< Format or encoding
207  eData //< Syntaxically correct but contains odd data (from the
208  //< point of view of particular CGI application)
209  };
210  virtual const char* GetErrCodeString(void) const override
211  {
212  switch ( GetErrCode() ) {
213  case eCookie: return "Malformed HTTP Cookie";
214  case eRead: return "Error in receiving HTTP request";
215  case eIndex: return "Error in parsing ISINDEX-type CGI arguments";
216  case eEntry: return "Error in parsing CGI arguments";
217  case eAttribute: return "Bad part attribute in multipart HTTP request";
218  case eFormat: return "Misformatted data in HTTP request";
219  case eData: return "Unexpected or inconsistent HTTP request";
220  default: return CException::GetErrCodeString();
221  }
222  }
223 
225 };
226 
227 
228 
229 /////////////////////////////////////////////////////////////////////////////
230 ///
231 /// CCgiParseException --
232 ///
233 /// Exceptions used by CGI framework when the error has occured while
234 /// parsing the contents (header and/or body) of the HTTP request
235 
236 class CCgiParseException : public CParseTemplException<CCgiRequestException>
237 {
238 public:
239  /// @sa CCgiRequestException
240  enum EErrCode {
246  // WARNING: no enums not listed in "CCgiRequestException::EErrCode"
247  // can be here -- unless you re-implement GetErrCodeString()
248  };
249 
252  std::string::size_type);
253 };
254 
255 
256 /////////////////////////////////////////////////////////////////////////////
257 ///
258 /// CCgiErrnoException --
259 ///
260 /// Exceptions used by CGI framework when the error is more system-related
261 /// and there is an "errno" status from the system call that can be obtained
262 
263 class CCgiErrnoException : public CErrnoTemplException<CCgiException>
264 {
265 public:
266  enum EErrCode {
267  eErrno, //< Generic system call failure
268  eModTime //< File modification time cannot be obtained
269  };
270  virtual const char* GetErrCodeString(void) const override
271  {
272  switch (GetErrCode()) {
273  case eErrno: return "System error";
274  case eModTime: return "File system error";
275  default: return CException::GetErrCodeString();
276  }
277  }
278 
281 };
282 
283 
284 /////////////////////////////////////////////////////////////////////////////
285 ///
286 /// CCgiResponseException --
287 ///
288 /// Exceptions used by CGI response
289 
291 {
292 public:
293  enum EErrCode {
294  eDoubleHeader, ///< Header has already been written
295  eBadHeaderValue ///< Invalid header value
296  };
297  virtual const char* GetErrCodeString(void) const override
298  {
299  switch ( GetErrCode() ) {
300  case eDoubleHeader: return "Header has already been written";
301  case eBadHeaderValue: return "Invalid header value";
302  default: return CException::GetErrCodeString();
303  }
304  }
305 
307 };
308 
309 
310 /////////////////////////////////////////////////////////////////////////////
311 ///
312 /// CCgiHeadException --
313 ///
314 /// Exceptions used to stop processing request after sending header when
315 /// the request method is HEAD. This does not indicate an error, it's
316 /// just a way to exit from ProcessRequest.
317 
319 {
320 public:
321  enum EErrCode {
322  eHeaderSent ///< Header has been written
323  };
324 
325  virtual const char* GetErrCodeString(void) const override
326  {
327  switch ( GetErrCode() ) {
328  case eHeaderSent: return "Header has been written";
329  default: return CException::GetErrCodeString();
330  }
331  }
332 
334 };
335 
336 
337 /////////////////////////////////////////////////////////////////////////////
338 ///
339 /// CCgiSessionException --
340 ///
341 /// Exceptions used by CGI session
342 
344 {
345 public:
346  enum EErrCode {
347  eSessionId, ///< SessionId not specified
348  eImplNotSet, ///< Session implementation not set
349  eDeleted, ///< Session has been deleted
350  eSessionDoesnotExist, ///< Session does not exist
351  eImplException, ///< Implementation exception
352  eAttrNotFound, ///< Attribute not found
353  eNotLoaded ///< Session not loaded
354  };
355  virtual const char* GetErrCodeString(void) const override
356  {
357  switch ( GetErrCode() ) {
358  case eSessionId: return "SessionId not specified";
359  case eImplNotSet: return "Session implementation not set";
360  case eDeleted: return "Session has been deleted";
361  case eSessionDoesnotExist: return "Session does not exist";
362  case eImplException: return "Implementation exception";
363  case eAttrNotFound: return "Attribute not found";
364  case eNotLoaded: return "Session not loaded";
365  default: return CException::GetErrCodeString();
366  }
367  }
368 
370 };
371 
372 
373 /////////////////////////////////////////////////////////////////////////////
374 ///
375 /// CCgiAppException --
376 ///
377 /// Other CCgiApplication exceptions.
378 
380 {
381 public:
382  enum EErrCode {
383  eApp //< Other error
384  };
385 
386  virtual const char* GetErrCodeString(void) const override
387  {
388  switch (GetErrCode()) {
389  case eApp: return "CGI application error";
390  default: return CException::GetErrCodeString();
391  }
392  }
393 
395 };
396 
397 
398 #define NCBI_CGI_THROW_WITH_STATUS(exception, err_code, message, status) \
399  { \
400  NCBI_EXCEPTION_VAR(cgi_exception, exception, err_code, message); \
401  cgi_exception.SetStatus( (status) ); \
402  NCBI_EXCEPTION_THROW(cgi_exception); \
403  }
404 
405 #define NCBI_CGI_THROW2_WITH_STATUS(exception, err_code, \
406  message, extra, status) \
407  { \
408  NCBI_EXCEPTION2_VAR(cgi_exception, exception, \
409  err_code, message, extra); \
410  cgi_exception.SetStatus( (status) ); \
411  NCBI_EXCEPTION_THROW(cgi_exception); \
412  }
413 
414 
415 inline
417 {
418  m_StatusCode = status.m_Code;
419  m_StatusMessage = status.m_Message;
420  return *this;
421 }
422 
423 
424 /// @deprecated Use CUrlException
427 
428 /// @deprecated Use CUrlParserException
431 
432 
434 
435 
436 /* @} */
437 
438 #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:4397
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:4455
#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 Sat May 18 11:37:19 2024 by modify_doxy.py rev. 669887