Skip to content

Commit

Permalink
Command doc: improved exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 29, 2020
1 parent 79b9feb commit f36c685
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,21 @@ private Object doc(CommandInput input) {
if (!Desktop.isDesktopSupported()) {
throw new IllegalStateException("Desktop is not supported!");
}
Map<String,Object> docs = consoleOption("docs", null);
Map<String,Object> docs;
try {
docs = consoleOption("docs", null);
} catch (Exception e) {
Exception exception = new IllegalStateException("Bad documents configuration!");
exception.addSuppressed(e);
throw exception;
}
if (docs == null) {
throw new IllegalStateException("No documents configuration!");
}
boolean done = false;
Object arg = input.xargs()[0];
if (arg instanceof String) {
String address = docs != null ? (String)docs.get(input.args()[0]) : null;
String address = (String)docs.get(input.args()[0]);
if (address != null) {
done = true;
if (urlExists(address)) {
Expand All @@ -1086,33 +1096,33 @@ private Object doc(CommandInput input) {
}
name = name.replaceAll("\\.", "/") + ".html";
Object doc = null;
assert docs != null;
for (Map.Entry<String,Object> entry : docs.entrySet()) {
if (name.matches(entry.getKey())) {
doc = entry.getValue();
break;
}
}
boolean docFound = false;
if (doc != null) {
if (doc instanceof Collection) {
for (Object o : (Collection<?>) doc) {
String url = o + name;
if (urlExists(url)) {
Desktop.getDesktop().browse(new URI(url));
docFound = true;
}
}
} else {
String url = doc + name;
if (doc == null) {
throw new IllegalArgumentException("No document configuration for " + name);
}
String url = name;
if (doc instanceof Collection) {
for (Object o : (Collection<?>) doc) {
url = o + name;
if (urlExists(url)) {
Desktop.getDesktop().browse(new URI(url));
docFound = true;
done = true;
}
}
} else {
url = doc + name;
if (urlExists(url)) {
Desktop.getDesktop().browse(new URI(url));
done = true;
}
}
if (!docFound) {
throw new IllegalArgumentException("Document not found: " + name);
if (!done) {
throw new IllegalArgumentException("Document not found: " + url);
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -1227,10 +1237,13 @@ private List<Completer> unaliasCompleter(String command) {

private List<String> docs() {
List<String> out = new ArrayList<>();
Map<String,String> docs = consoleOption("docs", null);
if (docs == null) {
return out;
}
for (String v : engine.find().keySet()) {
out.add("$" + v);
}
Map<String,String> docs = consoleOption("docs", null);
if (!docs.isEmpty()) {
for (String d : docs.keySet()) {
if (d.matches("\\w+")) {
Expand Down

0 comments on commit f36c685

Please sign in to comment.