I
Iñaki Baz Castillo
Hi, I've coded a DNS library. When a DNS query fails my library
doesn't raise an exception (as it's expensive) but instead returns a
Symbol (more efficient):
- :dns_error_nxdomain - The domain name does not exist.
- :dns_error_nodata - There is no data of requested type found.
- :dns_error_tempfail - Temporary error, the resolver nameserver was
not able to process our query or timed out.
- :dns_error_protocol - Protocol error, a nameserver returned malformed rep=
ly.
An example usage:
-----------------------------------------------------------
resolver =3D EM::Udns::Resolver.new
EM::Udns.run resolver
query =3D resolver.submit_A "google.com"
query.callback do |result|
puts "result =3D> #{result.inspect}"
end
query.errback do |error|
case error
when :dns_error_nxdomain
# do something
when :dns_error_nodata
# do something
when :dns_error_tempfail
# do something
end
end
--------------------------------------------------------------
So returning a Symbol (in case of failure) is good as I can use it
within a case/when statement.
Another possibility would be returning an instance of a class, something li=
ke:
class EM::Udns::ErrorNoDomain < EM::Udns::Error ; end
class EM::Udns::ErrorNoData < EM::Udns::Error ; end
class EM::Udns::ErrorTempFail < EM::Udns::Error ; end
or I could extend Errno module:
class Errno:nsNoDomain ; end
class Errno:nsNoData ; end
class Errno:nsTempail ; end
In your opinnion, which is the most elegant way? any other suggestion?
Thanks a lot.
--=20
I=C3=B1aki Baz Castillo
<[email protected]>
doesn't raise an exception (as it's expensive) but instead returns a
Symbol (more efficient):
- :dns_error_nxdomain - The domain name does not exist.
- :dns_error_nodata - There is no data of requested type found.
- :dns_error_tempfail - Temporary error, the resolver nameserver was
not able to process our query or timed out.
- :dns_error_protocol - Protocol error, a nameserver returned malformed rep=
ly.
An example usage:
-----------------------------------------------------------
resolver =3D EM::Udns::Resolver.new
EM::Udns.run resolver
query =3D resolver.submit_A "google.com"
query.callback do |result|
puts "result =3D> #{result.inspect}"
end
query.errback do |error|
case error
when :dns_error_nxdomain
# do something
when :dns_error_nodata
# do something
when :dns_error_tempfail
# do something
end
end
--------------------------------------------------------------
So returning a Symbol (in case of failure) is good as I can use it
within a case/when statement.
Another possibility would be returning an instance of a class, something li=
ke:
class EM::Udns::ErrorNoDomain < EM::Udns::Error ; end
class EM::Udns::ErrorNoData < EM::Udns::Error ; end
class EM::Udns::ErrorTempFail < EM::Udns::Error ; end
or I could extend Errno module:
class Errno:nsNoDomain ; end
class Errno:nsNoData ; end
class Errno:nsTempail ; end
In your opinnion, which is the most elegant way? any other suggestion?
Thanks a lot.
--=20
I=C3=B1aki Baz Castillo
<[email protected]>