From 3fcbf17536aebe77d4d8e3f25472e1248aa79335 Mon Sep 17 00:00:00 2001
From: Julian G <23147553+JulianGmp@users.noreply.github.com>
Date: Tue, 15 Jun 2021 08:42:29 +0200
Subject: [PATCH] [C++][Pistache] Fix compile break, error handling (#9742)
* fix compilation break with validate function
* fix error handling in handleParsingException
bug caused all errors to be regarded as an internal server error
* generate samples
---
.../resources/cpp-pistache-server/api-header.mustache | 6 ++++--
.../resources/cpp-pistache-server/api-source.mustache | 4 +++-
.../resources/cpp-pistache-server/model-header.mustache | 9 ++++++---
samples/server/petstore/cpp-pistache/api/PetApi.cpp | 4 +++-
samples/server/petstore/cpp-pistache/api/PetApi.h | 6 ++++--
samples/server/petstore/cpp-pistache/api/StoreApi.cpp | 4 +++-
samples/server/petstore/cpp-pistache/api/StoreApi.h | 6 ++++--
samples/server/petstore/cpp-pistache/api/UserApi.cpp | 4 +++-
samples/server/petstore/cpp-pistache/api/UserApi.h | 6 ++++--
samples/server/petstore/cpp-pistache/model/ApiResponse.h | 9 ++++++---
samples/server/petstore/cpp-pistache/model/Category.h | 9 ++++++---
samples/server/petstore/cpp-pistache/model/Order.h | 9 ++++++---
samples/server/petstore/cpp-pistache/model/Pet.h | 9 ++++++---
samples/server/petstore/cpp-pistache/model/Tag.h | 9 ++++++---
samples/server/petstore/cpp-pistache/model/User.h | 9 ++++++---
15 files changed, 70 insertions(+), 33 deletions(-)
diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache
index f614d478e9..c79979173f 100644
--- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache
+++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache
@@ -42,13 +42,15 @@ private:
///
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleParsingException(const std::exception& ex) const noexcept;
///
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleOperationException(const std::exception& ex) const noexcept;
diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache
index 9814ba0cd6..dad2eb75c6 100644
--- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache
+++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache
@@ -36,11 +36,13 @@ void {{classname}}::setupRoutes() {
std::pair {{classname}}::handleParsingException(const std::exception& ex) const noexcept
{
try {
- throw ex;
+ throw;
} catch (nlohmann::detail::exception &e) {
return std::make_pair(Pistache::Http::Code::Bad_Request, e.what());
} catch ({{helpersNamespace}}::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())
}
}
diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache
index cb6d8d98ee..656109b155 100644
--- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache
+++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const {{classname}}& rhs) const;
bool operator!=(const {{classname}}& rhs) const;
@@ -77,9 +83,6 @@ protected:
{{#isEnum}}
{{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED;
{{/isEnum}}
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace {{modelNamespace}}
diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp
index 7b3641599e..a05048ec79 100644
--- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp
+++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp
@@ -49,11 +49,13 @@ void PetApi::setupRoutes() {
std::pair PetApi::handleParsingException(const std::exception& ex) const noexcept
{
try {
- throw ex;
+ 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())
}
}
diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.h b/samples/server/petstore/cpp-pistache/api/PetApi.h
index f689a0675e..3f790af02c 100644
--- a/samples/server/petstore/cpp-pistache/api/PetApi.h
+++ b/samples/server/petstore/cpp-pistache/api/PetApi.h
@@ -58,13 +58,15 @@ private:
///
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleParsingException(const std::exception& ex) const noexcept;
///
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleOperationException(const std::exception& ex) const noexcept;
diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp
index ea0c0e1793..480bdcc033 100644
--- a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp
+++ b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp
@@ -45,11 +45,13 @@ void StoreApi::setupRoutes() {
std::pair StoreApi::handleParsingException(const std::exception& ex) const noexcept
{
try {
- throw ex;
+ 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())
}
}
diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.h b/samples/server/petstore/cpp-pistache/api/StoreApi.h
index 6eb2243874..e354a2710f 100644
--- a/samples/server/petstore/cpp-pistache/api/StoreApi.h
+++ b/samples/server/petstore/cpp-pistache/api/StoreApi.h
@@ -54,13 +54,15 @@ private:
///
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleParsingException(const std::exception& ex) const noexcept;
///
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleOperationException(const std::exception& ex) const noexcept;
diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp
index 94903581dc..7b67dd0846 100644
--- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp
+++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp
@@ -49,11 +49,13 @@ void UserApi::setupRoutes() {
std::pair UserApi::handleParsingException(const std::exception& ex) const noexcept
{
try {
- throw ex;
+ 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())
}
}
diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h
index 003b6db4df..fadf0b046c 100644
--- a/samples/server/petstore/cpp-pistache/api/UserApi.h
+++ b/samples/server/petstore/cpp-pistache/api/UserApi.h
@@ -58,13 +58,15 @@ private:
///
/// Helper function to handle unexpected Exceptions during Parameter parsing and validation.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleParsingException(const std::exception& ex) const noexcept;
///
/// Helper function to handle unexpected Exceptions during processing of the request in handler functions.
- /// May be overriden to return custom error formats.
+ /// May be overriden to return custom error formats. This is called inside a catch block.
+ /// Important: When overriding, do not call `throw ex;`, but instead use `throw;`.
///
virtual std::pair handleOperationException(const std::exception& ex) const noexcept;
diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.h b/samples/server/petstore/cpp-pistache/model/ApiResponse.h
index d963b76650..c4227b2187 100644
--- a/samples/server/petstore/cpp-pistache/model/ApiResponse.h
+++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.h
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const ApiResponse& rhs) const;
bool operator!=(const ApiResponse& rhs) const;
@@ -83,9 +89,6 @@ protected:
bool m_TypeIsSet;
std::string m_Message;
bool m_MessageIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model
diff --git a/samples/server/petstore/cpp-pistache/model/Category.h b/samples/server/petstore/cpp-pistache/model/Category.h
index 1929ce33e9..71682c36c3 100644
--- a/samples/server/petstore/cpp-pistache/model/Category.h
+++ b/samples/server/petstore/cpp-pistache/model/Category.h
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const Category& rhs) const;
bool operator!=(const Category& rhs) const;
@@ -74,9 +80,6 @@ protected:
bool m_IdIsSet;
std::string m_Name;
bool m_NameIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model
diff --git a/samples/server/petstore/cpp-pistache/model/Order.h b/samples/server/petstore/cpp-pistache/model/Order.h
index 3d28e1eef6..86a6e35f87 100644
--- a/samples/server/petstore/cpp-pistache/model/Order.h
+++ b/samples/server/petstore/cpp-pistache/model/Order.h
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const Order& rhs) const;
bool operator!=(const Order& rhs) const;
@@ -110,9 +116,6 @@ protected:
bool m_StatusIsSet;
bool m_Complete;
bool m_CompleteIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model
diff --git a/samples/server/petstore/cpp-pistache/model/Pet.h b/samples/server/petstore/cpp-pistache/model/Pet.h
index 1cd841a08f..af773cb3e5 100644
--- a/samples/server/petstore/cpp-pistache/model/Pet.h
+++ b/samples/server/petstore/cpp-pistache/model/Pet.h
@@ -49,6 +49,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const Pet& rhs) const;
bool operator!=(const Pet& rhs) const;
@@ -109,9 +115,6 @@ protected:
bool m_TagsIsSet;
std::string m_Status;
bool m_StatusIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model
diff --git a/samples/server/petstore/cpp-pistache/model/Tag.h b/samples/server/petstore/cpp-pistache/model/Tag.h
index ddebeadae4..1b607e27fd 100644
--- a/samples/server/petstore/cpp-pistache/model/Tag.h
+++ b/samples/server/petstore/cpp-pistache/model/Tag.h
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const Tag& rhs) const;
bool operator!=(const Tag& rhs) const;
@@ -74,9 +80,6 @@ protected:
bool m_IdIsSet;
std::string m_Name;
bool m_NameIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model
diff --git a/samples/server/petstore/cpp-pistache/model/User.h b/samples/server/petstore/cpp-pistache/model/User.h
index 02bd5f4230..38048fe58e 100644
--- a/samples/server/petstore/cpp-pistache/model/User.h
+++ b/samples/server/petstore/cpp-pistache/model/User.h
@@ -46,6 +46,12 @@ public:
///
bool validate(std::stringstream& msg) const;
+ ///
+ /// Helper overload for validate. Used when one model stores another model and calls it's validate.
+ /// Not meant to be called outside that case.
+ ///
+ bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
+
bool operator==(const User& rhs) const;
bool operator!=(const User& rhs) const;
@@ -128,9 +134,6 @@ protected:
bool m_PhoneIsSet;
int32_t m_UserStatus;
bool m_UserStatusIsSet;
-
- // Helper overload for validate. Used when one model stores another model and calls it's validate.
- bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
};
} // namespace org::openapitools::server::model