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] Separate exception types when handling requests #2930

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 @@ -79,10 +79,14 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
try {
this->{{operationIdSnakeCase}}(request, response);
{{/vendorExtensions.x-codegen-pistache-isParsingSupported}}
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand Down
98 changes: 65 additions & 33 deletions samples/server/petstore/cpp-pistache/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Expand Down Expand Up @@ -54,10 +54,14 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H
try {
nlohmann::json::parse(request.body()).get_to(body);
this->add_pet(body, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -70,10 +74,14 @@ void PetApi::delete_pet_handler(const Pistache::Rest::Request &request, Pistache

try {
this->delete_pet(petId, apiKey, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -91,10 +99,14 @@ void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request,

try {
this->find_pets_by_status(status, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -112,10 +124,14 @@ void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, P

try {
this->find_pets_by_tags(tags, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -125,10 +141,14 @@ void PetApi::get_pet_by_id_handler(const Pistache::Rest::Request &request, Pista

try {
this->get_pet_by_id(petId, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -141,30 +161,42 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache
try {
nlohmann::json::parse(request.body()).get_to(body);
this->update_pet(body, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
void PetApi::update_pet_with_form_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
try {
this->update_pet_with_form(request, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
void PetApi::upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
try {
this->upload_file(request, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand Down
2 changes: 1 addition & 1 deletion samples/server/petstore/cpp-pistache/api/PetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Expand Down
50 changes: 33 additions & 17 deletions samples/server/petstore/cpp-pistache/api/StoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Expand Down Expand Up @@ -47,21 +47,29 @@ void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pist

try {
this->delete_order(orderId, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
void StoreApi::get_inventory_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {

try {
this->get_inventory(response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -71,10 +79,14 @@ void StoreApi::get_order_by_id_handler(const Pistache::Rest::Request &request, P

try {
this->get_order_by_id(orderId, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand All @@ -87,10 +99,14 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista
try {
nlohmann::json::parse(request.body()).get_to(body);
this->place_order(body, response);
} catch (std::runtime_error & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (std::runtime_error &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}

}
Expand Down
2 changes: 1 addition & 1 deletion samples/server/petstore/cpp-pistache/api/StoreApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
Expand Down
Loading