errorhandler.hxx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Definition of the pqxx::errorhandler class.
  2. *
  3. * pqxx::errorhandler handlers errors and warnings in a database session.
  4. *
  5. * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/errorhandler instead.
  6. *
  7. * Copyright (c) 2000-2022, Jeroen T. Vermeulen.
  8. *
  9. * See COPYING for copyright license. If you did not receive a file called
  10. * COPYING with this source code, please notify the distributor of this
  11. * mistake, or contact the author.
  12. */
  13. #ifndef PQXX_H_ERRORHANDLER
  14. #define PQXX_H_ERRORHANDLER
  15. #if !defined(PQXX_HEADER_PRE)
  16. # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
  17. #endif
  18. #include "pqxx/types.hxx"
  19. namespace pqxx::internal::gate
  20. {
  21. class errorhandler_connection;
  22. }
  23. namespace pqxx
  24. {
  25. /**
  26. * @addtogroup errorhandler
  27. */
  28. //@{
  29. /// Base class for error-handler callbacks.
  30. /** To receive errors and warnings from a connection, subclass this with your
  31. * own error-handler functor, and instantiate it for the connection. Destroying
  32. * the handler un-registers it.
  33. *
  34. * A connection can have multiple error handlers at the same time. When the
  35. * database connection emits an error or warning message, it passes the message
  36. * to each error handler, starting with the most recently registered one and
  37. * progressing towards the oldest one. However an error handler may also
  38. * instruct the connection not to pass the message to further handlers by
  39. * returning "false."
  40. *
  41. * @warning Strange things happen when a result object outlives its parent
  42. * connection. If you register an error handler on a connection, then you must
  43. * not access the result after destroying the connection. This applies even if
  44. * you destroy the error handler first!
  45. */
  46. class PQXX_LIBEXPORT errorhandler
  47. {
  48. public:
  49. explicit errorhandler(connection &);
  50. virtual ~errorhandler();
  51. /// Define in subclass: receive an error or warning message from the
  52. /// database.
  53. /**
  54. * @return Whether the same error message should also be passed to the
  55. * remaining, older errorhandlers.
  56. */
  57. virtual bool operator()(char const msg[]) noexcept = 0;
  58. errorhandler() = delete;
  59. errorhandler(errorhandler const &) = delete;
  60. errorhandler &operator=(errorhandler const &) = delete;
  61. private:
  62. connection *m_home;
  63. friend class internal::gate::errorhandler_connection;
  64. void unregister() noexcept;
  65. };
  66. /// An error handler that suppresses any previously registered error handlers.
  67. class quiet_errorhandler : public errorhandler
  68. {
  69. public:
  70. /// Suppress error notices.
  71. quiet_errorhandler(connection &conn) : errorhandler{conn} {}
  72. /// Revert to previous handling of error notices.
  73. virtual bool operator()(char const[]) noexcept override { return false; }
  74. };
  75. //@}
  76. } // namespace pqxx
  77. #endif