ControllerWithErrorHandler.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_test_web_app_ControllerWithErrorHandler_hpp
  25. #define oatpp_test_web_app_ControllerWithErrorHandler_hpp
  26. #include "oatpp/web/server/api/ApiController.hpp"
  27. #include "oatpp/parser/json/mapping/ObjectMapper.hpp"
  28. #include "oatpp/core/utils/ConversionUtils.hpp"
  29. #include "oatpp/core/macro/codegen.hpp"
  30. #include "oatpp/core/macro/component.hpp"
  31. #include <sstream>
  32. namespace oatpp { namespace test { namespace web { namespace app {
  33. namespace http = oatpp::web::protocol::http;
  34. /**
  35. * Custom Error Handler.
  36. */
  37. class CustomErrorHandler : public oatpp::base::Countable, public oatpp::web::server::handler::ErrorHandler {
  38. public:
  39. /**
  40. * Constructor.
  41. */
  42. CustomErrorHandler() = default;
  43. public:
  44. /**
  45. * Create shared DefaultErrorHandler.
  46. * @return - `std::shared_ptr` to DefaultErrorHandler.
  47. */
  48. static std::shared_ptr<CustomErrorHandler> createShared() {
  49. return std::make_shared<CustomErrorHandler>();
  50. }
  51. std::shared_ptr<http::outgoing::Response> handleError(const std::exception_ptr& error) override {
  52. try {
  53. std::rethrow_exception(error);
  54. } catch(const std::runtime_error& e) {
  55. return oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(http::Status::CODE_418, e.what());
  56. } catch(...) {
  57. throw;
  58. }
  59. }
  60. std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> handleError(const http::Status& status, const oatpp::String& message, const Headers& headers) override {
  61. throw std::logic_error("Function not implemented");
  62. }
  63. };
  64. class ControllerWithErrorHandler : public oatpp::web::server::api::ApiController {
  65. private:
  66. static constexpr const char* TAG = "test::web::app::ControllerWithErrorHandler";
  67. public:
  68. explicit ControllerWithErrorHandler(const std::shared_ptr<ObjectMapper>& objectMapper)
  69. : oatpp::web::server::api::ApiController(objectMapper)
  70. {
  71. setErrorHandler(CustomErrorHandler::createShared());
  72. }
  73. public:
  74. static std::shared_ptr<ControllerWithErrorHandler> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
  75. return std::make_shared<ControllerWithErrorHandler>(objectMapper);
  76. }
  77. #include OATPP_CODEGEN_BEGIN(ApiController)
  78. ENDPOINT("GET", "test/errorhandling", errorCaught,
  79. REQUEST(std::shared_ptr<IncomingRequest>, request))
  80. {
  81. throw std::runtime_error("Controller With Errors!");
  82. }
  83. #include OATPP_CODEGEN_END(ApiController)
  84. };
  85. }}}}
  86. #endif /* oatpp_test_web_app_ControllerWithErrorHandler_hpp */