Azure / Microsoft365R

R SDK for interacting with Microsoft 365 APIs
Other
308 stars 41 forks source link

unintelligible error message when uploading empty file #164

Closed jessekps closed 1 year ago

jessekps commented 1 year ago

when using an ms_drive_item to attempt to upload a file these lines in are evaluated at some point: https://github.com/Azure/Microsoft365R/blob/aaad2dd424ce9ac1817c3dfe40ebace8871773f3/R/ms_drive_item.R#L582

        repeat
        {
            next_blocksize <- min(next_blockend - next_blockstart + 1, blocksize)
            seek(src$con, next_blockstart)
            body <- readBin(src$con, "raw", next_blocksize)
            thisblock <- length(body)
            if(thisblock == 0)
                break

            headers <- httr::add_headers(
                `Content-Length`=thisblock,
                `Content-Range`=sprintf("bytes %.0f-%.0f/%.0f",
                    next_blockstart, next_blockstart + thisblock - 1, size)
            )
            res <- httr::PUT(upload_dest, headers, body=body)
            httr::stop_for_status(res)

            next_block <- parse_upload_range(httr::content(res), blocksize)
            if(is.null(next_block))
                break
            next_blockstart <- next_block[1]
            next_blockend <- next_block[2]
        }
        invisible(ms_drive_item$new(self$token, self$tenant, httr::content(res)))

however, when the file is empty, thisblock == 0 is true and we break out of the loop. This leads to an error message in the final line:

Error in httr::content(res) : object 'res' not found

because res is never created. It should possibly be an error message indicating that the file is empty or preferably result in an empty file being uploaded.

(otherwise a great package, tnx!)

hongooi73 commented 1 year ago

That's actually a bug, transferring an empty file shouldn't result in an error.

hongooi73 commented 1 year ago

https://github.com/Azure/Microsoft365R/commit/e62c3e0ed6eaa12994201a56be9612c294882ef5