diff --git a/DESCRIPTION b/DESCRIPTION index 61ab1f6..cef5869 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: owidapi Title: Access the Our World in Data Chart API -Version: 0.1.0.9001 +Version: 0.1.0.9002 Authors@R: c( person("Christoph", "Scheuch", , "christoph@tidy-intelligence.com", role = c("aut", "cre", "cph"), diff --git a/NEWS.md b/NEWS.md index f0ba9ec..2ddd882 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # owidapi (development version) - Encapsulated request logic in internal `perform_request()` function. +- Updated variable parsing & introduced graceful error handling in `owid_get_catalog()` # owidapi 0.1.0 diff --git a/R/owid_get_catalog.R b/R/owid_get_catalog.R index 17abead..292b428 100644 --- a/R/owid_get_catalog.R +++ b/R/owid_get_catalog.R @@ -22,19 +22,41 @@ owid_get_catalog <- function( req <- request(base_url) - resp <- perform_request(req, "owid_get_catalog") + tryCatch( + { + resp <- perform_request(req, "owid_get_catalog") - catalog_raw <- resp |> - resp_body_string() |> - textConnection() |> - read.csv() |> - tibble::as_tibble() + catalog_raw <- resp |> + resp_body_string() |> + textConnection() |> + read.csv() |> + tibble::as_tibble() - if (snake_case) { - catalog <- to_snake_case(catalog_raw) - } else { - catalog <- catalog_raw - } + # Parse logicals + catalog_raw$isInheritanceEnabled <- catalog_raw$isInheritanceEnabled == + "True" + catalog_raw$isIndexable <- catalog_raw$isIndexable == "True" + catalog_raw$isPublished <- catalog_raw$isPublished == "True" - catalog + # Parse dates + catalog_raw$createdAt <- as.Date(catalog_raw$createdAt) + catalog_raw$updatedAt <- as.Date(catalog_raw$updatedAt) + catalog_raw$lastEditedAt <- as.Date(catalog_raw$lastEditedAt) + catalog_raw$publishedAt <- as.Date(catalog_raw$publishedAt) + + if (snake_case) { + catalog <- to_snake_case(catalog_raw) + } else { + catalog <- catalog_raw + } + + catalog + }, + error = function(e) { + cli_alert( + conditionMessage(e) + ) + invisible(NULL) + } + ) } diff --git a/tests/testthat/test-owid_get_catalog.R b/tests/testthat/test-owid_get_catalog.R index 12577e5..2d7fff2 100644 --- a/tests/testthat/test-owid_get_catalog.R +++ b/tests/testthat/test-owid_get_catalog.R @@ -22,7 +22,7 @@ test_that("owid_get_catalog handles request errors gracefully", { cli_abort("Mocked network error") }, { - expect_error( + expect_message( owid_get_catalog(), regexp = "Failed to retrieve data from Our World in Data\\." )