Skip to content

Commit

Permalink
[C++][Pistache] Catch HttpError from user-provided handler (#6520)
Browse files Browse the repository at this point in the history
* [C++][Pistache] Catch HttpError when user-provided handler has thrown

This allows for returning valid http code through exception

* [C++][Pistache] Update Petstore sample
  • Loading branch information
muttleyxd authored Jun 5, 2020
1 parent d5ea62f commit c102ced
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand Down
29 changes: 29 additions & 0 deletions samples/server/petstore/cpp-pistache/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CMakeLists.txt
README.md
api/PetApi.cpp
api/PetApi.h
api/StoreApi.cpp
api/StoreApi.h
api/UserApi.cpp
api/UserApi.h
impl/PetApiImpl.cpp
impl/PetApiImpl.h
impl/StoreApiImpl.cpp
impl/StoreApiImpl.h
impl/UserApiImpl.cpp
impl/UserApiImpl.h
main-api-server.cpp
model/ApiResponse.cpp
model/ApiResponse.h
model/Category.cpp
model/Category.h
model/Helpers.cpp
model/Helpers.h
model/Order.cpp
model/Order.h
model/Pet.cpp
model/Pet.h
model/Tag.cpp
model/Tag.h
model/User.cpp
model/User.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.0-SNAPSHOT
5.0.0-SNAPSHOT
24 changes: 24 additions & 0 deletions samples/server/petstore/cpp-pistache/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -78,6 +81,9 @@ void PetApi::delete_pet_handler(const Pistache::Rest::Request &request, Pistache
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -103,6 +109,9 @@ void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request,
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -128,6 +137,9 @@ void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, P
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -145,6 +157,9 @@ void PetApi::get_pet_by_id_handler(const Pistache::Rest::Request &request, Pista
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -165,6 +180,9 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -179,6 +197,9 @@ void PetApi::update_pet_with_form_handler(const Pistache::Rest::Request &request
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -193,6 +214,9 @@ void PetApi::upload_file_handler(const Pistache::Rest::Request &request, Pistach
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand Down
12 changes: 12 additions & 0 deletions samples/server/petstore/cpp-pistache/api/StoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pist
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -66,6 +69,9 @@ void StoreApi::get_inventory_handler(const Pistache::Rest::Request &, Pistache::
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -83,6 +89,9 @@ void StoreApi::get_order_by_id_handler(const Pistache::Rest::Request &request, P
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -103,6 +112,9 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand Down
24 changes: 24 additions & 0 deletions samples/server/petstore/cpp-pistache/api/UserApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistac
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -77,6 +80,9 @@ void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Reques
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -96,6 +102,9 @@ void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -113,6 +122,9 @@ void UserApi::delete_user_handler(const Pistache::Rest::Request &request, Pistac
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -130,6 +142,9 @@ void UserApi::get_user_by_name_handler(const Pistache::Rest::Request &request, P
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand Down Expand Up @@ -163,6 +178,9 @@ void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistach
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -178,6 +196,9 @@ void UserApi::logout_user_handler(const Pistache::Rest::Request &, Pistache::Htt
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand All @@ -200,6 +221,9 @@ void UserApi::update_user_handler(const Pistache::Rest::Request &request, Pistac
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
Expand Down

0 comments on commit c102ced

Please sign in to comment.