PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/boost/cgi/common/error.hpp

http://github.com/darrengarvey/cgi
C++ Header | 158 lines | 96 code | 30 blank | 32 comment | 0 complexity | af9a77d51b809e77d9df0e3b343c5045 MD5 | raw file
  1. // -- error.hpp --
  2. //
  3. // Copyright (c) Darren Garvey 2007.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Abstract:
  9. // ---------
  10. //
  11. // The common errors are defined in here.
  12. //
  13. // **FIXME** This is a mess.
  14. //
  15. ////////////////////////////////////////////////////////////////
  16. #ifndef BOOST_COMMON_ERROR_HPP_INCLUDED__
  17. #define BOOST_COMMON_ERROR_HPP_INCLUDED__
  18. #include <string>
  19. #include <boost/system/error_code.hpp>
  20. #include "boost/cgi/config.hpp"
  21. BOOST_CGI_NAMESPACE_BEGIN
  22. namespace common {
  23. namespace error {
  24. enum cgi_errors
  25. {
  26. // Tried to read/write from/to the client associated to a request when it
  27. // was closed.
  28. client_closed = 1,
  29. // A begin_request packet has come in with an existing request id.
  30. duplicate_request,
  31. // Calling async_accept on a request that hasn't been closed isn't
  32. // allowed.
  33. accepting_on_an_open_request,
  34. invalid_socket,
  35. // The CONTENT_TYPE for form data wasn't recognised.
  36. invalid_form_type,
  37. // Used in basic_connection<tags::stdio>
  38. // **FIXME**
  39. broken_pipe,
  40. // **TODO** Document
  41. bad_read,
  42. // **TODO** Document
  43. bad_write,
  44. // A client wasn't able to open.
  45. client_not_open,
  46. // End of File (read zero bytes)
  47. eof,
  48. // The first multipart form boundary was not found.
  49. multipart_form_boundary_not_found,
  50. // The first multipart form boundary was not found.
  51. multipart_meta_data_not_terminated,
  52. // Expected a boundary marker for a multipart form, but did not find it.
  53. no_boundary_marker,
  54. already_closed,
  55. // The content length of the file upload is larger than maximum allowed
  56. // by the BOOST_CGI_POST_MAX macro.
  57. max_post_exceeded
  58. };
  59. namespace detail {
  60. class cgi_category
  61. : public boost::system::error_category
  62. {
  63. public:
  64. const char* name() const BOOST_NOEXCEPT { return "CGI Error"; }
  65. std::string message(int e) const
  66. {
  67. switch(e)
  68. {
  69. case client_closed:
  70. return "Attempting to read from or write to a client that has been"
  71. " closed.";
  72. case accepting_on_an_open_request:
  73. return "`async_accept` called with an open request (ie. it should be"
  74. " closed first).";
  75. case multipart_form_boundary_not_found:
  76. return "The first multipart form boundary was not found.";
  77. case no_boundary_marker:
  78. return "Expected a boundary marker for a multipart form, but did not"
  79. " find it.";
  80. case invalid_form_type:
  81. return "The CONTENT_TYPE for form data wasn't recognised.";
  82. case eof:
  83. return "End of File.";
  84. case duplicate_request:
  85. return "FastCGI: new request received with a duplicate id.";
  86. case max_post_exceeded:
  87. return "The content length of the file upload is larger than maximum"
  88. " allowed by the BOOST_CGI_POST_MAX macro.";
  89. case already_closed:
  90. return "Trying to close a request that has already been closed.";
  91. default:
  92. return "(CGI) BOOM!!!";
  93. }
  94. }
  95. };
  96. } // namespace detail
  97. inline const boost::system::error_category& get_cgi_category()
  98. {
  99. static detail::cgi_category instance;
  100. return instance;
  101. }
  102. static const boost::system::error_category& cgi_category
  103. = ::BOOST_CGI_NAMESPACE::common::error::get_cgi_category();
  104. } // namespace error
  105. } // namespace common
  106. BOOST_CGI_NAMESPACE_END
  107. namespace boost {
  108. namespace system {
  109. template<> struct is_error_code_enum<
  110. ::BOOST_CGI_NAMESPACE::common::error::cgi_errors
  111. >
  112. {
  113. BOOST_STATIC_CONSTANT(bool, value = true);
  114. };
  115. } // namespace system
  116. } // namespace boost
  117. BOOST_CGI_NAMESPACE_BEGIN
  118. namespace common {
  119. namespace error {
  120. inline boost::system::error_code make_error_code(cgi_errors e)
  121. {
  122. return boost::system::error_code(
  123. static_cast<int>(e), get_cgi_category());
  124. }
  125. } // namespace error
  126. } // namespace common
  127. BOOST_CGI_NAMESPACE_END
  128. #endif // BOOST_COMMON_ERROR_HPP_INCLUDED__