enhance api exception in r client (#13046)

This commit is contained in:
William Cheng 2022-07-31 16:20:39 +08:00 committed by GitHub
parent 054264df90
commit 6b6403b2bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 178 additions and 128 deletions

View File

@ -68,16 +68,20 @@ result <- tryCatch(
) )
# In case of error, print the error object # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
{{#errorObjectType}}
# error object
dput(result$ApiException$error_object)
{{/errorObjectType}}
} else { } else {
{{#returnType}} {{#returnType}}
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
{{/returnType}} {{/returnType}}
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # response status code
response.status.code <- result$response$status_code dput(result$response$status_code)
} }
{{/useRlangExceptionHandling}} {{/useRlangExceptionHandling}}
{{/returnExceptionOnFailure}} {{/returnExceptionOnFailure}}

View File

@ -9,7 +9,7 @@
#' @field body Body of the http response #' @field body Body of the http response
#' @field headers Headers of the http response #' @field headers Headers of the http response
{{#errorObjectType}} {{#errorObjectType}}
#' @field errorObject error object type #' @field error_object error object type
{{/errorObjectType}} {{/errorObjectType}}
#' @export #' @export
ApiException <- R6::R6Class( ApiException <- R6::R6Class(
@ -20,7 +20,7 @@ ApiException <- R6::R6Class(
body = NULL, body = NULL,
headers = NULL, headers = NULL,
{{#errorObjectType}} {{#errorObjectType}}
errorObject = NULL, error_object = NULL,
{{/errorObjectType}} {{/errorObjectType}}
#' Initialize a new ApiException class. #' Initialize a new ApiException class.
#' #'
@ -42,7 +42,7 @@ ApiException <- R6::R6Class(
self$headers <- http_response$headers self$headers <- http_response$headers
self$reason <- http_response$http_status_desc self$reason <- http_response$http_status_desc
{{#errorObjectType}} {{#errorObjectType}}
self$errorObject <- {{errorObjectType}}$new()$fromJSONString(http_response$response) self$error_object <- {{errorObjectType}}$new()$fromJSONString(http_response$response)
{{/errorObjectType}} {{/errorObjectType}}
} else { } else {
self$status <- status self$status <- status
@ -50,7 +50,7 @@ ApiException <- R6::R6Class(
self$body <- NULL self$body <- NULL
self$headers <- NULL self$headers <- NULL
{{#errorObjectType}} {{#errorObjectType}}
self$errorObject <- NULL self$error_object <- NULL
{{/errorObjectType}} {{/errorObjectType}}
} }
}, },
@ -76,9 +76,9 @@ ApiException <- R6::R6Class(
errorMsg <- paste(errorMsg, self$body, "\n") errorMsg <- paste(errorMsg, self$body, "\n")
} }
{{#errorObjectType}} {{#errorObjectType}}
if (!is.null(self$errorObject)) { if (!is.null(self$error_object)) {
errorMsg <- paste(errorMsg, "Error object : ", "\n", sep = "") errorMsg <- paste(errorMsg, "Error object : ", "\n", sep = "")
errorMsg <- paste(errorMsg, self$errorObject$toJSONString(), "\n") errorMsg <- paste(errorMsg, self$error_object$toJSONString(), "\n")
} }
{{/errorObjectType}} {{/errorObjectType}}
errorMsg errorMsg

View File

@ -14,7 +14,7 @@
#' @field reason Reason of the ApiException #' @field reason Reason of the ApiException
#' @field body Body of the http response #' @field body Body of the http response
#' @field headers Headers of the http response #' @field headers Headers of the http response
#' @field errorObject error object type #' @field error_object error object type
#' @export #' @export
ApiException <- R6::R6Class( ApiException <- R6::R6Class(
"ApiException", "ApiException",
@ -23,7 +23,7 @@ ApiException <- R6::R6Class(
reason = NULL, reason = NULL,
body = NULL, body = NULL,
headers = NULL, headers = NULL,
errorObject = NULL, error_object = NULL,
#' Initialize a new ApiException class. #' Initialize a new ApiException class.
#' #'
#' @description #' @description
@ -43,13 +43,13 @@ ApiException <- R6::R6Class(
self$body <- errorMsg self$body <- errorMsg
self$headers <- http_response$headers self$headers <- http_response$headers
self$reason <- http_response$http_status_desc 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 { } else {
self$status <- status self$status <- status
self$reason <- reason self$reason <- reason
self$body <- NULL self$body <- NULL
self$headers <- NULL self$headers <- NULL
self$errorObject <- NULL self$error_object <- NULL
} }
}, },
#' Returns the string format of ApiException. #' Returns the string format of ApiException.
@ -73,9 +73,9 @@ ApiException <- R6::R6Class(
errorMsg <- paste(errorMsg, "Body : ", "\n", sep = "") errorMsg <- paste(errorMsg, "Body : ", "\n", sep = "")
errorMsg <- paste(errorMsg, self$body, "\n") 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, "Error object : ", "\n", sep = "")
errorMsg <- paste(errorMsg, self$errorObject$toJSONString(), "\n") errorMsg <- paste(errorMsg, self$error_object$toJSONString(), "\n")
} }
errorMsg errorMsg
} }

View File

@ -31,14 +31,16 @@ result <- tryCatch(
) )
# In case of error, print the error object # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # response status code
response.status.code <- result$response$status_code dput(result$response$status_code)
} }
``` ```

View File

@ -41,14 +41,16 @@ result <- tryCatch(
) )
# In case of error, print the error object # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # response status code
response.status.code <- result$response$status_code dput(result$response$status_code)
} }
``` ```

View File

@ -31,12 +31,14 @@ result <- tryCatch(
) )
# In case of error, print the error object # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # response status code
response.status.code <- result$response$status_code dput(result$response$status_code)
} }
``` ```

View File

@ -37,12 +37,14 @@ result <- tryCatch(
) )
# In case of error, print the error object # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# deserialized response object # deserialized response object
response.object <- result$content dput(result$content)
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # 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 # In case of error, print the error object
if (!is.null(result$ApiException)) { if (!is.null(result$ApiException)) {
cat(result$ApiException$toString()) dput(result$ApiException)
# error object
dput(result$ApiException$error_object)
} else { } else {
# response headers # response headers
response.headers <- result$response$headers dput(result$response$headers)
# response status code # response status code
response.status.code <- result$response$status_code dput(result$response$status_code)
} }
``` ```

View File

@ -49,27 +49,28 @@ dput(result)
##a <- ModelApiResponse$new()$fromJSONString(errorMsg) ##a <- ModelApiResponse$new()$fromJSONString(errorMsg)
##dput(a) ##dput(a)
## ##
##var_pet_id <- 1231256 # integer | ID of pet to return
## ## Test exceptions
###Find pet by ID var_pet_id <- 1231256 # integer | ID of pet to return
##api_instance <- PetApi$new() #Find pet by ID
### Configure API key authorization: api_key api_instance <- PetApi$new()
##api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; # Configure API key authorization: api_key
##result <- tryCatch( api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
## api_instance$GetPetById(var_pet_id), result <- tryCatch(
## ApiException = function(ex) ex api_instance$GetPetById(var_pet_id),
## ) ApiException = function(ex) ex
### In case of error, print the error object )
##if(!is.null(result$ApiException)) { # In case of error, print the error object
## cat(result$ApiException$toString()) if(!is.null(result$ApiException)) {
##} else { cat(result$ApiException$toString())
## # deserialized response object } else {
## response.object <- result$content # deserialized response object
## # response headers response.object <- result$content
## response.headers <- result$response$headers # response headers
## # response status code response.headers <- result$response$headers
## response.status.code <- result$response$status_code # response status code
##} response.status.code <- result$response$status_code
}
# #
#json2 <- #json2 <-
#'{"name": "pet", "photoUrls" : ["http://a.com", "http://b.com"]}' #'{"name": "pet", "photoUrls" : ["http://a.com", "http://b.com"]}'

View File

@ -107,18 +107,19 @@ test_that("GetPetByIdStreaming", {
) )
}) })
#test_that("test GetPetById exception", { test_that("Test GetPetById exception", {
# # test exception # test exception
# result <- tryCatch(petApi$GetPetById(98765), # petId not exist result <- tryCatch(pet_api$GetPetById(98765), # petId not exist
# error = function(ex) ex ApiException = function(ex) ex
# ) )
#
# expect_true(!is.null(result)) expect_true(!is.null(result))
# #expect_equal(result$toString(),"") expect_true(!is.null(result$ApiException))
# expect_equal(result, "1") expect_equal(result$ApiException$status, 404)
# #expect_equal(result$ApiException$errorObject$code, 1) # test error object `ApiResponse`
# #expect_equal(response$name, "name_test") 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_that("GetPetById with data_file", {
# test to ensure json is saved to the file `get_pet_by_id.json` # test to ensure json is saved to the file `get_pet_by_id.json`