Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Net::HTTP.get failures

The following line:

page_source = Net::HTTP.get(URI.parse("http://not-a-real-url.com"))

When passed a url that is properly formatted, but doesn't go anywhere (like the example above), dies out with:

getaddrinfo: nodename nor servname provided, or not known

I'm trying to figure out how to "begin / rescue" this condition, but I can't seem to find in the documentation what error, if any, the 'get' method is throwing.

like image 976
jefflunt Avatar asked Oct 15 '10 23:10

jefflunt


People also ask

How do you handle errors when HTTP Fails?

When the error occurs in the HTTP Request it is intercepted and invokes the catchError . Inside the catchError you can handle the error and then use throwError to throw it to the service. We then register the Interceptor in the Providers array of the root module using the injection token HTTP_INTERCEPTORS .

How do you deal with failed fetch?

The fetch() function will automatically throw an error for network errors but not for HTTP errors such as 4xx or 5xx responses. For HTTP errors we can check the response. ok property to see if the request failed and reject the promise ourselves by calling return Promise.


1 Answers

Does this help?

begin
  page_source = Net::HTTP.get(URI.parse("http://not-a-real-url.com"))
rescue SocketError => e
  puts e.message
end
like image 56
tinifni Avatar answered Oct 04 '22 22:10

tinifni