mirror of
https://github.com/google/nomulus.git
synced 2025-07-26 04:28:34 +02:00
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:
parent
5780feaa0f
commit
33759bd448
6 changed files with 44 additions and 10 deletions
|
@ -106,6 +106,17 @@ public final class Registries {
|
||||||
return tld;
|
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. */
|
/** Pass-through check that every TLD in the given iterable exists, otherwise throw an IAE. */
|
||||||
public static Iterable<String> assertTldsExist(Iterable<String> tlds) {
|
public static Iterable<String> assertTldsExist(Iterable<String> tlds) {
|
||||||
for (String tld : tlds) {
|
for (String tld : tlds) {
|
||||||
|
|
|
@ -70,7 +70,8 @@ final class CreateReservedListCommand extends CreateOrUpdateReservedListCommand
|
||||||
checkArgument(nameParts.size() == 2, INVALID_FORMAT_ERROR_MESSAGE);
|
checkArgument(nameParts.size() == 2, INVALID_FORMAT_ERROR_MESSAGE);
|
||||||
String tld = nameParts.get(0);
|
String tld = nameParts.get(0);
|
||||||
if (!tld.equals("common")) {
|
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);
|
checkArgument(nameParts.get(1).matches("[-a-zA-Z0-9]+"), INVALID_FORMAT_ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
package google.registry.tools.server;
|
package google.registry.tools.server;
|
||||||
|
|
||||||
import static com.google.common.flogger.LazyArgs.lazy;
|
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.collect.ImmutableMap;
|
||||||
import com.google.common.flogger.FluentLogger;
|
import com.google.common.flogger.FluentLogger;
|
||||||
|
@ -45,27 +46,29 @@ public abstract class CreateOrUpdatePremiumListAction implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
checkArgumentNotNull(inputData, "Input data must not be null");
|
||||||
save();
|
save();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
logger.atInfo().withCause(e).log(
|
logger.atInfo().withCause(e).log(
|
||||||
"Usage error in attempting to save premium list from nomulus tool command");
|
"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) {
|
} catch (Exception e) {
|
||||||
logger.atSevere().withCause(e).log(
|
logger.atSevere().withCause(e).log(
|
||||||
"Unexpected error saving premium list to Datastore from nomulus tool command");
|
"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. */
|
/** Logs the premium list data at INFO, truncated if too long. */
|
||||||
void logInputData() {
|
void logInputData() {
|
||||||
|
String logData = (inputData == null) ? "(null)" : inputData;
|
||||||
logger.atInfo().log(
|
logger.atInfo().log(
|
||||||
"Received the following input data: %s",
|
"Received the following input data: %s",
|
||||||
lazy(
|
lazy(
|
||||||
() ->
|
() ->
|
||||||
(inputData.length() < MAX_LOGGING_PREMIUM_LIST_LENGTH)
|
(logData.length() < MAX_LOGGING_PREMIUM_LIST_LENGTH)
|
||||||
? inputData
|
? logData
|
||||||
: (inputData.substring(0, MAX_LOGGING_PREMIUM_LIST_LENGTH) + "<truncated>")));
|
: (logData.substring(0, MAX_LOGGING_PREMIUM_LIST_LENGTH) + "<truncated>")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Saves the premium list to both Datastore and Cloud SQL. */
|
/** Saves the premium list to both Datastore and Cloud SQL. */
|
||||||
|
|
|
@ -51,9 +51,12 @@ public class CreatePremiumListAction extends CreateOrUpdatePremiumListAction {
|
||||||
@Override
|
@Override
|
||||||
protected void save() {
|
protected void save() {
|
||||||
checkArgument(
|
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) {
|
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);
|
logger.atInfo().log("Saving premium list for TLD %s", name);
|
||||||
logInputData();
|
logInputData();
|
||||||
|
|
|
@ -113,7 +113,9 @@ class CreateReservedListCommandTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNamingRules_tldThatDoesNotExist_failsWithoutOverride() {
|
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
|
@Test
|
||||||
|
@ -153,7 +155,9 @@ class CreateReservedListCommandTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNamingRules_commonAsListName_failsWithoutOverride() {
|
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
|
@Test
|
||||||
|
|
|
@ -70,6 +70,18 @@ public class CreatePremiumListActionTest {
|
||||||
assertThat(error).contains("A premium list of this name already exists");
|
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
|
@Test
|
||||||
void test_nonExistentTld_successWithOverride() {
|
void test_nonExistentTld_successWithOverride() {
|
||||||
action.name = "zanzibar";
|
action.name = "zanzibar";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue