Skip to content

Commit 8cd5a41

Browse files
Ramanthwing328
authored andcommitted
[R] feat(r): handling error response for 3xx cases (#3571)
* feat(r): handling error response for 3xx cases * fix(r) : changed the message for 3XX case * fix(r) : fixing the status code 3xx
1 parent a05f684 commit 8cd5a41

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

modules/openapi-generator/src/main/resources/r/api.mustache

+16
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@
291291
{{! Returning the ApiResponse object with NULL object when the endpoint doesn't return anything}}
292292
ApiResponse$new(NULL, resp)
293293
{{/returnType}}
294+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
295+
{{#returnExceptionOnFailure}}
296+
errorMsg <- toString(content(resp))
297+
if(errorMsg == ""){
298+
errorMsg <- paste("Server returned " , httr::status_code(resp) , " response status code.")
299+
}
300+
{{#useDefaultExceptionHandling}}
301+
stop(errorMsg)
302+
{{/useDefaultExceptionHandling}}
303+
{{#useRlangExceptionHandling}}
304+
rlang::abort(message = errorMsg, .subclass = "ApiException", ApiException = ApiException$new(http_response = resp))
305+
{{/useRlangExceptionHandling}}
306+
{{/returnExceptionOnFailure}}
307+
{{^returnExceptionOnFailure}}
308+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
309+
{{/returnExceptionOnFailure}}
294310
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
295311
{{#returnExceptionOnFailure}}
296312
errorMsg <- toString(content(resp))

samples/client/petstore/R/R/pet_api.R

+16
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ PetApi <- R6::R6Class(
371371

372372
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
373373
ApiResponse$new(NULL, resp)
374+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
375+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
374376
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
375377
ApiResponse$new("API client error", resp)
376378
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -417,6 +419,8 @@ PetApi <- R6::R6Class(
417419

418420
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
419421
ApiResponse$new(NULL, resp)
422+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
423+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
420424
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
421425
ApiResponse$new("API client error", resp)
422426
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -465,6 +469,8 @@ PetApi <- R6::R6Class(
465469
}
466470
)
467471
ApiResponse$new(deserializedRespObj, resp)
472+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
473+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
468474
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
469475
ApiResponse$new("API client error", resp)
470476
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -513,6 +519,8 @@ PetApi <- R6::R6Class(
513519
}
514520
)
515521
ApiResponse$new(deserializedRespObj, resp)
522+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
523+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
516524
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
517525
ApiResponse$new("API client error", resp)
518526
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -565,6 +573,8 @@ PetApi <- R6::R6Class(
565573
}
566574
)
567575
ApiResponse$new(deserializedRespObj, resp)
576+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
577+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
568578
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
569579
ApiResponse$new("API client error", resp)
570580
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -611,6 +621,8 @@ PetApi <- R6::R6Class(
611621

612622
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
613623
ApiResponse$new(NULL, resp)
624+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
625+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
614626
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
615627
ApiResponse$new("API client error", resp)
616628
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -660,6 +672,8 @@ PetApi <- R6::R6Class(
660672

661673
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
662674
ApiResponse$new(NULL, resp)
675+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
676+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
663677
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
664678
ApiResponse$new("API client error", resp)
665679
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -715,6 +729,8 @@ PetApi <- R6::R6Class(
715729
}
716730
)
717731
ApiResponse$new(deserializedRespObj, resp)
732+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
733+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
718734
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
719735
ApiResponse$new("API client error", resp)
720736
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {

samples/client/petstore/R/R/store_api.R

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ StoreApi <- R6::R6Class(
210210

211211
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
212212
ApiResponse$new(NULL, resp)
213+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
214+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
213215
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
214216
ApiResponse$new("API client error", resp)
215217
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -254,6 +256,8 @@ StoreApi <- R6::R6Class(
254256
}
255257
)
256258
ApiResponse$new(deserializedRespObj, resp)
259+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
260+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
257261
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
258262
ApiResponse$new("API client error", resp)
259263
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -302,6 +306,8 @@ StoreApi <- R6::R6Class(
302306
}
303307
)
304308
ApiResponse$new(deserializedRespObj, resp)
309+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
310+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
305311
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
306312
ApiResponse$new("API client error", resp)
307313
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -352,6 +358,8 @@ StoreApi <- R6::R6Class(
352358
}
353359
)
354360
ApiResponse$new(deserializedRespObj, resp)
361+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
362+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
355363
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
356364
ApiResponse$new("API client error", resp)
357365
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {

samples/client/petstore/R/R/user_api.R

+16
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ UserApi <- R6::R6Class(
329329

330330
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
331331
ApiResponse$new(NULL, resp)
332+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
333+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
332334
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
333335
ApiResponse$new("API client error", resp)
334336
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -374,6 +376,8 @@ UserApi <- R6::R6Class(
374376

375377
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
376378
ApiResponse$new(NULL, resp)
379+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
380+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
377381
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
378382
ApiResponse$new("API client error", resp)
379383
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -419,6 +423,8 @@ UserApi <- R6::R6Class(
419423

420424
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
421425
ApiResponse$new(NULL, resp)
426+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
427+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
422428
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
423429
ApiResponse$new("API client error", resp)
424430
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -461,6 +467,8 @@ UserApi <- R6::R6Class(
461467

462468
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
463469
ApiResponse$new(NULL, resp)
470+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
471+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
464472
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
465473
ApiResponse$new("API client error", resp)
466474
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -509,6 +517,8 @@ UserApi <- R6::R6Class(
509517
}
510518
)
511519
ApiResponse$new(deserializedRespObj, resp)
520+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
521+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
512522
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
513523
ApiResponse$new("API client error", resp)
514524
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -561,6 +571,8 @@ UserApi <- R6::R6Class(
561571
}
562572
)
563573
ApiResponse$new(deserializedRespObj, resp)
574+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
575+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
564576
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
565577
ApiResponse$new("API client error", resp)
566578
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -595,6 +607,8 @@ UserApi <- R6::R6Class(
595607

596608
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
597609
ApiResponse$new(NULL, resp)
610+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
611+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
598612
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
599613
ApiResponse$new("API client error", resp)
600614
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
@@ -647,6 +661,8 @@ UserApi <- R6::R6Class(
647661

648662
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
649663
ApiResponse$new(NULL, resp)
664+
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
665+
ApiResponse$new(paste("Server returned " , httr::status_code(resp) , " response status code."), resp)
650666
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
651667
ApiResponse$new("API client error", resp)
652668
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {

0 commit comments

Comments
 (0)