Sylvain Joubert 8af3ff2828
[C++][Pistache] Add error handlers overload taking the response object (#19314)
This allows overriders to have full access to the response object and
more finely control the error handling behavior.
For example, this enables the specification of a proper Content-Type in
case of custom format responses (application/json, ...)
2024-08-14 14:31:55 +08:00

98 lines
3.1 KiB
C++

/**
* Test swagger file
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
#include "StoreApi.h"
#include "Helpers.h"
namespace org::openapitools::server::api
{
using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model;
const std::string StoreApi::base = "";
StoreApi::StoreApi(const std::shared_ptr<Pistache::Rest::Router>& rtr)
: ApiBase(rtr)
{
}
void StoreApi::init() {
setupRoutes();
}
void StoreApi::setupRoutes() {
using namespace Pistache::Rest;
Routes::Get(*router, base + "/pet", Routes::bind(&StoreApi::get_nested_object_handler, this));
// Default handler, called when a route is not found
router->addCustomHandler(Routes::bind(&StoreApi::store_api_default_handler, this));
}
void StoreApi::handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept
{
std::pair<Pistache::Http::Code, std::string> codeAndError = handleParsingException(ex);
response.send(codeAndError.first, codeAndError.second);
}
std::pair<Pistache::Http::Code, std::string> StoreApi::handleParsingException(const std::exception& ex) const noexcept
{
try {
throw;
} catch (nlohmann::detail::exception &e) {
return std::make_pair(Pistache::Http::Code::Bad_Request, e.what());
} catch (org::openapitools::server::helpers::ValidationException &e) {
return std::make_pair(Pistache::Http::Code::Bad_Request, e.what());
} catch (std::exception &e) {
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, e.what());
}
}
void StoreApi::handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept
{
std::pair<Pistache::Http::Code, std::string> codeAndError = handleOperationException(ex);
response.send(codeAndError.first, codeAndError.second);
}
std::pair<Pistache::Http::Code, std::string> StoreApi::handleOperationException(const std::exception& ex) const noexcept
{
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what());
}
void StoreApi::get_nested_object_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {
try {
try {
this->get_nested_object(response);
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
this->handleOperationException(e, response);
return;
}
} catch (std::exception &e) {
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
}
}
void StoreApi::store_api_default_handler(const Pistache::Rest::Request &, Pistache::Http::ResponseWriter response) {
response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
}
} // namespace org::openapitools::server::api