Skip to content

Commit

Permalink
Change IDNA::Error exception to inherit URI::InvalidURIError
Browse files Browse the repository at this point in the history
  • Loading branch information
jarthod committed Apr 18, 2023
1 parent c63f9ba commit ccbcb9e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/addressable/idna.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
#++

module Addressable
# moved from uri.rb here so we can inherit it
class URI
class InvalidURIError < StandardError; end
end

module IDNA
# All IDNA conversion related errors
class Error < StandardError; end
class Error < Addressable::URI::InvalidURIError; end
# Input is invalid.
class PunycodeBadInput < Error; end
# Output would exceed the space provided.
Expand Down
5 changes: 0 additions & 5 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ module Addressable
# <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>,
# <a href="http://www.ietf.org/rfc/rfc3987.txt">RFC 3987</a>.
class URI
##
# Raised if something other than a uri is supplied.
class InvalidURIError < StandardError
end

##
# Container for the character classes specified in
# <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>.
Expand Down
12 changes: 10 additions & 2 deletions spec/addressable/idna_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@
expect(Addressable::IDNA.to_ascii("faß.de")).to eq("xn--fa-hia.de")
end

it "throws exceptions which inherits Addressable::URI::InvalidURIError" do
# this way IDNA exceptions are also caught by existing rescue on InvalidURIError
expect(Addressable::IDNA::Error).to be < Addressable::URI::InvalidURIError
expect(Addressable::IDNA::PunycodeBadInput).to be < Addressable::IDNA::Error
expect(Addressable::IDNA::PunycodeBigOutput).to be < Addressable::IDNA::Error
expect(Addressable::IDNA::PunycodeOverflow).to be < Addressable::IDNA::Error
end

begin
require "fiber"

Expand Down Expand Up @@ -333,10 +341,10 @@
it "should raise on label too long (>63)" do
expect {
Addressable::IDNA.to_unicode(long)
}.to raise_error(/longer than 63 char|too large/)
}.to raise_error(Addressable::IDNA::Error, /longer than 63 char/)
expect {
Addressable::IDNA.to_ascii(long)
}.to raise_error(/longer than 63 char|too large/)
}.to raise_error(Addressable::URI::InvalidURIError, /longer than 63 char/)
end

it "should raise when punycode decode fails" do
Expand Down

0 comments on commit ccbcb9e

Please sign in to comment.