Skip to content

Commit

Permalink
Separate exception types (#2930)
Browse files Browse the repository at this point in the history
  • Loading branch information
etherealjoy authored May 18, 2019
1 parent f550553 commit e85e1f1
Show file tree
Hide file tree
Showing 28 changed files with 195 additions and 111 deletions.
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

0 comments on commit e85e1f1

Please sign in to comment.