Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abhi132004 patch 3 #45

Closed
Closed
Changes from 15 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 @@ -125,47 +125,74 @@ 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<String> 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<String> 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<String> 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we do a catch here, the error is logged elsewhere and also printed to the stdout. The exception is not thrown to here. Hence, this catch will not be able to catch it. We need to remove the log with the stacktrace and display a reason for the failure if possible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch (Exception e) {
// Handle the exception and print a user-friendly error message
handleException(e);
}
}

private void handleException(Exception e) {
// Log the exception or print a user-friendly error message
// If you want to suppress the stack trace, you can just print a custom error message here
outStream.println("Error: " + e.getMessage());
}

In this modified code, the catch block in the execute method calls a handleException method, which you can customize to log the exception, print an error message, or suppress the stack trace as needed. This allows you to have control over how the exception is handled without the default stack trace logging.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abhi132004. It seems you have not understood what I meant in my comment. Let me try and explaing a bit more. The issue is related to the generateBalFile method. In this method, when there are errors in the descriptor generator phase, they are caught as a CodeGeneratorException in here [1]. However, we have only printed a generic error and the error message. But this prints stack traces as well. What we need to do is to check if we can add any error information to this printed error and try and stop the stack trace printing in the cmd.

[1]

// Handle other exceptions if needed
// You can choose to log, print, or handle them differently
outStream.println("Unexpected Error: " + e.getMessage());
}
}
private void generateBalFile(String protoFile) {
Abhi132004 marked this conversation as resolved.
Show resolved Hide resolved
try {
// Your code to generate .bal file from the proto file

// If an error occurs during the descriptor generator phase, throw a CodeGeneratorException with a descriptive message.
if (errorOccurred) {
throw new CodeGeneratorException("An error occurred during code generation. Additional information here.");
}

// Rest of the code for successful generation
} catch (CodeGeneratorException e) {
// Handle CodeGeneratorException without printing stack traces
String errorMessage = "Code Generation Error: " + e.getMessage();
LOG.error(errorMessage);
outStream.println(errorMessage);
} catch (IOException e) {
// Handle IO Exception
String errorMessage = "IO Error: " + e.getMessage();
LOG.error(errorMessage);
outStream.println(errorMessage);
} catch (Exception e) {
// Handle unexpected exceptions
String errorMessage = "Unexpected Error: " + e.getMessage();
LOG.error(errorMessage, e);
outStream.println(errorMessage);
}
}

private void handleException(Exception e) {
// Log the exception or print a user-friendly error message
// If you want to suppress the stack trace, you can just print a custom error message here
outStream.println("Error: " + e.getMessage());
}

private List<String> getProtoFiles(String path) throws IOException {
if (path.endsWith("**.proto")) {
try (Stream<Path> walk = Files.walk(Paths.get(path.substring(0, path.length() - 8)))) {
Expand Down
Loading