diff --git a/src/oatpp/codegen/api_controller/base_define.hpp b/src/oatpp/codegen/api_controller/base_define.hpp index be0f726f..e741e92c 100644 --- a/src/oatpp/codegen/api_controller/base_define.hpp +++ b/src/oatpp/codegen/api_controller/base_define.hpp @@ -71,34 +71,26 @@ const auto& OATPP_MACRO_FIRSTARG PARAM_LIST = __request; #define OATPP_MACRO_API_CONTROLLER_HEADER_1(TYPE, NAME) \ const auto& __param_str_val_##NAME = __request->getHeader(#NAME); \ if(!__param_str_val_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing HEADER parameter '" #NAME "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing HEADER parameter '" #NAME "'"); \ } \ bool __param_validation_check_##NAME; \ const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid HEADER parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid HEADER parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_HEADER_2(TYPE, NAME, QUALIFIER) \ const auto& __param_str_val_##NAME = __request->getHeader(QUALIFIER); \ if(!__param_str_val_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, oatpp::String("Missing HEADER parameter '") + QUALIFIER + "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, oatpp::String("Missing HEADER parameter '") + QUALIFIER + "'"); \ } \ bool __param_validation_check_##NAME; \ const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Invalid HEADER parameter '") + \ - QUALIFIER + \ - "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Invalid HEADER parameter '") + \ + QUALIFIER + \ + "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_HEADER(TYPE, PARAM_LIST) \ @@ -121,35 +113,27 @@ OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_HEADER_INFO #define OATPP_MACRO_API_CONTROLLER_PATH_1(TYPE, NAME) \ const auto& __param_str_val_##NAME = __request->getPathVariable(#NAME); \ if(!__param_str_val_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing PATH parameter '" #NAME "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing PATH parameter '" #NAME "'"); \ } \ bool __param_validation_check_##NAME; \ const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid PATH parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid PATH parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_PATH_2(TYPE, NAME, QUALIFIER) \ const auto& __param_str_val_##NAME = __request->getPathVariable(QUALIFIER); \ if(!__param_str_val_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Missing PATH parameter '") + QUALIFIER + "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Missing PATH parameter '") + QUALIFIER + "'"); \ } \ bool __param_validation_check_##NAME; \ const auto NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Invalid PATH parameter '") + \ - QUALIFIER + \ - "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Invalid PATH parameter '") + \ + QUALIFIER + \ + "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_PATH(TYPE, PARAM_LIST) \ @@ -178,35 +162,27 @@ const auto& OATPP_MACRO_FIRSTARG PARAM_LIST = __request->getQueryParameters(); #define OATPP_MACRO_API_CONTROLLER_QUERY_1(TYPE, NAME) \ const auto& __param_str_val_##NAME = __request->getQueryParameter(#NAME); \ if(!__param_str_val_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing QUERY parameter '" #NAME "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing QUERY parameter '" #NAME "'"); \ } \ bool __param_validation_check_##NAME; \ const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid QUERY parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Invalid QUERY parameter '" #NAME "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_QUERY_2(TYPE, NAME, QUALIFIER) \ const auto& __param_str_val_##NAME = __request->getQueryParameter(QUALIFIER); \ if(!__param_str_val_##NAME) \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Missing QUERY parameter '") + QUALIFIER + "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Missing QUERY parameter '") + QUALIFIER + "'"); \ } \ bool __param_validation_check_##NAME; \ const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Invalid QUERY parameter '") + \ - QUALIFIER + \ - "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Invalid QUERY parameter '") + \ + QUALIFIER + \ + "'. Expected type is '" #TYPE "'"); \ } #define OATPP_MACRO_API_CONTROLLER_QUERY_3(TYPE, NAME, QUALIFIER, DEFAULT) \ @@ -216,12 +192,10 @@ if(__param_str_val_##NAME) { \ bool __param_validation_check_##NAME; \ NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ if(!__param_validation_check_##NAME){ \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, \ - oatpp::String("Invalid QUERY parameter '") + \ - QUALIFIER + \ - "'. Expected type is '" #TYPE "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, \ + oatpp::String("Invalid QUERY parameter '") + \ + QUALIFIER + \ + "'. Expected type is '" #TYPE "'"); \ } \ } @@ -261,16 +235,12 @@ if(getDefaultObjectMapper()) { \ #define OATPP_MACRO_API_CONTROLLER_BODY_DTO(TYPE, PARAM_LIST) \ if(!getDefaultObjectMapper()) { \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_500, "ObjectMapper was NOT set. Can't deserialize the request body."); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "ObjectMapper was NOT set. Can't deserialize the request body."); \ } \ const auto& OATPP_MACRO_FIRSTARG PARAM_LIST = \ __request->readBodyToDto(getDefaultObjectMapper().get()); \ if(!OATPP_MACRO_FIRSTARG PARAM_LIST) { \ - auto httpError = oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'"); \ - auto eptr = std::make_exception_ptr(httpError); \ - return ApiController::handleError(eptr); \ + throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'"); \ } // __INFO diff --git a/src/oatpp/web/server/api/ApiController.cpp b/src/oatpp/web/server/api/ApiController.cpp index beffc3b1..35e600d2 100644 --- a/src/oatpp/web/server/api/ApiController.cpp +++ b/src/oatpp/web/server/api/ApiController.cpp @@ -56,11 +56,17 @@ void ApiController::setErrorHandler(const std::shared_ptr } std::shared_ptr ApiController::handleError(const std::exception_ptr& exceptionPtr) const { + if(m_errorHandler) { return m_errorHandler->handleError(exceptionPtr); } - return nullptr; + if(exceptionPtr) { + std::rethrow_exception(exceptionPtr); + } + + throw std::runtime_error("[oatpp::web::server::api::ApiController::handleError()]: Error. 'exceptionPtr' is not set."); + } void ApiController::setDefaultAuthorizationHandler(const std::shared_ptr& authorizationHandler){ diff --git a/src/oatpp/web/server/api/ApiController.hpp b/src/oatpp/web/server/api/ApiController.hpp index 0703338b..3f979996 100644 --- a/src/oatpp/web/server/api/ApiController.hpp +++ b/src/oatpp/web/server/api/ApiController.hpp @@ -360,7 +360,9 @@ public: ApiController(const std::shared_ptr& defaultObjectMapper, const oatpp::String &routerPrefix = nullptr) : m_defaultObjectMapper(defaultObjectMapper) , m_routerPrefix(routerPrefix) - {} + { + + } public: template