mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/ipaddr] Avoid reraising exceptions
Instead of raising a new exception with a modified message, just use the correct message to begin with. This avoids the issue with both exceptions being displayed at error exit. https://github.com/ruby/ipaddr/commit/09edfd4a7f
This commit is contained in:
parent
b23fba91ae
commit
54ee22be53
1 changed files with 16 additions and 18 deletions
|
@ -327,7 +327,7 @@ class IPAddr
|
||||||
# into an IPv4-mapped IPv6 address.
|
# into an IPv4-mapped IPv6 address.
|
||||||
def ipv4_mapped
|
def ipv4_mapped
|
||||||
if !ipv4?
|
if !ipv4?
|
||||||
raise InvalidAddressError, "not an IPv4 address"
|
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
|
||||||
end
|
end
|
||||||
return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
|
return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
|
||||||
end
|
end
|
||||||
|
@ -337,7 +337,7 @@ class IPAddr
|
||||||
def ipv4_compat
|
def ipv4_compat
|
||||||
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
|
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
|
||||||
if !ipv4?
|
if !ipv4?
|
||||||
raise InvalidAddressError, "not an IPv4 address"
|
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
|
||||||
end
|
end
|
||||||
return self.clone.set(@addr, Socket::AF_INET6)
|
return self.clone.set(@addr, Socket::AF_INET6)
|
||||||
end
|
end
|
||||||
|
@ -368,7 +368,7 @@ class IPAddr
|
||||||
# Returns a string for DNS reverse lookup compatible with RFC3172.
|
# Returns a string for DNS reverse lookup compatible with RFC3172.
|
||||||
def ip6_arpa
|
def ip6_arpa
|
||||||
if !ipv6?
|
if !ipv6?
|
||||||
raise InvalidAddressError, "not an IPv6 address"
|
raise InvalidAddressError, "not an IPv6 address: #{@addr}"
|
||||||
end
|
end
|
||||||
return _reverse + ".ip6.arpa"
|
return _reverse + ".ip6.arpa"
|
||||||
end
|
end
|
||||||
|
@ -376,7 +376,7 @@ class IPAddr
|
||||||
# Returns a string for DNS reverse lookup compatible with RFC1886.
|
# Returns a string for DNS reverse lookup compatible with RFC1886.
|
||||||
def ip6_int
|
def ip6_int
|
||||||
if !ipv6?
|
if !ipv6?
|
||||||
raise InvalidAddressError, "not an IPv6 address"
|
raise InvalidAddressError, "not an IPv6 address: #{@addr}"
|
||||||
end
|
end
|
||||||
return _reverse + ".ip6.int"
|
return _reverse + ".ip6.int"
|
||||||
end
|
end
|
||||||
|
@ -447,7 +447,7 @@ class IPAddr
|
||||||
when Integer
|
when Integer
|
||||||
mask!(prefix)
|
mask!(prefix)
|
||||||
else
|
else
|
||||||
raise InvalidPrefixError, "prefix must be an integer"
|
raise InvalidPrefixError, "prefix must be an integer: #{@addr}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -475,11 +475,11 @@ class IPAddr
|
||||||
case family[0] ? family[0] : @family
|
case family[0] ? family[0] : @family
|
||||||
when Socket::AF_INET
|
when Socket::AF_INET
|
||||||
if addr < 0 || addr > IN4MASK
|
if addr < 0 || addr > IN4MASK
|
||||||
raise InvalidAddressError, "invalid address"
|
raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
end
|
end
|
||||||
when Socket::AF_INET6
|
when Socket::AF_INET6
|
||||||
if addr < 0 || addr > IN6MASK
|
if addr < 0 || addr > IN6MASK
|
||||||
raise InvalidAddressError, "invalid address"
|
raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise AddressFamilyError, "unsupported address family"
|
raise AddressFamilyError, "unsupported address family"
|
||||||
|
@ -500,12 +500,12 @@ class IPAddr
|
||||||
else
|
else
|
||||||
m = IPAddr.new(mask)
|
m = IPAddr.new(mask)
|
||||||
if m.family != @family
|
if m.family != @family
|
||||||
raise InvalidPrefixError, "address family is not same"
|
raise InvalidPrefixError, "address family is not same: #{@addr}"
|
||||||
end
|
end
|
||||||
@mask_addr = m.to_i
|
@mask_addr = m.to_i
|
||||||
n = @mask_addr ^ m.instance_variable_get(:@mask_addr)
|
n = @mask_addr ^ m.instance_variable_get(:@mask_addr)
|
||||||
unless ((n + 1) & n).zero?
|
unless ((n + 1) & n).zero?
|
||||||
raise InvalidPrefixError, "invalid mask #{mask}"
|
raise InvalidPrefixError, "invalid mask #{mask}: #{@addr}"
|
||||||
end
|
end
|
||||||
@addr &= @mask_addr
|
@addr &= @mask_addr
|
||||||
return self
|
return self
|
||||||
|
@ -516,13 +516,13 @@ class IPAddr
|
||||||
case @family
|
case @family
|
||||||
when Socket::AF_INET
|
when Socket::AF_INET
|
||||||
if prefixlen < 0 || prefixlen > 32
|
if prefixlen < 0 || prefixlen > 32
|
||||||
raise InvalidPrefixError, "invalid length"
|
raise InvalidPrefixError, "invalid length: #{@addr}"
|
||||||
end
|
end
|
||||||
masklen = 32 - prefixlen
|
masklen = 32 - prefixlen
|
||||||
@mask_addr = ((IN4MASK >> masklen) << masklen)
|
@mask_addr = ((IN4MASK >> masklen) << masklen)
|
||||||
when Socket::AF_INET6
|
when Socket::AF_INET6
|
||||||
if prefixlen < 0 || prefixlen > 128
|
if prefixlen < 0 || prefixlen > 128
|
||||||
raise InvalidPrefixError, "invalid length"
|
raise InvalidPrefixError, "invalid length: #{@addr}"
|
||||||
end
|
end
|
||||||
masklen = 128 - prefixlen
|
masklen = 128 - prefixlen
|
||||||
@mask_addr = ((IN6MASK >> masklen) << masklen)
|
@mask_addr = ((IN6MASK >> masklen) << masklen)
|
||||||
|
@ -593,8 +593,6 @@ class IPAddr
|
||||||
else
|
else
|
||||||
@mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
|
@mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
|
||||||
end
|
end
|
||||||
rescue InvalidAddressError => e
|
|
||||||
raise e.class, "#{e.message}: #{addr}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def coerce_other(other)
|
def coerce_other(other)
|
||||||
|
@ -617,8 +615,8 @@ class IPAddr
|
||||||
octets = m.captures
|
octets = m.captures
|
||||||
end
|
end
|
||||||
octets.inject(0) { |i, s|
|
octets.inject(0) { |i, s|
|
||||||
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address"
|
(n = s.to_i) < 256 or raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
s.match(/\A0./) and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous"
|
s.match(/\A0./) and raise InvalidAddressError, "zero-filled number in IPv4 address is ambiguous: #{@addr}"
|
||||||
i << 8 | n
|
i << 8 | n
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -635,19 +633,19 @@ class IPAddr
|
||||||
right = ''
|
right = ''
|
||||||
when RE_IPV6ADDRLIKE_COMPRESSED
|
when RE_IPV6ADDRLIKE_COMPRESSED
|
||||||
if $4
|
if $4
|
||||||
left.count(':') <= 6 or raise InvalidAddressError, "invalid address"
|
left.count(':') <= 6 or raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
addr = in_addr($~[4,4])
|
addr = in_addr($~[4,4])
|
||||||
left = $1
|
left = $1
|
||||||
right = $3 + '0:0'
|
right = $3 + '0:0'
|
||||||
else
|
else
|
||||||
left.count(':') <= ($1.empty? || $2.empty? ? 8 : 7) or
|
left.count(':') <= ($1.empty? || $2.empty? ? 8 : 7) or
|
||||||
raise InvalidAddressError, "invalid address"
|
raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
left = $1
|
left = $1
|
||||||
right = $2
|
right = $2
|
||||||
addr = 0
|
addr = 0
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise InvalidAddressError, "invalid address"
|
raise InvalidAddressError, "invalid address: #{@addr}"
|
||||||
end
|
end
|
||||||
l = left.split(':')
|
l = left.split(':')
|
||||||
r = right.split(':')
|
r = right.split(':')
|
||||||
|
|
Loading…
Reference in a new issue