From 9b190b3007841ffc2d196a2af8c1d553d6ea5eff Mon Sep 17 00:00:00 2001 From: Daniel Herszenhut Date: Wed, 11 Dec 2024 12:29:40 -0300 Subject: [PATCH] improved error when unable to download cnefe data --- DESCRIPTION | 1 + R/download_cnefe.R | 14 ++++++++++---- codemeta.json | 18 +++++++++++++++--- tests/testthat/_snaps/download_cnefe.md | 9 +++++++++ tests/testthat/test-download_cnefe.R | 7 ++++++- 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 tests/testthat/_snaps/download_cnefe.md diff --git a/DESCRIPTION b/DESCRIPTION index 09005d1..d25c4e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Imports: httr2, parallel, purrr, + rlang, tools, utils, zip diff --git a/R/download_cnefe.R b/R/download_cnefe.R index 23a19c5..b5fe296 100644 --- a/R/download_cnefe.R +++ b/R/download_cnefe.R @@ -98,10 +98,7 @@ download_files <- function(files_to_download, progress) { function(r) inherits(r, "error") ) - # TODO: improve this error - if (any(response_errored)) { - stop("Could not download data for one of the states, uh-oh!") - } + if (any(response_errored)) error_cnefe_download_failed() return(dest_files) } @@ -125,3 +122,12 @@ perform_requests_in_parallel <- function(requests, dest_files, progress) { ) } +error_cnefe_download_failed <- function() { + geocodebr_error( + c( + "Could not download CNEFE data for one or more states.", + "i" = "Please try again later." + ), + call = rlang::caller_env(n = 2) + ) +} diff --git a/codemeta.json b/codemeta.json index 37a8a99..61d2df8 100644 --- a/codemeta.json +++ b/codemeta.json @@ -309,16 +309,28 @@ "sameAs": "https://CRAN.R-project.org/package=purrr" }, "13": { + "@type": "SoftwareApplication", + "identifier": "rlang", + "name": "rlang", + "provider": { + "@id": "https://cran.r-project.org", + "@type": "Organization", + "name": "Comprehensive R Archive Network (CRAN)", + "url": "https://cran.r-project.org" + }, + "sameAs": "https://CRAN.R-project.org/package=rlang" + }, + "14": { "@type": "SoftwareApplication", "identifier": "tools", "name": "tools" }, - "14": { + "15": { "@type": "SoftwareApplication", "identifier": "utils", "name": "utils" }, - "15": { + "16": { "@type": "SoftwareApplication", "identifier": "zip", "name": "zip", @@ -332,7 +344,7 @@ }, "SystemRequirements": null }, - "fileSize": "176.64KB", + "fileSize": "181.875KB", "citation": [ { "@type": "SoftwareSourceCode", diff --git a/tests/testthat/_snaps/download_cnefe.md b/tests/testthat/_snaps/download_cnefe.md new file mode 100644 index 0000000..a97cf01 --- /dev/null +++ b/tests/testthat/_snaps/download_cnefe.md @@ -0,0 +1,9 @@ +# errors if could not download the data for one or more states + + Code + tester("AL", cache = FALSE) + Condition + Error in `download_cnefe()`: + ! Could not download CNEFE data for one or more states. + i Please try again later. + diff --git a/tests/testthat/test-download_cnefe.R b/tests/testthat/test-download_cnefe.R index 04077dc..bfda371 100644 --- a/tests/testthat/test-download_cnefe.R +++ b/tests/testthat/test-download_cnefe.R @@ -53,7 +53,12 @@ test_that("errors if could not download the data for one or more states", { } ) - expect_error(tester("AL", cache = FALSE)) + expect_error( + tester("AL", cache = FALSE), + class = "geocodebr_error_cnefe_download_failed" + ) + + expect_snapshot(tester("AL", cache = FALSE), error = TRUE, cnd_class = TRUE) }) test_that("would download the data of all states if state='all'", {