From f36c685b5e8da770eb0ef191d4deae049a6abd9b Mon Sep 17 00:00:00 2001 From: mattirn Date: Tue, 29 Dec 2020 11:05:42 +0100 Subject: [PATCH] Command doc: improved exceptions --- .../jline/console/impl/ConsoleEngineImpl.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java index 69e56f109..c05c99a4b 100644 --- a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java +++ b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java @@ -1063,11 +1063,21 @@ private Object doc(CommandInput input) { if (!Desktop.isDesktopSupported()) { throw new IllegalStateException("Desktop is not supported!"); } - Map docs = consoleOption("docs", null); + Map 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)) { @@ -1086,33 +1096,33 @@ private Object doc(CommandInput input) { } name = name.replaceAll("\\.", "/") + ".html"; Object doc = null; - assert docs != null; for (Map.Entry 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) { @@ -1227,10 +1237,13 @@ private List unaliasCompleter(String command) { private List docs() { List out = new ArrayList<>(); + Map docs = consoleOption("docs", null); + if (docs == null) { + return out; + } for (String v : engine.find().keySet()) { out.add("$" + v); } - Map docs = consoleOption("docs", null); if (!docs.isEmpty()) { for (String d : docs.keySet()) { if (d.matches("\\w+")) {