You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: inetstl/error/exceptions.hpp
  3. *
  4. * Purpose: Contains the internet_exception class.
  5. *
  6. * Created: 25th April 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file inetstl/error/exceptions.hpp
  40. *
  41. * \brief [C++ only] Definition of the inetstl::internet_exception and
  42. * exception class and the inetstl::throw_internet_exception_policy
  43. * exception policy class
  44. * (\ref group__library__error "Error" Library).
  45. */
  46. #ifndef INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS
  47. #define INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define INETSTL_VER_INETSTL_ERROR_HPP_EXCEPTIONS_MAJOR 4
  50. # define INETSTL_VER_INETSTL_ERROR_HPP_EXCEPTIONS_MINOR 2
  51. # define INETSTL_VER_INETSTL_ERROR_HPP_EXCEPTIONS_REVISION 1
  52. # define INETSTL_VER_INETSTL_ERROR_HPP_EXCEPTIONS_EDIT 42
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Compatibility
  56. */
  57. /*
  58. [DocumentationStatus:Ready]
  59. */
  60. /* /////////////////////////////////////////////////////////////////////////
  61. * Includes
  62. */
  63. #ifndef INETSTL_INCL_INETSTL_H_INETSTL
  64. # include <inetstl/inetstl.h>
  65. #endif /* !INETSTL_INCL_INETSTL_H_INETSTL */
  66. #ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
  67. # include <stlsoft/error/exceptions.hpp>
  68. #endif /* !STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS */
  69. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_EXCEPTION_STRING
  70. # include <stlsoft/util/exception_string.hpp>
  71. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_EXCEPTION_STRING */
  72. #ifndef INETSTL_OS_IS_WINDOWS
  73. # include <errno.h>
  74. #endif /* !INETSTL_OS_IS_WINDOWS */
  75. #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
  76. # error This file cannot be included when exception-handling is not supported
  77. #endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
  78. /* /////////////////////////////////////////////////////////////////////////
  79. * Namespace
  80. */
  81. #ifndef _INETSTL_NO_NAMESPACE
  82. # if defined(_STLSOFT_NO_NAMESPACE) || \
  83. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  84. /* There is no stlsoft namespace, so must define ::inetstl */
  85. namespace inetstl
  86. {
  87. # else
  88. /* Define stlsoft::inetstl_project */
  89. namespace stlsoft
  90. {
  91. namespace inetstl_project
  92. {
  93. # endif /* _STLSOFT_NO_NAMESPACE */
  94. #endif /* !_INETSTL_NO_NAMESPACE */
  95. /* /////////////////////////////////////////////////////////////////////////
  96. * Classes
  97. */
  98. /** \brief General exception class for internet-related failures.
  99. *
  100. * \ingroup group__library__error
  101. *
  102. */
  103. class internet_exception
  104. : public os_exception
  105. {
  106. /// \name Types
  107. /// @{
  108. protected:
  109. typedef stlsoft_ns_qual(exception_string) string_type;
  110. public:
  111. typedef os_exception parent_class_type;
  112. #ifdef INETSTL_OS_IS_WINDOWS
  113. typedef is_dword_t error_code_type;
  114. #else /* ? INETSTL_OS_IS_WINDOWS */
  115. typedef int error_code_type;
  116. #endif /* INETSTL_OS_IS_WINDOWS */
  117. typedef internet_exception class_type;
  118. /// @}
  119. /// \name Construction
  120. /// @{
  121. public:
  122. ss_explicit_k internet_exception(error_code_type err)
  123. : m_reason()
  124. , m_errorCode(err)
  125. {}
  126. internet_exception(class_type const& rhs)
  127. : m_reason(rhs.m_reason)
  128. , m_errorCode(rhs.m_errorCode)
  129. {}
  130. internet_exception(char const* reason, error_code_type err)
  131. : m_reason(class_type::create_reason_(reason, err))
  132. , m_errorCode(err)
  133. {}
  134. protected:
  135. internet_exception(string_type const& reason, error_code_type err)
  136. : m_reason(reason)
  137. , m_errorCode(err)
  138. {}
  139. public:
  140. /// Destructor
  141. ///
  142. /// \note This does not do have any implementation, but is required to placate
  143. /// the Comeau and GCC compilers, which otherwise complain about mismatched
  144. /// exception specifications between this class and its parent
  145. virtual ~internet_exception() stlsoft_throw_0()
  146. {}
  147. /// @}
  148. /// \name Accessors
  149. /// @{
  150. public:
  151. virtual char const* what() const stlsoft_throw_0()
  152. {
  153. if(!m_reason.empty())
  154. {
  155. return m_reason.c_str();
  156. }
  157. else
  158. {
  159. return "internet failure";
  160. }
  161. }
  162. /// The error code associated with the exception
  163. error_code_type get_error_code() const stlsoft_throw_0()
  164. {
  165. return m_errorCode;
  166. }
  167. /// [DEPRECATED] The error code associated with the exception
  168. ///
  169. /// \deprecated Use get_error_code() instead.
  170. error_code_type last_error() const stlsoft_throw_0()
  171. {
  172. return get_error_code();
  173. }
  174. /// [DEPRECATED] The error code associated with the exception
  175. ///
  176. /// \deprecated Use get_error_code() instead.
  177. error_code_type error() const stlsoft_throw_0()
  178. {
  179. return get_error_code();
  180. }
  181. /// @}
  182. /// \name Implementation
  183. /// @{
  184. private:
  185. static string_type create_reason_(char const* reason, error_code_type err)
  186. {
  187. #ifdef INETSTL_OS_IS_WINDOWS
  188. if( err == static_cast<error_code_type>(E_OUTOFMEMORY) ||
  189. err == static_cast<error_code_type>(ERROR_OUTOFMEMORY) ||
  190. NULL == reason ||
  191. '\0' == reason[0])
  192. {
  193. return string_type();
  194. }
  195. else
  196. #endif /* INETSTL_OS_IS_WINDOWS */
  197. {
  198. return string_type(reason);
  199. }
  200. }
  201. /// @}
  202. /// \name Members
  203. /// @{
  204. private:
  205. const string_type m_reason;
  206. error_code_type m_errorCode;
  207. /// @}
  208. /// \name Not to be implemented
  209. /// @{
  210. private:
  211. class_type& operator =(class_type const&);
  212. /// @}
  213. };
  214. /* /////////////////////////////////////////////////////////////////////////
  215. * Policies
  216. */
  217. /** \brief The policy class, which throws a internet_exception class.
  218. *
  219. * \ingroup group__library__error
  220. *
  221. */
  222. // [[synesis:class:exception-policy: throw_internet_exception_policy]]
  223. struct throw_internet_exception_policy
  224. {
  225. /// \name Member Types
  226. /// @{
  227. public:
  228. /// The thrown type
  229. typedef internet_exception thrown_type;
  230. typedef internet_exception::error_code_type error_code_type;
  231. /// @}
  232. /// \name Operators
  233. /// @{
  234. public:
  235. /// Function call operator, taking no parameters
  236. void operator ()() const
  237. {
  238. #ifdef INETSTL_OS_IS_WINDOWS
  239. STLSOFT_THROW_X(thrown_type(::GetLastError()));
  240. #else /* ? INETSTL_OS_IS_WINDOWS */
  241. STLSOFT_THROW_X(thrown_type(errno));
  242. #endif /* INETSTL_OS_IS_WINDOWS */
  243. }
  244. /// Function call operator, taking one parameter
  245. void operator ()(error_code_type err) const
  246. {
  247. STLSOFT_THROW_X(thrown_type(err));
  248. }
  249. /// Function call operator, taking two parameters
  250. void operator ()(char const* reason, error_code_type err) const
  251. {
  252. STLSOFT_THROW_X(thrown_type(reason, err));
  253. }
  254. /// @}
  255. };
  256. /* ////////////////////////////////////////////////////////////////////// */
  257. #ifndef _INETSTL_NO_NAMESPACE
  258. # if defined(_STLSOFT_NO_NAMESPACE) || \
  259. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  260. } // namespace inetstl
  261. # else
  262. } // namespace inetstl_project
  263. } // namespace stlsoft
  264. # endif /* _STLSOFT_NO_NAMESPACE */
  265. #endif /* !_INETSTL_NO_NAMESPACE */
  266. /* ////////////////////////////////////////////////////////////////////// */
  267. #endif /* INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS */
  268. /* ///////////////////////////// end of file //////////////////////////// */