Skip to content

Commit e85e1f1

Browse files
authored
Separate exception types (#2930)
1 parent f550553 commit e85e1f1

28 files changed

+195
-111
lines changed

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
7979
try {
8080
this->{{operationIdSnakeCase}}(request, response);
8181
{{/vendorExtensions.x-codegen-pistache-isParsingSupported}}
82-
} catch (std::runtime_error & e) {
83-
//send a 400 error
84-
response.send(Pistache::Http::Code::Bad_Request, e.what());
85-
return;
82+
} catch (nlohmann::detail::exception &e) {
83+
//send a 400 error
84+
response.send(Pistache::Http::Code::Bad_Request, e.what());
85+
return;
86+
} catch (std::runtime_error &e) {
87+
//send a 500 error
88+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
89+
return;
8690
}
8791

8892
}

samples/server/petstore/cpp-pistache/api/PetApi.cpp

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
77
*
88
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
@@ -54,10 +54,14 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H
5454
try {
5555
nlohmann::json::parse(request.body()).get_to(body);
5656
this->add_pet(body, response);
57-
} catch (std::runtime_error & e) {
58-
//send a 400 error
59-
response.send(Pistache::Http::Code::Bad_Request, e.what());
60-
return;
57+
} catch (nlohmann::detail::exception &e) {
58+
//send a 400 error
59+
response.send(Pistache::Http::Code::Bad_Request, e.what());
60+
return;
61+
} catch (std::runtime_error &e) {
62+
//send a 500 error
63+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
64+
return;
6165
}
6266

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

7175
try {
7276
this->delete_pet(petId, apiKey, response);
73-
} catch (std::runtime_error & e) {
74-
//send a 400 error
75-
response.send(Pistache::Http::Code::Bad_Request, e.what());
76-
return;
77+
} catch (nlohmann::detail::exception &e) {
78+
//send a 400 error
79+
response.send(Pistache::Http::Code::Bad_Request, e.what());
80+
return;
81+
} catch (std::runtime_error &e) {
82+
//send a 500 error
83+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
84+
return;
7785
}
7886

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

92100
try {
93101
this->find_pets_by_status(status, response);
94-
} catch (std::runtime_error & e) {
95-
//send a 400 error
96-
response.send(Pistache::Http::Code::Bad_Request, e.what());
97-
return;
102+
} catch (nlohmann::detail::exception &e) {
103+
//send a 400 error
104+
response.send(Pistache::Http::Code::Bad_Request, e.what());
105+
return;
106+
} catch (std::runtime_error &e) {
107+
//send a 500 error
108+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
109+
return;
98110
}
99111

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

113125
try {
114126
this->find_pets_by_tags(tags, response);
115-
} catch (std::runtime_error & e) {
116-
//send a 400 error
117-
response.send(Pistache::Http::Code::Bad_Request, e.what());
118-
return;
127+
} catch (nlohmann::detail::exception &e) {
128+
//send a 400 error
129+
response.send(Pistache::Http::Code::Bad_Request, e.what());
130+
return;
131+
} catch (std::runtime_error &e) {
132+
//send a 500 error
133+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
134+
return;
119135
}
120136

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

126142
try {
127143
this->get_pet_by_id(petId, response);
128-
} catch (std::runtime_error & e) {
129-
//send a 400 error
130-
response.send(Pistache::Http::Code::Bad_Request, e.what());
131-
return;
144+
} catch (nlohmann::detail::exception &e) {
145+
//send a 400 error
146+
response.send(Pistache::Http::Code::Bad_Request, e.what());
147+
return;
148+
} catch (std::runtime_error &e) {
149+
//send a 500 error
150+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
151+
return;
132152
}
133153

134154
}
@@ -141,30 +161,42 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache
141161
try {
142162
nlohmann::json::parse(request.body()).get_to(body);
143163
this->update_pet(body, response);
144-
} catch (std::runtime_error & e) {
145-
//send a 400 error
146-
response.send(Pistache::Http::Code::Bad_Request, e.what());
147-
return;
164+
} catch (nlohmann::detail::exception &e) {
165+
//send a 400 error
166+
response.send(Pistache::Http::Code::Bad_Request, e.what());
167+
return;
168+
} catch (std::runtime_error &e) {
169+
//send a 500 error
170+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
171+
return;
148172
}
149173

150174
}
151175
void PetApi::update_pet_with_form_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
152176
try {
153177
this->update_pet_with_form(request, response);
154-
} catch (std::runtime_error & e) {
155-
//send a 400 error
156-
response.send(Pistache::Http::Code::Bad_Request, e.what());
157-
return;
178+
} catch (nlohmann::detail::exception &e) {
179+
//send a 400 error
180+
response.send(Pistache::Http::Code::Bad_Request, e.what());
181+
return;
182+
} catch (std::runtime_error &e) {
183+
//send a 500 error
184+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
185+
return;
158186
}
159187

160188
}
161189
void PetApi::upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
162190
try {
163191
this->upload_file(request, response);
164-
} catch (std::runtime_error & e) {
165-
//send a 400 error
166-
response.send(Pistache::Http::Code::Bad_Request, e.what());
167-
return;
192+
} catch (nlohmann::detail::exception &e) {
193+
//send a 400 error
194+
response.send(Pistache::Http::Code::Bad_Request, e.what());
195+
return;
196+
} catch (std::runtime_error &e) {
197+
//send a 500 error
198+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
199+
return;
168200
}
169201

170202
}

samples/server/petstore/cpp-pistache/api/PetApi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
77
*
88
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

samples/server/petstore/cpp-pistache/api/StoreApi.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
77
*
88
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
@@ -47,21 +47,29 @@ void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pist
4747

4848
try {
4949
this->delete_order(orderId, response);
50-
} catch (std::runtime_error & e) {
51-
//send a 400 error
52-
response.send(Pistache::Http::Code::Bad_Request, e.what());
53-
return;
50+
} catch (nlohmann::detail::exception &e) {
51+
//send a 400 error
52+
response.send(Pistache::Http::Code::Bad_Request, e.what());
53+
return;
54+
} catch (std::runtime_error &e) {
55+
//send a 500 error
56+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
57+
return;
5458
}
5559

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

5963
try {
6064
this->get_inventory(response);
61-
} catch (std::runtime_error & e) {
62-
//send a 400 error
63-
response.send(Pistache::Http::Code::Bad_Request, e.what());
64-
return;
65+
} catch (nlohmann::detail::exception &e) {
66+
//send a 400 error
67+
response.send(Pistache::Http::Code::Bad_Request, e.what());
68+
return;
69+
} catch (std::runtime_error &e) {
70+
//send a 500 error
71+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
72+
return;
6573
}
6674

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

7280
try {
7381
this->get_order_by_id(orderId, response);
74-
} catch (std::runtime_error & e) {
75-
//send a 400 error
76-
response.send(Pistache::Http::Code::Bad_Request, e.what());
77-
return;
82+
} catch (nlohmann::detail::exception &e) {
83+
//send a 400 error
84+
response.send(Pistache::Http::Code::Bad_Request, e.what());
85+
return;
86+
} catch (std::runtime_error &e) {
87+
//send a 500 error
88+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
89+
return;
7890
}
7991

8092
}
@@ -87,10 +99,14 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista
8799
try {
88100
nlohmann::json::parse(request.body()).get_to(body);
89101
this->place_order(body, response);
90-
} catch (std::runtime_error & e) {
91-
//send a 400 error
92-
response.send(Pistache::Http::Code::Bad_Request, e.what());
93-
return;
102+
} catch (nlohmann::detail::exception &e) {
103+
//send a 400 error
104+
response.send(Pistache::Http::Code::Bad_Request, e.what());
105+
return;
106+
} catch (std::runtime_error &e) {
107+
//send a 500 error
108+
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
109+
return;
94110
}
95111

96112
}

samples/server/petstore/cpp-pistache/api/StoreApi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* OpenAPI Petstore
33
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
44
*
5-
* OpenAPI spec version: 1.0.0
5+
* The version of the OpenAPI document: 1.0.0
66
*
77
*
88
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

0 commit comments

Comments
 (0)