Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,12 +89,17 @@ public int execute(String[] argv) {
}

protected void printError(Throwable error) {
//message could be null in case of NPE. This is unexpected so we can
//print out the stack trace.
if (verbose || Strings.isNullOrEmpty(error.getMessage())) {
error.printStackTrace(cmd.getErr());
if (error instanceof FileSystemException) {
String errorMessage = handleFileSystemException((FileSystemException) error);
cmd.getErr().println(errorMessage);
Comment thread
adoroszlai marked this conversation as resolved.
Outdated
} else {
cmd.getErr().println(error.getMessage().split("\n")[0]);
//message could be null in case of NPE. This is unexpected so we can
//print out the stack trace.
if (verbose || Strings.isNullOrEmpty(error.getMessage())) {
error.printStackTrace(cmd.getErr());
} else {
cmd.getErr().println(error.getMessage().split("\n")[0]);
}
}
}

Expand Down Expand Up @@ -123,4 +132,25 @@ protected PrintWriter out() {
protected PrintWriter err() {
return cmd.getErr();
}

private String handleFileSystemException(FileSystemException e) {
// If reason is set, return the exception's message as it is.
if (e.getReason() != null) {
return e.getMessage();
}

// Otherwise, construct a custom message based on the type of exception
String errorMessage;
if (e instanceof NoSuchFileException) {
errorMessage = String.format("Error: File not found: %s", e.getFile());
} else if (e instanceof AccessDeniedException) {
errorMessage = String.format("Error: Access denied to file: %s", e.getFile());
} else if (e instanceof FileAlreadyExistsException) {
errorMessage = String.format("Error: File already exists: %s", e.getFile());
Comment thread
adoroszlai marked this conversation as resolved.
Outdated
} else {
errorMessage = String.format("Error with file: %s. Details: %s", e.getFile(), e.getMessage());
Comment thread
adoroszlai marked this conversation as resolved.
Outdated
}

return errorMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: .*
Comment thread
adoroszlai marked this conversation as resolved.

Test Volume Acls
[arguments] ${protocol} ${server} ${volume}
Expand Down Expand Up @@ -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 Match Regexp ${result} Error: File not found: .*
Comment thread
adoroszlai marked this conversation as resolved.
Outdated
${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
Expand Down