diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/cli/GenericCli.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/cli/GenericCli.java index 3ec9048dfcf4..5dc3f124eb3e 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/cli/GenericCli.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/cli/GenericCli.java @@ -18,6 +18,10 @@ import java.io.IOException; import java.io.PrintWriter; +import java.nio.file.AccessDeniedException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.FileSystemException; +import java.nio.file.NoSuchFileException; import java.util.Map; import com.google.common.base.Strings; @@ -90,7 +94,12 @@ protected void printError(Throwable error) { if (verbose || Strings.isNullOrEmpty(error.getMessage())) { error.printStackTrace(cmd.getErr()); } else { - cmd.getErr().println(error.getMessage().split("\n")[0]); + if (error instanceof FileSystemException) { + String errorMessage = handleFileSystemException((FileSystemException) error); + cmd.getErr().println(errorMessage); + } else { + cmd.getErr().println(error.getMessage().split("\n")[0]); + } } } @@ -123,4 +132,24 @@ protected PrintWriter out() { protected PrintWriter err() { return cmd.getErr(); } + + private static String handleFileSystemException(FileSystemException e) { + String errorMessage = e.getMessage(); + + // If reason is set, return the exception's message as it is. + // Otherwise, construct a custom message based on the type of exception + if (e.getReason() == null) { + if (e instanceof NoSuchFileException) { + errorMessage = "File not found: " + errorMessage; + } else if (e instanceof AccessDeniedException) { + errorMessage = "Access denied: " + errorMessage; + } else if (e instanceof FileAlreadyExistsException) { + errorMessage = "File already exists: " + errorMessage; + } else { + errorMessage = e.getClass().getSimpleName() + ": " + errorMessage; + } + } + + return "Error: " + errorMessage; + } } diff --git a/hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java b/hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java index 65d1bbc0eea5..30543f790749 100644 --- a/hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java +++ b/hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java @@ -23,7 +23,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; -import java.io.FileNotFoundException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Files; @@ -82,9 +81,6 @@ protected void execute(OzoneClient client, OzoneAddress address) String keyName = address.getKeyName(); File dataFile = new File(fileName); - if (!dataFile.exists()) { - throw new FileNotFoundException("Error: File not found: " + fileName); - } if (isVerbose()) { try (InputStream stream = Files.newInputStream(dataFile.toPath())) { diff --git a/hadoop-ozone/dist/src/main/smoketest/basic/ozone-shell-lib.robot b/hadoop-ozone/dist/src/main/smoketest/basic/ozone-shell-lib.robot index cbbc511595f3..4c4de2dab198 100644 --- a/hadoop-ozone/dist/src/main/smoketest/basic/ozone-shell-lib.robot +++ b/hadoop-ozone/dist/src/main/smoketest/basic/ozone-shell-lib.robot @@ -131,8 +131,6 @@ Test ozone shell errors ${result} = Execute and checkrc ozone sh bucket create ${protocol}${server}/${volume}/bucket1 255 Should contain ${result} QUOTA_ERROR Execute and checkrc ozone sh volume delete ${protocol}${server}/${volume} 0 - ${result} = Execute and checkrc ozone sh key put ${protocol}${server}/${volume}/bucket1/key1 sample.txt 255 - Should Match Regexp ${result} Error: File not found: .* Test Volume Acls [arguments] ${protocol} ${server} ${volume} @@ -188,6 +186,8 @@ Test key handling Should Contain ${result} NOTICE.txt.1 exists ${result} = Execute ozone sh key get --force ${protocol}${server}/${volume}/bb1/key1 /tmp/NOTICE.txt.1 Should Not Contain ${result} NOTICE.txt.1 exists + ${result} = Execute and checkrc ozone sh key put ${protocol}${server}/${volume}/bb1/key1 sample.txt 255 + Should Contain ${result} File not found: sample.txt ${result} = Execute ozone sh key info ${protocol}${server}/${volume}/bb1/key1 | jq -r '. | select(.name=="key1")' Should contain ${result} creationTime Should not contain ${result} ETag