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.
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 .
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.
Does this help?
begin
page_source = Net::HTTP.get(URI.parse("http://not-a-real-url.com"))
rescue SocketError => e
puts e.message
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With