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

Allow error handling #181

Closed
gregmacfarlane opened this issue Nov 10, 2017 · 2 comments
Closed

Allow error handling #181

gregmacfarlane opened this issue Nov 10, 2017 · 2 comments

Comments

@gregmacfarlane
Copy link

I want to write exception handling in case the object I want to get from my bucket isn't there. I tried to surround this in tryCatch:

tryCatch({
  header <- head_object(object = "s3://mybucket/not_there.txt")
}, error = function(e){
  message("object not found")
  # code to get around it
})

Unfortunately, when the object is not available, head_object returns a message saying "Client error: (404) Not Found" instead of an error, so there is nothing to catch. I've gotten around this by saying doing

suppressMessages(  
  header <- aws.s3::head_object(object = "s3://mybucket/not_there.txt")
)
if(!header[1]){
  message("object not found")
  # code to get around it
}

because the first element in the returned object is a boolean indicating whether it was there or not, this works. But it feels unnatural. Is there a reason for this design decision? Is there a better way to handle exceptions?

By the way, I only came across this when I didn't find a function for check_exists(), which would seem to be a natural thing to have here.

@leeper
Copy link
Member

leeper commented Jan 16, 2018

Thanks for this. Will reply again when I've got this working better.

@leeper
Copy link
Member

leeper commented Jul 28, 2018

Give the new object_exists() function a try, which should return a logical. Added via: 927f38f from #234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants