Errors
Occasionally a Whois
command can raise an exception when an unexpected error occurs. This section describes the available error classes and the conventions adopted.
Base Error
The base exception class for Whois is called Whois::Error
. Any Whois
exception inherits from this class.
In turns, this class extends the Ruby StandardError
exception class. So make sure to list rescue from a Whois::Error
before any StandardError
statement or your code might not work properly.
# Example: Example of invalid rescue
begin
Whois.whois("google.com")
rescue StandardError => e
# do something
rescue Whois::Error => e
# do something
end
# Example: Example of valid rescue
begin
Whois.whois("google.com")
rescue Whois::Error => e
# do something
rescue StandardError => e
# do something
end
Component Specific Errors
Different element of the library might raise different type of errors. For instance, a Whois::Server
connecting to a WHOIS interface can raise a socket error while a Whois::Record::Parser
trying to decompose a response can fail due to unexpected tokens.
Each component of the library defines its own base error classes.
ServerError < Error
ParserError < Error
and so on
AttributeNotImplemented < ParserError < Error
NoInterfaceError < InterfaceNotSupported < ServerError < Error
WebInterfaceError < InterfaceNotSupported < ServerError < Error
This encapsulation, along with the library conventions, makes your exception experience less painful.
Conventions
Whois
never raises global exceptions outside the Whois
namespace.
This encapsulation offers a bullet-proof approach if you need to rescue an exception and you want to be sure it originates from a Whois
statement.
# Example: How to rescue from a Whois::Error and Standard error at the same time.
begin
r = Whois.whois("google.com")
very_complex_operation_with(a.nameservers)
rescue Whois::Error => e
abort("Whois Exception #{e.class}: #{e.message}")
rescue StandardError => e
abort("Application Exception #{e.class}: #{e.message}")
end
Also, you can easily rescue from any Whois
error with one line of code, for instance for debugging or logging purposes.
# Example: How to rescue from a Whois::Error
begin
r = Whois.whois("google.com")
r.nameservers
rescue Whois::Error => e
abort("Exception #{e.class}: #{e.message}")
end
This is a consistent behavior across the entire library. If a statement doesn't follow this rule, this is a bug. Component Specific errors follows the same convention.
The only exception to this rule is the Timeout::Error
exception which is intentionally left unmanaged.