ErrorHandler.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_web_server_handler_ErrorHandler_hpp
  25. #define oatpp_web_server_handler_ErrorHandler_hpp
  26. #include "oatpp/web/protocol/http/outgoing/Response.hpp"
  27. #include "oatpp/web/protocol/http/Http.hpp"
  28. namespace oatpp { namespace web { namespace server { namespace handler {
  29. /**
  30. * Error Handler.
  31. */
  32. class ErrorHandler {
  33. public:
  34. /**
  35. * Convenience typedef for Headers. <br>
  36. * See &id:oatpp::web::protocol::http::Headers;
  37. */
  38. typedef web::protocol::http::Headers Headers;
  39. public:
  40. /**
  41. * Virtual destructor since the class is meant to be derived from.
  42. * */
  43. virtual ~ErrorHandler() = default;
  44. /**
  45. * Implement this method!
  46. * @param error - &std::exception;.
  47. * @return - std::shared_ptr to &id:oatpp::web::protocol::http::outgoing::Response;.
  48. */
  49. virtual std::shared_ptr<protocol::http::outgoing::Response> handleError(const std::exception_ptr& exceptionPtr) = 0;
  50. /**
  51. * Implement this method!
  52. * @param status - &id:oatpp::web::protocol::http::Status;.
  53. * @param message - &id:oatpp::String;.
  54. * @param Headers - &id:oatpp::web::protocol::http::Headers;
  55. * @return - std::shared_ptr to &id:oatpp::web::protocol::http::outgoing::Response;.
  56. */
  57. [[deprecated]]
  58. virtual
  59. std::shared_ptr<protocol::http::outgoing::Response>
  60. handleError(const protocol::http::Status& status, const oatpp::String& message, const Headers& headers) = 0;
  61. /**
  62. * Convenience method to call `handleError` method with no headers.
  63. * @param status - &id:oatpp::web::protocol::http::Status;
  64. * @param message - &id:oatpp::String;.
  65. * @return - std::shared_ptr to &id:oatpp::web::protocol::http::outgoing::Response;.
  66. */
  67. [[deprecated]]
  68. std::shared_ptr<protocol::http::outgoing::Response> handleError(const protocol::http::Status& status, const oatpp::String& message);
  69. };
  70. /**
  71. * Default Error Handler.
  72. */
  73. class DefaultErrorHandler : public oatpp::base::Countable, public ErrorHandler {
  74. public:
  75. /**
  76. * Constructor.
  77. */
  78. DefaultErrorHandler() = default;
  79. public:
  80. /**
  81. * Create shared DefaultErrorHandler.
  82. * @return - `std::shared_ptr` to DefaultErrorHandler.
  83. */
  84. static std::shared_ptr<DefaultErrorHandler> createShared() {
  85. return std::make_shared<DefaultErrorHandler>();
  86. }
  87. std::shared_ptr<protocol::http::outgoing::Response> handleError(const std::exception_ptr& error) override;
  88. /**
  89. * Implementation of &l:ErrorHandler::handleError ();
  90. * @param status - &id:oatpp::web::protocol::http::Status;.
  91. * @param message - &id:oatpp::String;.
  92. * @return - &id:oatpp::web::protocol::http::outgoing::Response;.
  93. */
  94. std::shared_ptr<protocol::http::outgoing::Response>
  95. handleError(const protocol::http::Status& status, const oatpp::String& message, const Headers& headers) override;
  96. };
  97. }}}}
  98. #endif /* oatpp_web_server_handler_ErrorHandler_hpp */