forked from loafle/openapi-generator-original
enhance api exception in r client (#13046)
This commit is contained in:
parent
054264df90
commit
6b6403b2bf
@ -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}}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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"]}'
|
||||
|
@ -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`
|
||||
|
Loading…
x
Reference in New Issue
Block a user