Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++][Pistache] Add error handlers overload taking the response object #19314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@ private:
{{/operation}}
void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual std::pair<Pistache::Http::Code, std::string> handleParsingException(const std::exception& ex) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ void {{classname}}::setupRoutes() {
router->addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this));
}

void {{classname}}::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> {{classname}}::handleParsingException(const std::exception& ex) const noexcept
{
try {
Expand All @@ -46,6 +52,12 @@ std::pair<Pistache::Http::Code, std::string> {{classname}}::handleParsingExcepti
}
}

void {{classname}}::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> {{classname}}::handleOperationException(const std::exception& ex) const noexcept
{
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what());
Expand Down Expand Up @@ -102,8 +114,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
{{paramName}} = request.body();
{{/isPrimitiveType}}
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleParsingException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleParsingException(e, response);
return;
}

Expand All @@ -120,8 +131,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand Down
42 changes: 22 additions & 20 deletions samples/server/petstore/cpp-pistache-everything/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ void PetApi::setupRoutes() {
router->addCustomHandler(Routes::bind(&PetApi::pet_api_default_handler, this));
}

void PetApi::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> PetApi::handleParsingException(const std::exception& ex) const noexcept
{
try {
Expand All @@ -59,6 +65,12 @@ std::pair<Pistache::Http::Code, std::string> PetApi::handleParsingException(cons
}
}

void PetApi::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> PetApi::handleOperationException(const std::exception& ex) const noexcept
{
return std::make_pair(Pistache::Http::Code::Internal_Server_Error, ex.what());
Expand All @@ -76,8 +88,7 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H
nlohmann::json::parse(request.body()).get_to(pet);
pet.validate();
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleParsingException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleParsingException(e, response);
return;
}

Expand All @@ -87,8 +98,7 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -112,8 +122,7 @@ void PetApi::delete_pet_handler(const Pistache::Rest::Request &request, Pistache
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand Down Expand Up @@ -142,8 +151,7 @@ void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request,
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand Down Expand Up @@ -172,8 +180,7 @@ void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, P
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -194,8 +201,7 @@ void PetApi::get_pet_by_id_handler(const Pistache::Rest::Request &request, Pista
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -216,8 +222,7 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache
nlohmann::json::parse(request.body()).get_to(pet);
pet.validate();
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleParsingException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleParsingException(e, response);
return;
}

Expand All @@ -227,8 +232,7 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -246,8 +250,7 @@ void PetApi::update_pet_with_form_handler(const Pistache::Rest::Request &request
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -265,8 +268,7 @@ void PetApi::upload_file_handler(const Pistache::Rest::Request &request, Pistach
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand Down
14 changes: 14 additions & 0 deletions samples/server/petstore/cpp-pistache-everything/api/PetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,27 @@ class PetApi : public ApiBase {
void upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
void pet_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual std::pair<Pistache::Http::Code, std::string> handleParsingException(const std::exception& ex) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
Expand Down
27 changes: 17 additions & 10 deletions samples/server/petstore/cpp-pistache-everything/api/StoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ void StoreApi::setupRoutes() {
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 {
Expand All @@ -55,6 +61,12 @@ std::pair<Pistache::Http::Code, std::string> StoreApi::handleParsingException(co
}
}

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());
Expand All @@ -72,8 +84,7 @@ void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pist
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -92,8 +103,7 @@ void StoreApi::get_inventory_handler(const Pistache::Rest::Request &, Pistache::
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -114,8 +124,7 @@ void StoreApi::get_order_by_id_handler(const Pistache::Rest::Request &request, P
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand All @@ -136,8 +145,7 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista
nlohmann::json::parse(request.body()).get_to(order);
order.validate();
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleParsingException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleParsingException(e, response);
return;
}

Expand All @@ -147,8 +155,7 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
const std::pair<Pistache::Http::Code, std::string> errorInfo = this->handleOperationException(e);
response.send(errorInfo.first, errorInfo.second);
this->handleOperationException(e, response);
return;
}

Expand Down
14 changes: 14 additions & 0 deletions samples/server/petstore/cpp-pistache-everything/api/StoreApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,27 @@ class StoreApi : public ApiBase {
void place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
void store_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleParsingException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual std::pair<Pistache::Http::Code, std::string> handleParsingException(const std::exception& ex) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
/// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
/// </summary>
virtual void handleOperationException(const std::exception& ex, Pistache::Http::ResponseWriter &response) const noexcept;

/// <summary>
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
/// May be overridden to return custom error formats. This is called inside a catch block.
Expand Down
Loading
Loading