Join GitHub today
Added meaningful exceptions to IPAddr #173
At my day job we are doing some processing of an incoming IP address / host name in a Rails middleware and I noticed that the IPAddr class would always raise
I needed to be able to programmatically determine to cause of the failure to determine how to procede. Matching against the error message was less than ideal so modifying the Exceptions in seemed to be the most logical course of action.
With new exceptions a developer can pragmatically determine the cause of the IPAddr failure in a more effective manor than checking the exception message.
begin ip = IPAddr.new(some_other_variable) rescue ArgumentError => e # Can't programmatically determine cause of the failure end
begin ip = IPAddr.new(some_other_variable) rescue IPAddr::InvalidAddress => e rescue IPAddr::UnsupportedAddressFamily => e rescue IPAddr::UnsupportedAddressFamily => e rescue IPAddr::ZeroFilledNumber => e # can use the exception to determine cause of the failure. end
I like the basics of the idea. Here's my suggestions.
Thanks. On further consideration, I'm going to make the following changes.