I have a block of code that I have written to pull JSON data from an API and convert it to a data frame. The base code is:
hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
        hospitals_num <- c(0, 2000, 4000, 6000)
        hosget <- lapply(hospitals_num, function(num) {
          hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
          hospitals_json <- fromJSON(hospitals_url_loop)
          hospitals_df <- as.data.frame(hospitals_json$features$attributes)})
        
        hospitals_df <- do.call(rbind, hosget)}
I am trying to add this to a tryCatch() function so that I can be more engaged in error management and control. What I am trying to do is use the error and warning sections of thetryCatch() function to create variables containing errors (if there are any), and throw them in a list. I have the following:
tryCatch({
        hospitals_url <- "https://services1.arcgis.com/Hp6G80Pky0om7QvQ/arcgis/rest/services/Hospitals_1/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
        hospitals_num <- c(0, 2000, 4000, 6000)
        hosget <- lapply(hospitals_num, function(num) {
          hospitals_url_loop <- paste(hospitals_url, "&resultOffset=", num)
          hospitals_json <- fromJSON(hospitals_url_loop)
          hospitals_df <- as.data.frame(hospitals_json$features$attributes)})
        
        hospitals_df <- do.call(rbind, hosget)},
      
      error = function(hosp_e){
              hosp_e <- simpleError("Error: Hospital data has not been collected due to an error. Please ensure that the number loop is correct.")
              print(hosp_e)},
      
      warning = function(hosp_w){
              hosp_w <- simpleWarning("Warning: Hospital data has not been collected. Please ensure that the number loop is correct.")
              print(hosp_w)},
        
      finally = {
              hospitals_log <- (paste("Hospitals Code Has Been Executed on ", Sys.Date()))
              hosp_error <- if (exists("hosp_e") == FALSE) {
                      ("No Error")}
              hosp_warning <- if (exists("hosp_w") == FALSE) {
                      ("No Warning")}
              print(paste(hospitals_log, ", ", hosp_error, ", ", hosp_warning))
              hospitals_output = list(hospitals_log, hosp_error, hosp_warning, paste("URL: ", hospitals_url))
              rm(hospitals_log, hosp_warning, hosp_error, hospitals_num, hosget, hospitals_url)
                })
When I create an error (e.g., adding an extra "l" to hospitals_url) my simpleerror prints (<simpleError: Error: Hospital data has not been collected due to an error. Please ensure that the number loop is correct.>), but is not stored as a variable and added to the list. How can I change my code to accomplish this?
I have looked at the following (here and here) but am now more confused... :(
 
    