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

Move catch to proper place #1133

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Changes from all 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 @@ -131,12 +131,6 @@ public Map<String, PromptResultItemIF> prompt(
Map<String, PromptResultItemIF> resultMap = new HashMap<>();
prompt(header, promptableElementList, resultMap);
return resultMap;
} catch (IOError e) {
if (e.getCause() instanceof InterruptedIOException) {
throw new UserInterruptException(e.getCause());
} else {
throw e;
}
} finally {
close();
}
Expand Down Expand Up @@ -252,38 +246,46 @@ public void prompt(

for (int i = resultMap.isEmpty() ? 0 : resultMap.size() - 1; i < promptableElementList.size(); i++) {
PromptableElementIF pe = promptableElementList.get(i);
PromptResultItemIF result = promptElement(header, pe);
if (result == null) {
// Prompt was cancelled by the user
if (i > 0) {
// Remove last result
header.remove(header.size() - 1);
// Go back to previous prompt
i -= 2;
continue;
} else {
if (config.cancellableFirstPrompt()) {
resultMap.clear();
return;
} else {
// Repeat current prompt
i -= 1;
try {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to prefer the inner loop rather than the outer scope ? just wondering...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does it make any difference? Loop with be broken anyway. Also, this way "catch is closer to the source", no?

PromptResultItemIF result = promptElement(header, pe);
if (result == null) {
// Prompt was cancelled by the user
if (i > 0) {
// Remove last result
header.remove(header.size() - 1);
// Go back to previous prompt
i -= 2;
continue;
} else {
if (config.cancellableFirstPrompt()) {
resultMap.clear();
return;
} else {
// Repeat current prompt
i -= 1;
continue;
}
}
}
}
String resp = result.getDisplayResult();
if (result instanceof ConfirmResult) {
ConfirmResult cr = (ConfirmResult) result;
if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) {
resp = config.resourceBundle().getString("confirmation_yes_answer");
String resp = result.getDisplayResult();
if (result instanceof ConfirmResult) {
ConfirmResult cr = (ConfirmResult) result;
if (cr.getConfirmed() == ConfirmChoice.ConfirmationValue.YES) {
resp = config.resourceBundle().getString("confirmation_yes_answer");
} else {
resp = config.resourceBundle().getString("confirmation_no_answer");
}
}
AttributedStringBuilder message = createMessage(pe.getMessage(), resp);
header.add(message.toAttributedString());
resultMap.put(pe.getName(), result);
} catch (IOError e) {
if (e.getCause() instanceof InterruptedIOException) {
throw new UserInterruptException(e.getCause());
} else {
resp = config.resourceBundle().getString("confirmation_no_answer");
throw e;
}
}
AttributedStringBuilder message = createMessage(pe.getMessage(), resp);
header.add(message.toAttributedString());
resultMap.put(pe.getName(), result);
}
}

Expand Down
Loading