Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't let corrupt cache files get in our way #485

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# httr2 (development version)

* Corrupt `rds` files no longer cause the request to fail.

# httr2 1.0.1

* `req_perform_stream()` gains a `round = c("byte", "line")` argument to control
Expand Down
19 changes: 16 additions & 3 deletions R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,23 @@ cache_debug <- function(req) {

cache_exists <- function(req) {
if (!req_policy_exists(req, "cache_path")) {
FALSE
} else {
file.exists(req_cache_path(req))
return(FALSE)
}

path <- req_cache_path(req)
if (!file.exists(path)) {
return(FALSE)
}

tryCatch(
{
readRDS(path)
TRUE
},
error = function(e) {
FALSE
}
)
}

# Callers responsibility to check that cache exists
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ test_that("handles responses with files", {
expect_equal(body, new_path(path2))
})

test_that("corrupt files are ignored", {
cache_dir <- withr::local_tempdir()
req <- request("http://example.com") %>% req_cache(cache_dir)

writeLines(letters, req_cache_path(req))
expect_false(cache_exists(req))

saveRDS(1:10, req_cache_path(req))
expect_true(cache_exists(req))
})

# pruning -----------------------------------------------------------------

Expand Down
Loading