In my Rails app, I want to check if a redirect works correctly, and if it doesn't, then redirect to some other page that I know will work. Essentially, I have a line that says
redirect_to("somesite")
and if the redirect works, it will take me there, but if it doesn't, I get a response like:
Completed 302 Found in 1ms (ActiveRecord: 0.0ms) ERROR URI::InvalidURIError
I tried using rescue block that catches the URI::InvalidURIError, but the error is thrown and never reaches the rescue block. Though what I'm doing may be a little unorthodox, I was wondering if there was a way to grab the error message of "ERROR URI::InvalidURIError" from the server and use that information to write conditions in my code? Thanks!
You can validate your url before the redirect_to action.
for example
url = "somesite"
if valid? uri
redirect_to uri
else
#handler
end
private
def valid?(uri)
!!URI.parse(uri)
rescue URI::InvalidURIError
false
end