Improve error when creating domain label lists for non-existent TLDs (#1112)

* Improve error message when creating domain label lists for non-existent TLDs
This commit is contained in:
Ben McIlwain 2021-04-27 19:17:23 -04:00 committed by GitHub
parent 5780feaa0f
commit 33759bd448
6 changed files with 44 additions and 10 deletions

View file

@ -106,6 +106,17 @@ public final class Registries {
return tld;
}
/**
* Pass-through check that the TLD exists, otherwise throw using the given error format message.
*
* <p>The specified TLD will be passed to the format message string.
*/
public static String assertTldExists(String tld, String fmtMessage) {
String message = String.format(fmtMessage, tld);
checkArgument(getTlds().contains(checkArgumentNotNull(emptyToNull(tld), message)), message);
return tld;
}
/** Pass-through check that every TLD in the given iterable exists, otherwise throw an IAE. */
public static Iterable<String> assertTldsExist(Iterable<String> tlds) {
for (String tld : tlds) {

View file

@ -70,7 +70,8 @@ final class CreateReservedListCommand extends CreateOrUpdateReservedListCommand
checkArgument(nameParts.size() == 2, INVALID_FORMAT_ERROR_MESSAGE);
String tld = nameParts.get(0);
if (!tld.equals("common")) {
assertTldExists(tld);
assertTldExists(
tld, "The name must be in the format {tld|common}_list-name, yet TLD %s does not exist");
}
checkArgument(nameParts.get(1).matches("[-a-zA-Z0-9]+"), INVALID_FORMAT_ERROR_MESSAGE);
}

View file

@ -15,6 +15,7 @@
package google.registry.tools.server;
import static com.google.common.flogger.LazyArgs.lazy;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
@ -45,27 +46,29 @@ public abstract class CreateOrUpdatePremiumListAction implements Runnable {
@Override
public void run() {
try {
checkArgumentNotNull(inputData, "Input data must not be null");
save();
} catch (IllegalArgumentException e) {
logger.atInfo().withCause(e).log(
"Usage error in attempting to save premium list from nomulus tool command");
response.setPayload(ImmutableMap.of("error", e.toString(), "status", "error"));
response.setPayload(ImmutableMap.of("error", e.getMessage(), "status", "error"));
} catch (Exception e) {
logger.atSevere().withCause(e).log(
"Unexpected error saving premium list to Datastore from nomulus tool command");
response.setPayload(ImmutableMap.of("error", e.toString(), "status", "error"));
response.setPayload(ImmutableMap.of("error", e.getMessage(), "status", "error"));
}
}
/** Logs the premium list data at INFO, truncated if too long. */
void logInputData() {
String logData = (inputData == null) ? "(null)" : inputData;
logger.atInfo().log(
"Received the following input data: %s",
lazy(
() ->
(inputData.length() < MAX_LOGGING_PREMIUM_LIST_LENGTH)
? inputData
: (inputData.substring(0, MAX_LOGGING_PREMIUM_LIST_LENGTH) + "<truncated>")));
(logData.length() < MAX_LOGGING_PREMIUM_LIST_LENGTH)
? logData
: (logData.substring(0, MAX_LOGGING_PREMIUM_LIST_LENGTH) + "<truncated>")));
}
/** Saves the premium list to both Datastore and Cloud SQL. */

View file

@ -51,9 +51,12 @@ public class CreatePremiumListAction extends CreateOrUpdatePremiumListAction {
@Override
protected void save() {
checkArgument(
!PremiumListDualDao.exists(name), "A premium list of this name already exists: %s.", name);
!PremiumListDualDao.exists(name), "A premium list of this name already exists: %s", name);
if (!override) {
assertTldExists(name);
assertTldExists(
name,
"Premium names must match the name of the TLD they are intended to be used on"
+ " (unless --override is specified), yet TLD %s does not exist");
}
logger.atInfo().log("Saving premium list for TLD %s", name);
logInputData();

View file

@ -113,7 +113,9 @@ class CreateReservedListCommandTest
@Test
void testNamingRules_tldThatDoesNotExist_failsWithoutOverride() {
runNameTestExpectedFailure("footld_reserved-list", "TLD footld does not exist");
runNameTestExpectedFailure(
"footld_reserved-list",
"The name must be in the format {tld|common}_list-name, yet TLD footld does not exist");
}
@Test
@ -153,7 +155,9 @@ class CreateReservedListCommandTest
@Test
void testNamingRules_commonAsListName_failsWithoutOverride() {
runNameTestExpectedFailure("invalidtld_common", "TLD invalidtld does not exist");
runNameTestExpectedFailure(
"invalidtld_common",
"The name must be in the format {tld|common}_list-name, yet TLD invalidtld does not exist");
}
@Test

View file

@ -70,6 +70,18 @@ public class CreatePremiumListActionTest {
assertThat(error).contains("A premium list of this name already exists");
}
@Test
void test_nonExistentTld_fails() {
action.name = "zanzibar";
action.inputData = "zanzibar,USD 100";
action.run();
assertThat(response.getResponseMap().get("status")).isEqualTo("error");
assertThat(response.getResponseMap().get("error").toString())
.isEqualTo(
"Premium names must match the name of the TLD they are intended to be used on"
+ " (unless --override is specified), yet TLD zanzibar does not exist");
}
@Test
void test_nonExistentTld_successWithOverride() {
action.name = "zanzibar";