7

I use OpenURI library.

object = open("http://example.com")

If http://example.com server code response is equals to 200 my program acts as I expected. But if http://example.com server response code is equals to 400 (or other) then script aborts with OpenURI::HTTPError: 404 Not Found.
I can avoid this if I use 'begin-rescue' construction and handle 'HTTPError exception'.
Is this a correct way? Should I use Net/Http library instead of OpenURI to handle all cases?

borisbn
  • 4,988
  • 25
  • 42
Sergey Blohin
  • 600
  • 1
  • 4
  • 31

1 Answers1

7

Rescuing the OpenURI::HTTPError is perfectly reasonable. See this related answer: https://stackoverflow.com/a/7495389/289274

But if you would rather not deal with exception handling, here's how you can do it with Net::HTTP: https://stackoverflow.com/a/2797443/289274

Community
  • 1
  • 1
Gregory Brown
  • 1,380
  • 9
  • 15
  • Sadly, it also looks like `OpenURI::HTTPError` does not include any content that might have been returned from the request either (like an explanation from the server, beyond a status code). – maurice Jan 28 '15 at 22:56