diff --git a/modules/openapi-generator/src/main/resources/r/api_doc.mustache b/modules/openapi-generator/src/main/resources/r/api_doc.mustache index 17c3bdc1e91..a9604172115 100644 --- a/modules/openapi-generator/src/main/resources/r/api_doc.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_doc.mustache @@ -68,16 +68,20 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + {{#errorObjectType}} + # error object + dput(result$ApiException$error_object) + {{/errorObjectType}} } else { {{#returnType}} # deserialized response object - response.object <- result$content + dput(result$content) {{/returnType}} # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } {{/useRlangExceptionHandling}} {{/returnExceptionOnFailure}} diff --git a/modules/openapi-generator/src/main/resources/r/api_exception.mustache b/modules/openapi-generator/src/main/resources/r/api_exception.mustache index 76473661503..64aa6c3d76a 100644 --- a/modules/openapi-generator/src/main/resources/r/api_exception.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_exception.mustache @@ -9,7 +9,7 @@ #' @field body Body of the http response #' @field headers Headers of the http response {{#errorObjectType}} -#' @field errorObject error object type +#' @field error_object error object type {{/errorObjectType}} #' @export ApiException <- R6::R6Class( @@ -20,7 +20,7 @@ ApiException <- R6::R6Class( body = NULL, headers = NULL, {{#errorObjectType}} - errorObject = NULL, + error_object = NULL, {{/errorObjectType}} #' Initialize a new ApiException class. #' @@ -42,7 +42,7 @@ ApiException <- R6::R6Class( self$headers <- http_response$headers self$reason <- http_response$http_status_desc {{#errorObjectType}} - self$errorObject <- {{errorObjectType}}$new()$fromJSONString(http_response$response) + self$error_object <- {{errorObjectType}}$new()$fromJSONString(http_response$response) {{/errorObjectType}} } else { self$status <- status @@ -50,7 +50,7 @@ ApiException <- R6::R6Class( self$body <- NULL self$headers <- NULL {{#errorObjectType}} - self$errorObject <- NULL + self$error_object <- NULL {{/errorObjectType}} } }, @@ -76,9 +76,9 @@ ApiException <- R6::R6Class( errorMsg <- paste(errorMsg, self$body, "\n") } {{#errorObjectType}} - if (!is.null(self$errorObject)) { + if (!is.null(self$error_object)) { errorMsg <- paste(errorMsg, "Error object : ", "\n", sep = "") - errorMsg <- paste(errorMsg, self$errorObject$toJSONString(), "\n") + errorMsg <- paste(errorMsg, self$error_object$toJSONString(), "\n") } {{/errorObjectType}} errorMsg diff --git a/samples/client/petstore/R/R/api_exception.R b/samples/client/petstore/R/R/api_exception.R index e198854bf34..77b1d26d111 100644 --- a/samples/client/petstore/R/R/api_exception.R +++ b/samples/client/petstore/R/R/api_exception.R @@ -14,7 +14,7 @@ #' @field reason Reason of the ApiException #' @field body Body of the http response #' @field headers Headers of the http response -#' @field errorObject error object type +#' @field error_object error object type #' @export ApiException <- R6::R6Class( "ApiException", @@ -23,7 +23,7 @@ ApiException <- R6::R6Class( reason = NULL, body = NULL, headers = NULL, - errorObject = NULL, + error_object = NULL, #' Initialize a new ApiException class. #' #' @description @@ -43,13 +43,13 @@ ApiException <- R6::R6Class( self$body <- errorMsg self$headers <- http_response$headers self$reason <- http_response$http_status_desc - self$errorObject <- ModelApiResponse$new()$fromJSONString(http_response$response) + self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$response) } else { self$status <- status self$reason <- reason self$body <- NULL self$headers <- NULL - self$errorObject <- NULL + self$error_object <- NULL } }, #' Returns the string format of ApiException. @@ -73,9 +73,9 @@ ApiException <- R6::R6Class( errorMsg <- paste(errorMsg, "Body : ", "\n", sep = "") errorMsg <- paste(errorMsg, self$body, "\n") } - if (!is.null(self$errorObject)) { + if (!is.null(self$error_object)) { errorMsg <- paste(errorMsg, "Error object : ", "\n", sep = "") - errorMsg <- paste(errorMsg, self$errorObject$toJSONString(), "\n") + errorMsg <- paste(errorMsg, self$error_object$toJSONString(), "\n") } errorMsg } diff --git a/samples/client/petstore/R/docs/FakeApi.md b/samples/client/petstore/R/docs/FakeApi.md index 484ca98a1d6..558584fe697 100644 --- a/samples/client/petstore/R/docs/FakeApi.md +++ b/samples/client/petstore/R/docs/FakeApi.md @@ -31,14 +31,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` diff --git a/samples/client/petstore/R/docs/PetApi.md b/samples/client/petstore/R/docs/PetApi.md index f094099bd54..0a156275691 100644 --- a/samples/client/petstore/R/docs/PetApi.md +++ b/samples/client/petstore/R/docs/PetApi.md @@ -41,14 +41,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -101,12 +103,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -160,14 +164,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -221,14 +227,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -282,14 +290,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -346,14 +356,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -408,14 +420,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -471,12 +485,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -533,14 +549,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` diff --git a/samples/client/petstore/R/docs/StoreApi.md b/samples/client/petstore/R/docs/StoreApi.md index 31343a84ea0..b7910812681 100644 --- a/samples/client/petstore/R/docs/StoreApi.md +++ b/samples/client/petstore/R/docs/StoreApi.md @@ -31,12 +31,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -89,14 +91,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -144,14 +148,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -204,14 +210,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` diff --git a/samples/client/petstore/R/docs/UserApi.md b/samples/client/petstore/R/docs/UserApi.md index 3382f734cef..a9d8a646d90 100644 --- a/samples/client/petstore/R/docs/UserApi.md +++ b/samples/client/petstore/R/docs/UserApi.md @@ -37,12 +37,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -93,12 +95,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -149,12 +153,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -205,12 +211,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -262,14 +270,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -323,14 +333,16 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # deserialized response object - response.object <- result$content + dput(result$content) # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -382,12 +394,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` @@ -436,12 +450,14 @@ result <- tryCatch( ) # In case of error, print the error object if (!is.null(result$ApiException)) { - cat(result$ApiException$toString()) + dput(result$ApiException) + # error object + dput(result$ApiException$error_object) } else { # response headers - response.headers <- result$response$headers + dput(result$response$headers) # response status code - response.status.code <- result$response$status_code + dput(result$response$status_code) } ``` diff --git a/samples/client/petstore/R/test_petstore.R b/samples/client/petstore/R/test_petstore.R index f5c7843bc4f..40b2f358802 100644 --- a/samples/client/petstore/R/test_petstore.R +++ b/samples/client/petstore/R/test_petstore.R @@ -49,27 +49,28 @@ dput(result) ##a <- ModelApiResponse$new()$fromJSONString(errorMsg) ##dput(a) ## -##var_pet_id <- 1231256 # integer | ID of pet to return -## -###Find pet by ID -##api_instance <- PetApi$new() -### Configure API key authorization: api_key -##api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; -##result <- tryCatch( -## api_instance$GetPetById(var_pet_id), -## ApiException = function(ex) ex -## ) -### In case of error, print the error object -##if(!is.null(result$ApiException)) { -## cat(result$ApiException$toString()) -##} else { -## # deserialized response object -## response.object <- result$content -## # response headers -## response.headers <- result$response$headers -## # response status code -## response.status.code <- result$response$status_code -##} + +## Test exceptions +var_pet_id <- 1231256 # integer | ID of pet to return +#Find pet by ID +api_instance <- PetApi$new() +# Configure API key authorization: api_key +api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; +result <- tryCatch( + api_instance$GetPetById(var_pet_id), + ApiException = function(ex) ex + ) +# In case of error, print the error object +if(!is.null(result$ApiException)) { + cat(result$ApiException$toString()) +} else { + # deserialized response object + response.object <- result$content + # response headers + response.headers <- result$response$headers + # response status code + response.status.code <- result$response$status_code +} # #json2 <- #'{"name": "pet", "photoUrls" : ["http://a.com", "http://b.com"]}' diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index 9a0d059f56d..3132def5352 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -107,18 +107,19 @@ test_that("GetPetByIdStreaming", { ) }) -#test_that("test GetPetById exception", { -# # test exception -# result <- tryCatch(petApi$GetPetById(98765), # petId not exist -# error = function(ex) ex -# ) -# -# expect_true(!is.null(result)) -# #expect_equal(result$toString(),"") -# expect_equal(result, "1") -# #expect_equal(result$ApiException$errorObject$code, 1) -# #expect_equal(response$name, "name_test") -#}) +test_that("Test GetPetById exception", { + # test exception + result <- tryCatch(pet_api$GetPetById(98765), # petId not exist + ApiException = function(ex) ex + ) + + expect_true(!is.null(result)) + expect_true(!is.null(result$ApiException)) + expect_equal(result$ApiException$status, 404) + # test error object `ApiResponse` + expect_equal(result$ApiException$error_object$toString(), "{\"code\":1,\"type\":\"error\",\"message\":\"Pet not found\"}") + expect_equal(result$ApiException$error_object$code, 1) +}) test_that("GetPetById with data_file", { # test to ensure json is saved to the file `get_pet_by_id.json`