Skip to content

Commit c12673a

Browse files
committed
fix error handling for 4xx, 5xx returned by server
1 parent 2f5f2b3 commit c12673a

File tree

6 files changed

+132
-254
lines changed

6 files changed

+132
-254
lines changed

modules/swagger-codegen/src/main/resources/csharp/api.mustache

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,13 @@ namespace {{package}} {
7070
{{#bodyParam}}_request.AddParameter("application/json", ApiInvoker.Serialize({{paramName}}), ParameterType.RequestBody); // http body (model) parameter
7171
{{/bodyParam}}
7272

73-
try {
74-
// make the HTTP request
75-
{{#returnType}}IRestResponse response = restClient.Execute(_request);
76-
return ({{{returnType}}}) ApiInvoker.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}restClient.Execute(_request);
77-
return;{{/returnType}}
78-
} catch (Exception ex) {
79-
if(ex != null) {
80-
return {{#returnType}}null{{/returnType}};
81-
}
82-
else {
83-
throw ex;
84-
}
73+
// make the HTTP request
74+
IRestResponse response = restClient.Execute(_request);
75+
if (((int)response.StatusCode) >= 400) {
76+
throw new ApiException ((int)response.StatusCode, "Error calling {{nickname}}: " + response.Content);
8577
}
78+
{{#returnType}}return ({{{returnType}}}) ApiInvoker.Deserialize(response.Content, typeof({{{returnType}}}));{{/returnType}}{{^returnType}}
79+
return;{{/returnType}}
8680
}
8781
{{/operation}}
8882
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
using System;
22

33
namespace {{invokerPackage}} {
4+
45
public class ApiException : Exception {
5-
6-
private int errorCode = 0;
76
8-
public ApiException() {}
7+
public int ErrorCode { get; set; }
98

10-
public int ErrorCode {
11-
get
12-
{
13-
return errorCode;
14-
}
15-
}
9+
public ApiException() {}
1610

1711
public ApiException(int errorCode, string message) : base(message) {
18-
this.errorCode = errorCode;
12+
this.ErrorCode = errorCode;
1913
}
14+
2015
}
21-
}
16+
17+
}

samples/client/petstore/csharp/src/main/csharp/io/swagger/Api/PetApi.cs

Lines changed: 45 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,13 @@ public void UpdatePet (Pet Body) {
6060
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
6161

6262

63-
try {
64-
// make the HTTP request
65-
restClient.Execute(_request);
66-
return;
67-
} catch (Exception ex) {
68-
if(ex != null) {
69-
return ;
70-
}
71-
else {
72-
throw ex;
73-
}
63+
// make the HTTP request
64+
IRestResponse response = restClient.Execute(_request);
65+
if (((int)response.StatusCode) >= 400) {
66+
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePet: " + response.Content);
7467
}
68+
69+
return;
7570
}
7671

7772

@@ -100,18 +95,13 @@ public void AddPet (Pet Body) {
10095
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
10196

10297

103-
try {
104-
// make the HTTP request
105-
restClient.Execute(_request);
106-
return;
107-
} catch (Exception ex) {
108-
if(ex != null) {
109-
return ;
110-
}
111-
else {
112-
throw ex;
113-
}
98+
// make the HTTP request
99+
IRestResponse response = restClient.Execute(_request);
100+
if (((int)response.StatusCode) >= 400) {
101+
throw new ApiException ((int)response.StatusCode, "Error calling AddPet: " + response.Content);
114102
}
103+
104+
return;
115105
}
116106

117107

@@ -140,18 +130,12 @@ public List<Pet> FindPetsByStatus (List<string> Status) {
140130

141131

142132

143-
try {
144-
// make the HTTP request
145-
IRestResponse response = restClient.Execute(_request);
146-
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
147-
} catch (Exception ex) {
148-
if(ex != null) {
149-
return null;
150-
}
151-
else {
152-
throw ex;
153-
}
133+
// make the HTTP request
134+
IRestResponse response = restClient.Execute(_request);
135+
if (((int)response.StatusCode) >= 400) {
136+
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByStatus: " + response.Content);
154137
}
138+
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
155139
}
156140

157141

@@ -180,18 +164,12 @@ public List<Pet> FindPetsByTags (List<string> Tags) {
180164

181165

182166

183-
try {
184-
// make the HTTP request
185-
IRestResponse response = restClient.Execute(_request);
186-
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
187-
} catch (Exception ex) {
188-
if(ex != null) {
189-
return null;
190-
}
191-
else {
192-
throw ex;
193-
}
167+
// make the HTTP request
168+
IRestResponse response = restClient.Execute(_request);
169+
if (((int)response.StatusCode) >= 400) {
170+
throw new ApiException ((int)response.StatusCode, "Error calling FindPetsByTags: " + response.Content);
194171
}
172+
return (List<Pet>) ApiInvoker.Deserialize(response.Content, typeof(List<Pet>));
195173
}
196174

197175

@@ -223,18 +201,12 @@ public Pet GetPetById (long? PetId) {
223201

224202

225203

226-
try {
227-
// make the HTTP request
228-
IRestResponse response = restClient.Execute(_request);
229-
return (Pet) ApiInvoker.Deserialize(response.Content, typeof(Pet));
230-
} catch (Exception ex) {
231-
if(ex != null) {
232-
return null;
233-
}
234-
else {
235-
throw ex;
236-
}
204+
// make the HTTP request
205+
IRestResponse response = restClient.Execute(_request);
206+
if (((int)response.StatusCode) >= 400) {
207+
throw new ApiException ((int)response.StatusCode, "Error calling GetPetById: " + response.Content);
237208
}
209+
return (Pet) ApiInvoker.Deserialize(response.Content, typeof(Pet));
238210
}
239211

240212

@@ -270,18 +242,13 @@ public void UpdatePetWithForm (string PetId, string Name, string Status) {
270242

271243

272244

273-
try {
274-
// make the HTTP request
275-
restClient.Execute(_request);
276-
return;
277-
} catch (Exception ex) {
278-
if(ex != null) {
279-
return ;
280-
}
281-
else {
282-
throw ex;
283-
}
245+
// make the HTTP request
246+
IRestResponse response = restClient.Execute(_request);
247+
if (((int)response.StatusCode) >= 400) {
248+
throw new ApiException ((int)response.StatusCode, "Error calling UpdatePetWithForm: " + response.Content);
284249
}
250+
251+
return;
285252
}
286253

287254

@@ -315,18 +282,13 @@ public void DeletePet (string ApiKey, long? PetId) {
315282

316283

317284

318-
try {
319-
// make the HTTP request
320-
restClient.Execute(_request);
321-
return;
322-
} catch (Exception ex) {
323-
if(ex != null) {
324-
return ;
325-
}
326-
else {
327-
throw ex;
328-
}
285+
// make the HTTP request
286+
IRestResponse response = restClient.Execute(_request);
287+
if (((int)response.StatusCode) >= 400) {
288+
throw new ApiException ((int)response.StatusCode, "Error calling DeletePet: " + response.Content);
329289
}
290+
291+
return;
330292
}
331293

332294

@@ -362,18 +324,13 @@ public void UploadFile (long? PetId, string AdditionalMetadata, string File) {
362324

363325

364326

365-
try {
366-
// make the HTTP request
367-
restClient.Execute(_request);
368-
return;
369-
} catch (Exception ex) {
370-
if(ex != null) {
371-
return ;
372-
}
373-
else {
374-
throw ex;
375-
}
327+
// make the HTTP request
328+
IRestResponse response = restClient.Execute(_request);
329+
if (((int)response.StatusCode) >= 400) {
330+
throw new ApiException ((int)response.StatusCode, "Error calling UploadFile: " + response.Content);
376331
}
332+
333+
return;
377334
}
378335

379336
}

samples/client/petstore/csharp/src/main/csharp/io/swagger/Api/StoreApi.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,12 @@ public String GetBasePath() {
5858

5959

6060

61-
try {
62-
// make the HTTP request
63-
IRestResponse response = restClient.Execute(_request);
64-
return (Dictionary<String, int?>) ApiInvoker.Deserialize(response.Content, typeof(Dictionary<String, int?>));
65-
} catch (Exception ex) {
66-
if(ex != null) {
67-
return null;
68-
}
69-
else {
70-
throw ex;
71-
}
61+
// make the HTTP request
62+
IRestResponse response = restClient.Execute(_request);
63+
if (((int)response.StatusCode) >= 400) {
64+
throw new ApiException ((int)response.StatusCode, "Error calling GetInventory: " + response.Content);
7265
}
66+
return (Dictionary<String, int?>) ApiInvoker.Deserialize(response.Content, typeof(Dictionary<String, int?>));
7367
}
7468

7569

@@ -98,18 +92,12 @@ public Order PlaceOrder (Order Body) {
9892
_request.AddParameter("application/json", ApiInvoker.Serialize(Body), ParameterType.RequestBody); // http body (model) parameter
9993

10094

101-
try {
102-
// make the HTTP request
103-
IRestResponse response = restClient.Execute(_request);
104-
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
105-
} catch (Exception ex) {
106-
if(ex != null) {
107-
return null;
108-
}
109-
else {
110-
throw ex;
111-
}
95+
// make the HTTP request
96+
IRestResponse response = restClient.Execute(_request);
97+
if (((int)response.StatusCode) >= 400) {
98+
throw new ApiException ((int)response.StatusCode, "Error calling PlaceOrder: " + response.Content);
11299
}
100+
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
113101
}
114102

115103

@@ -141,18 +129,12 @@ public Order GetOrderById (string OrderId) {
141129

142130

143131

144-
try {
145-
// make the HTTP request
146-
IRestResponse response = restClient.Execute(_request);
147-
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
148-
} catch (Exception ex) {
149-
if(ex != null) {
150-
return null;
151-
}
152-
else {
153-
throw ex;
154-
}
132+
// make the HTTP request
133+
IRestResponse response = restClient.Execute(_request);
134+
if (((int)response.StatusCode) >= 400) {
135+
throw new ApiException ((int)response.StatusCode, "Error calling GetOrderById: " + response.Content);
155136
}
137+
return (Order) ApiInvoker.Deserialize(response.Content, typeof(Order));
156138
}
157139

158140

@@ -184,18 +166,13 @@ public void DeleteOrder (string OrderId) {
184166

185167

186168

187-
try {
188-
// make the HTTP request
189-
restClient.Execute(_request);
190-
return;
191-
} catch (Exception ex) {
192-
if(ex != null) {
193-
return ;
194-
}
195-
else {
196-
throw ex;
197-
}
169+
// make the HTTP request
170+
IRestResponse response = restClient.Execute(_request);
171+
if (((int)response.StatusCode) >= 400) {
172+
throw new ApiException ((int)response.StatusCode, "Error calling DeleteOrder: " + response.Content);
198173
}
174+
175+
return;
199176
}
200177

201178
}

0 commit comments

Comments
 (0)