ErrorHandler.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "./ErrorHandler.hpp"
  25. #include "oatpp/web/protocol/http/outgoing/BufferBody.hpp"
  26. namespace oatpp { namespace web { namespace server { namespace handler {
  27. std::shared_ptr<protocol::http::outgoing::Response> ErrorHandler::handleError(const std::exception_ptr& exceptionPtr) {
  28. std::shared_ptr<protocol::http::outgoing::Response> response;
  29. #pragma GCC diagnostic push
  30. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  31. /* Default impl for backwards compatibility until the deprecated methods are removed */
  32. try {
  33. if(exceptionPtr) {
  34. std::rethrow_exception(exceptionPtr);
  35. }
  36. } catch (protocol::http::HttpError& httpError) {
  37. response = handleError(httpError.getInfo().status, httpError.getMessage(), httpError.getHeaders());
  38. } catch (std::exception& error) {
  39. response = handleError(protocol::http::Status::CODE_500, error.what());
  40. } catch (...) {
  41. response = handleError(protocol::http::Status::CODE_500, "An unknown error has occurred");
  42. }
  43. #pragma GCC diagnostic pop
  44. return response;
  45. }
  46. std::shared_ptr<protocol::http::outgoing::Response> ErrorHandler::handleError(const protocol::http::Status& status, const oatpp::String& message) {
  47. Headers headers;
  48. #pragma GCC diagnostic push
  49. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  50. return handleError(status, message, headers);
  51. #pragma GCC diagnostic pop
  52. }
  53. std::shared_ptr<protocol::http::outgoing::Response>
  54. DefaultErrorHandler::handleError(const oatpp::web::protocol::http::Status &status, const oatpp::String &message, const Headers& headers){
  55. data::stream::BufferOutputStream stream;
  56. stream << "server=" << protocol::http::Header::Value::SERVER << "\n";
  57. stream << "code=" << status.code << "\n";
  58. stream << "description=" << status.description << "\n";
  59. stream << "message=" << message << "\n";
  60. auto response = protocol::http::outgoing::Response::createShared
  61. (status, protocol::http::outgoing::BufferBody::createShared(stream.toString()));
  62. response->putHeader(protocol::http::Header::SERVER, protocol::http::Header::Value::SERVER);
  63. response->putHeader(protocol::http::Header::CONNECTION, protocol::http::Header::Value::CONNECTION_CLOSE);
  64. for(const auto& pair : headers.getAll()) {
  65. response->putHeader_Unsafe(pair.first, pair.second);
  66. }
  67. return response;
  68. }
  69. std::shared_ptr<protocol::http::outgoing::Response>
  70. DefaultErrorHandler::handleError(const std::exception_ptr& error) {
  71. return ErrorHandler::handleError(error);
  72. }
  73. }}}}