diff --git a/protoc-cli/src/main/java/io/ballerina/protoc/protobuf/cmd/GrpcCmd.java b/protoc-cli/src/main/java/io/ballerina/protoc/protobuf/cmd/GrpcCmd.java index 31a8d90..386815d 100644 --- a/protoc-cli/src/main/java/io/ballerina/protoc/protobuf/cmd/GrpcCmd.java +++ b/protoc-cli/src/main/java/io/ballerina/protoc/protobuf/cmd/GrpcCmd.java @@ -125,47 +125,40 @@ private static void exportResource(String resourceName, ClassLoader classLoader) } @Override - public void execute() { - //Help flag check - if (helpFlag) { - String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); - outStream.println(commandUsageInfo); - return; - } + public void execute() { + // Help flag check + if (helpFlag) { + String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); + outStream.println(commandUsageInfo); + return; + } + try { if (protoPath.endsWith("**.proto") || new File(protoPath).isDirectory()) { // Multiple proto files - List protoFiles; - try { - protoFiles = getProtoFiles(protoPath); - } catch (IOException e) { - String errorMessage = "Failed to find proto files in the directory. " + - "Please input a valid proto files directory."; - outStream.println(errorMessage); - return; + List protoFiles = getProtoFiles(protoPath); + if (protoFiles.isEmpty()) { + throw new RuntimeException("Input directory does not contain any proto files. " + + "Please input a valid proto files directory."); } - if (protoFiles.size() == 0) { - String errorMessage = "Input directory does not contain any proto files. " + - "Please input a valid proto files directory."; - outStream.println(errorMessage); - } else { - for (String protoFile : protoFiles) { - generateBalFile(protoFile); - } + for (String protoFile : protoFiles) { + generateBalFile(protoFile); } } else { // Single proto file - // check input protobuf file path Optional pathExtension = getFileExtension(protoPath); if (pathExtension.isEmpty() || !PROTO_EXTENSION.equalsIgnoreCase(pathExtension.get())) { - String errorMessage = "Invalid proto file path. Please input valid proto file location."; - outStream.println(errorMessage); - return; + throw new RuntimeException("Input directory does not contain any proto files. " + + "Please input a valid proto files directory."); } generateBalFile(protoPath); } + } catch (Exception e) { + // Handle other exceptions if needed + // You can choose to log, print, or handle them differently + outStream.println("Unexpected Error: " + e.getMessage()); } - +} private List getProtoFiles(String path) throws IOException { if (path.endsWith("**.proto")) { try (Stream walk = Files.walk(Paths.get(path.substring(0, path.length() - 8)))) {