iface_mgr_error_handler.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef IFACE_MGR_ERROR_HANDLER_H
  7. #define IFACE_MGR_ERROR_HANDLER_H
  8. /// @brief A macro which handles an error in IfaceMgr.
  9. ///
  10. /// There are certain cases when IfaceMgr may hit an error which shouldn't
  11. /// result in interruption of the function processing. A typical case is
  12. /// the function which opens sockets on available interfaces for a DHCP
  13. /// server. If this function fails to open a socket on a specific interface
  14. /// (for example, there is another socket already open on this interface
  15. /// and bound to the same address and port), it is desired that the server
  16. /// logs a warning but will try to open sockets on other interfaces. In order
  17. /// to log an error, the IfaceMgr will use the error handler function provided
  18. /// by the server and pass an error string to it. When the handler function
  19. /// returns, the IfaceMgr will proceed to open other sockets. It is allowed
  20. /// that the error handler function is not installed (is NULL). In these
  21. /// cases it is expected that the exception is thrown instead. A possible
  22. /// solution would be to enclose this conditional behavior in a function.
  23. /// However, despite the hate for macros, the macro seems to be a bit
  24. /// better solution in this case as it allows to convenietly pass an
  25. /// error string in a stream (not as a string).
  26. ///
  27. /// @param ex_type Exception to be thrown if error_handler is NULL.
  28. /// @param handler Error handler function to be called or NULL to indicate
  29. /// that exception should be thrown instead.
  30. /// @param stream stream object holding an error string.
  31. #define IFACEMGR_ERROR(ex_type, handler, stream) \
  32. { \
  33. std::ostringstream ieoss__; \
  34. ieoss__ << stream; \
  35. if (handler) { \
  36. handler(ieoss__.str()); \
  37. } else { \
  38. isc_throw(ex_type, ieoss__.str()); \
  39. } \
  40. } \
  41. #endif // IFACE_MGR_ERROR_HANDLER_H