diff --git a/src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java b/src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java index b34cbeec9..4609f7250 100644 --- a/src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java +++ b/src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java @@ -140,8 +140,12 @@ public List listBranches() { } @Override - public Long uploadStorage(String fileName, InputStream content) { - Storage storage = executeRequest(() -> this.client.getStorageApi() + public Long uploadStorage(String fileName, InputStream content) throws ResponseException { + Map, ResponseException> errorHandlers = new LinkedHashMap, ResponseException>() {{ + put((code, message) -> StringUtils.containsAny(message, "streamIsEmpty", "Stream size is null. Not empty content expected"), + new EmptyFileException("Not empty content expected")); + }}; + Storage storage = executeRequest(errorHandlers, () -> this.client.getStorageApi() .addStorage(fileName, content) .getData()); return storage.getId(); @@ -187,6 +191,7 @@ public void addSource(AddFileRequest request) throws ResponseException { put((code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"), new RepeatException()); put((code, message) -> StringUtils.contains(message, "Name must be unique"), new ExistsResponseException()); put((code, message) -> StringUtils.contains(message, "Invalid SRX specified"), new ResponseException("Invalid SRX file specified")); + put((code, message) -> StringUtils.containsAny(message, "isEmpty", "Value is required and can't be empty"), new EmptyFileException("Value is required and can't be empty")); }}; executeRequestWithPossibleRetry( errorHandlers, diff --git a/src/main/java/com/crowdin/cli/client/EmptyFileException.java b/src/main/java/com/crowdin/cli/client/EmptyFileException.java new file mode 100644 index 000000000..b7a4e7acd --- /dev/null +++ b/src/main/java/com/crowdin/cli/client/EmptyFileException.java @@ -0,0 +1,8 @@ +package com.crowdin.cli.client; + +public class EmptyFileException extends ResponseException { + + public EmptyFileException(String message) { + super(message); + } +} diff --git a/src/main/java/com/crowdin/cli/client/ProjectClient.java b/src/main/java/com/crowdin/cli/client/ProjectClient.java index 2208644b1..4e5daa5a5 100644 --- a/src/main/java/com/crowdin/cli/client/ProjectClient.java +++ b/src/main/java/com/crowdin/cli/client/ProjectClient.java @@ -39,7 +39,7 @@ public interface ProjectClient extends Client { List listBranches(); - Long uploadStorage(String fileName, InputStream content); + Long uploadStorage(String fileName, InputStream content) throws ResponseException; Directory addDirectory(AddDirectoryRequest request) throws ResponseException; diff --git a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java index 49e8a94f6..68551c3a1 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/UploadSourcesAction.java @@ -1,6 +1,7 @@ package com.crowdin.cli.commands.actions; import com.crowdin.cli.client.CrowdinProjectFull; +import com.crowdin.cli.client.EmptyFileException; import com.crowdin.cli.client.ExistsResponseException; import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.NewAction; @@ -259,6 +260,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) { try (InputStream fileStream = new FileInputStream(sourceFile)) { request.setStorageId(client.uploadStorage(source.substring(source.lastIndexOf(Utils.PATH_SEPARATOR) + 1), fileStream)); + } catch (EmptyFileException e){ + errorsPresented.set(false); + out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.uploading_file_skipped"), fileFullPath))); + return; } catch (Exception e) { errorsPresented.set(true); throw new RuntimeException( diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index e00057959..51036b248 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -398,6 +398,7 @@ message.new_version_text=New version of Crowdin CLI is available! %s -> %s message.new_version_text.2=Changelog: @|cyan https://github.com/crowdin/crowdin-cli/releases/latest|@ message.new_version_text.3=Please update for the best experience! message.uploading_file=File @|bold '%s'|@ +message.uploading_file_skipped=File @|bold '%s'|@ was skipped since it is empty message.downloaded_file=File @|bold '%s'|@ message.translation_file=Translation file @|bold '%s'|@ message.build_language_archive=Building ZIP archive with the latest translations for @|bold '%s'|@ diff --git a/src/test/java/com/crowdin/cli/client/CrowdinProjectClientTest.java b/src/test/java/com/crowdin/cli/client/CrowdinProjectClientTest.java index 8225f5be5..97ac0bd84 100644 --- a/src/test/java/com/crowdin/cli/client/CrowdinProjectClientTest.java +++ b/src/test/java/com/crowdin/cli/client/CrowdinProjectClientTest.java @@ -329,7 +329,7 @@ public void testAddBranch() { } @Test - public void testUploadStorage() throws IOException { + public void testUploadStorage() throws IOException, ResponseException { InputStream requestData = IOUtils.toInputStream("Something to send", "UTF-8"); StorageResponseObject response = new StorageResponseObject() {{ setData(new Storage());