Skip to content

Commit

Permalink
Handle errors properly when req_perform_connection(mode="text")
Browse files Browse the repository at this point in the history
Fixes #532
  • Loading branch information
jcheng5 committed Sep 3, 2024
1 parent 579bc3f commit 8f175f3
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions R/req-perform-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,24 @@ as_round_function <- function(round = c("byte", "line"),
}

read_con <- function(con, buffer = 32 * 1024) {
bytes <- raw()
repeat {
new <- readBin(con, "raw", n = buffer)
if (length(new) == 0) break
bytes <- c(bytes, new)
if (identical(summary(con)$text, "text")) {
# The connection is in text mode; readBin will error. So we must read it as

Check warning on line 333 in R/req-perform-stream.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-stream.R#L332-L333

Added lines #L332 - L333 were not covered by tests
# characters and then convert to bytes.
chars <- character()
repeat {
new <- readChar(con, nchars = buffer)
if (length(new) == 0) break
chars <- c(chars, new)
}
# TODO: Seems wrong that there's no mention of encodings here??
bytes <- charToRaw(chars)
} else {
bytes <- raw()

Check warning on line 344 in R/req-perform-stream.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-stream.R#L344

Added line #L344 was not covered by tests
repeat {
new <- readBin(con, "raw", n = buffer)
if (length(new) == 0) break
bytes <- c(bytes, new)
}

Check warning on line 349 in R/req-perform-stream.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-stream.R#L347-L349

Added lines #L347 - L349 were not covered by tests
}
if (length(bytes) == 0) {
NULL
Expand Down

0 comments on commit 8f175f3

Please sign in to comment.