IQSS / dataverse-client-r

R Client for Dataverse Repositories
https://iqss.github.io/dataverse-client-r
61 stars 25 forks source link

More verbose httr error message for get_file() #31

Closed EdJeeOnGitHub closed 3 years ago

EdJeeOnGitHub commented 4 years ago

I often find get_file() errors since the API can't return a certain file format. Since the dataverse API returns informative messages when a request fails it makes sense to pass this along in R instead of just the default from httr::stop_for_status().

For instance:

library(dataverse)
library(tidyverse)

IPA_dataset <- get_dataset("doi:10.7910/DVN/JGLOZF")
# doi:  https://doi.org/10.7910/DVN/JGLOZF
community_files <- IPA_dataset$files$id %>% 
  map(function(x){
    print(x)
    return(get_file(x))
  })

after the PR will return:

Not Found (HTTP 404). Failed to '/api/v1/access/datafile/2972336' datafile access error: requested optional service (image scaling, format conversion, etc.) is not supported on this datafile.

instead of:

Error in get_file(x) : Not Found (HTTP 404).

kuriwaki commented 3 years ago

I like this feature very much. (See below for a reprex).

Your current IPA example fails to give the intended error (i.e. it actually passes without any error messages). It's not clear if it should fail: your example trys to download a Word Doc and indeed https://dataverse.harvard.edu/api/access/datafile/2972336?format=original gives that error you posted but https://dataverse.harvard.edu/api/access/datafile/2972336 but downloads the Word Doc fine. In #39 I changed so that querys/format args are only used when the target is an ingested file.

That was a stopgap and should be revisited... in the meantime, are there any examples of .tab files for which we'd get an informative error message? We can put that in a testthat too.

# Get `ReadMe with Codebook.docx` (https://doi.org/10.7910/DVN/JGLOZF/GYZ7OQ, id = 2972336)
# master branch
library(dataverse)
get_file(2972336, server = "demo.dataverse.org") # wrong server
#> Error in get_file(2972336, server = "demo.dataverse.org"): Not Found (HTTP 404).
# EdJeeOnGitHub fork branch
library(dataverse)
get_file(2972336, server = "demo.dataverse.org") # wrong server
#> Error in get_file(2972336, server = "demo.dataverse.org"): Not Found (HTTP 404). Failed to API endpoint does not exist on this server. Please check your code for typos, or consult our API guide at http://guides.dataverse.org..

Created on 2020-12-28 by the reprex package (v0.3.0)

EdJeeOnGitHub commented 3 years ago

Thanks!

Back in Nov 2019 it was definitely throwing errors but things seem to have changed quite a bit since then - I think the pull request was meant to be incorporated some time ago but seems to have fallen through the cracks.

I haven't touched any dataverse data in a while now so don't have any handy candidates for test cases I'm afraid.