This is an automated email from the ASF dual-hosted git repository.

apkhmv pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 2d709f5446 IGNITE-23209 Improve error message when config file 
couldn't be parsed (#4479)
2d709f5446 is described below

commit 2d709f5446a61ae20f6d4be00faa670fd1b44a59
Author: Vadim Pakhnushev <[email protected]>
AuthorDate: Tue Oct 1 16:50:45 2024 +0300

    IGNITE-23209 Improve error message when config file couldn't be parsed 
(#4479)
---
 .../commands/cluster/init/ClusterInitOptions.java  |  5 ++-
 .../cluster/init/ConfigFileParseException.java     | 25 +++++++++++++
 .../cluster/init/ConfigParseExceptionHandler.java  | 42 ++++++++++++++++++++++
 .../handler/DefaultExceptionHandlers.java          |  2 ++
 .../cli/commands/cluster/ClusterInitTest.java      | 26 +++++++++++++-
 5 files changed, 98 insertions(+), 2 deletions(-)

diff --git 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ClusterInitOptions.java
 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ClusterInitOptions.java
index e4f60f220b..e69fe1e8a7 100644
--- 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ClusterInitOptions.java
+++ 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ClusterInitOptions.java
@@ -32,6 +32,7 @@ import static 
org.apache.ignite.internal.cli.commands.Options.Constants.META_STO
 import static 
org.apache.ignite.internal.cli.commands.Options.Constants.META_STORAGE_NODE_NAME_PARAM_LABEL;
 
 import com.typesafe.config.Config;
+import com.typesafe.config.ConfigException;
 import com.typesafe.config.ConfigFactory;
 import com.typesafe.config.ConfigRenderOptions;
 import java.io.File;
@@ -147,7 +148,9 @@ public class ClusterInitOptions {
 
                     config = 
config.withFallback(ConfigFactory.parseString(content));
                 } catch (IOException e) {
-                    throw new IgniteCliException("Couldn't read cluster 
configuration file: " + clusterConfigOptions.files, e);
+                    throw new IgniteCliException("Couldn't read cluster 
configuration file " + file, e);
+                } catch (ConfigException e) {
+                    throw new ConfigFileParseException("Couldn't parse cluster 
configuration file " + file, e);
                 }
             }
 
diff --git 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigFileParseException.java
 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigFileParseException.java
new file mode 100644
index 0000000000..c551bbb7b6
--- /dev/null
+++ 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigFileParseException.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.cli.commands.cluster.init;
+
+/** Exception thrown when config file parse failed. */
+public class ConfigFileParseException extends RuntimeException {
+    public ConfigFileParseException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigParseExceptionHandler.java
 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigParseExceptionHandler.java
new file mode 100644
index 0000000000..74bcbc2b9d
--- /dev/null
+++ 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/commands/cluster/init/ConfigParseExceptionHandler.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.cli.commands.cluster.init;
+
+import org.apache.ignite.internal.cli.core.exception.ExceptionHandler;
+import org.apache.ignite.internal.cli.core.exception.ExceptionWriter;
+import org.apache.ignite.internal.cli.core.style.component.ErrorUiComponent;
+
+/** Exception handler for {@link ConfigFileParseException}. */
+public class ConfigParseExceptionHandler implements 
ExceptionHandler<ConfigFileParseException> {
+    @Override
+    public int handle(ExceptionWriter err, ConfigFileParseException e) {
+        err.write(
+                ErrorUiComponent.builder()
+                        .header(e.getMessage())
+                        .details(e.getCause().getMessage())
+                        .build().render()
+        );
+
+        return 1;
+    }
+
+    @Override
+    public Class<ConfigFileParseException> applicableException() {
+        return ConfigFileParseException.class;
+    }
+}
diff --git 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/core/exception/handler/DefaultExceptionHandlers.java
 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/core/exception/handler/DefaultExceptionHandlers.java
index a0c20eeb7c..983949c9c2 100644
--- 
a/modules/cli/src/main/java/org/apache/ignite/internal/cli/core/exception/handler/DefaultExceptionHandlers.java
+++ 
b/modules/cli/src/main/java/org/apache/ignite/internal/cli/core/exception/handler/DefaultExceptionHandlers.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.cli.core.exception.handler;
 
+import 
org.apache.ignite.internal.cli.commands.cluster.init.ConfigParseExceptionHandler;
 import org.apache.ignite.internal.cli.core.exception.ExceptionHandlers;
 
 /**
@@ -39,5 +40,6 @@ public final class DefaultExceptionHandlers extends 
ExceptionHandlers {
         addExceptionHandler(new UnitNotFoundExceptionHandler());
         addExceptionHandler(new UnitAlreadyExistsExceptionHandler());
         addExceptionHandler(new FileNotFoundExceptionHandler());
+        addExceptionHandler(new ConfigParseExceptionHandler());
     }
 }
diff --git 
a/modules/cli/src/test/java/org/apache/ignite/internal/cli/commands/cluster/ClusterInitTest.java
 
b/modules/cli/src/test/java/org/apache/ignite/internal/cli/commands/cluster/ClusterInitTest.java
index 4f9aa07f24..46fb30b6cb 100644
--- 
a/modules/cli/src/test/java/org/apache/ignite/internal/cli/commands/cluster/ClusterInitTest.java
+++ 
b/modules/cli/src/test/java/org/apache/ignite/internal/cli/commands/cluster/ClusterInitTest.java
@@ -31,15 +31,20 @@ import com.typesafe.config.ConfigRenderOptions;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.List;
 import java.util.regex.Pattern;
 import org.apache.ignite.internal.cli.commands.IgniteCliInterfaceTestBase;
 import org.apache.ignite.internal.cli.commands.cluster.init.ClusterInitCommand;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockserver.model.MediaType;
 
 /** Tests "cluster init" command. */
 @DisplayName("cluster init")
+@ExtendWith(WorkDirectoryExtension.class)
 class ClusterInitTest extends IgniteCliInterfaceTestBase {
     private static final Pattern PATTERN = Pattern.compile("\"");
 
@@ -71,7 +76,26 @@ class ClusterInitTest extends IgniteCliInterfaceTestBase {
                 "--config-files", "wrong-path"
         );
 
-        assertErrOutputIs("Couldn't read cluster configuration file: 
[wrong-path]");
+        assertErrOutputIs("Couldn't read cluster configuration file 
wrong-path");
+    }
+
+    @Test
+    void wrongConfigFile(@WorkDirectory Path workDir) throws IOException {
+        Path configFile = Files.createTempFile(workDir, "config", "");
+        Files.write(configFile, List.of("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>"));
+
+        execute(
+                "--url", mockUrl,
+                "--metastorage-group", "node1ConsistentId",
+                "--name", "cluster",
+                "--config-files", configFile.toString()
+        );
+
+        assertErrOutputIs("Couldn't parse cluster configuration file " + 
configFile + "\n"
+                + "String: 1: Key '<' may not be followed by token: '?' 
(Reserved character '?' is not allowed outside quotes)"
+                + " (if you intended '?' (Reserved character '?' is not 
allowed outside quotes)"
+                + " to be part of a key or string value, try enclosing the key 
or value in double quotes)"
+        );
     }
 
     @Test

Reply via email to