@@ -55,15 +55,26 @@ public static Domain of(String domain) {
5555 Preconditions .checkArgument (domain .length () <= MAXIMUM_DOMAIN_LENGTH ,
5656 "Domain name length should not exceed %s characters" , MAXIMUM_DOMAIN_LENGTH );
5757
58- String domainWithoutBrackets = IDN .toASCII (removeBrackets (domain ), IDN .ALLOW_UNASSIGNED );
58+ String domainWithoutBrackets ;
59+ try {
60+ domainWithoutBrackets = IDN .toASCII (removeBrackets (domain ), IDN .ALLOW_UNASSIGNED );
61+ } catch (IllegalArgumentException e ) {
62+ // IDN.toASCII's own message can be cryptic ("Empty label is not
63+ // a legal name", "A prohibited code point was found in the
64+ // input..."). Let's save wear and tear on the poor developer's
65+ // brain.
66+ throw new IllegalArgumentException (
67+ "Domain '" + domain + "' is invalid according to IDNA: " + e .getMessage (), e );
68+ }
5969 Preconditions .checkArgument (PART_CHAR_MATCHER .matchesAllOf (domainWithoutBrackets ),
6070 "Domain parts ASCII chars must be a-z A-Z 0-9 - or _ in %s" , domain );
6171
6272 if (domainWithoutBrackets .startsWith ("xn--" ) ||
6373 domainWithoutBrackets .contains (".xn--" )) {
6474 domainWithoutBrackets = IDN .toUnicode (domainWithoutBrackets );
6575 Preconditions .checkArgument (!domainWithoutBrackets .startsWith ("xn--" ) &&
66- !domainWithoutBrackets .contains (".xn--" ));
76+ !domainWithoutBrackets .contains (".xn--" ),
77+ "A-label could not be decoded to Unicode in %s" , domain );
6778 }
6879
6980 int pos = 0 ;
0 commit comments