|
32 | 32 | import java.util.ArrayList; |
33 | 33 | import java.util.Collections; |
34 | 34 | import java.util.List; |
| 35 | +import java.util.Map; |
35 | 36 | import java.util.Optional; |
| 37 | +import java.util.regex.PatternSyntaxException; |
36 | 38 |
|
37 | 39 | import org.scijava.Context; |
38 | 40 | import org.scijava.log.LogService; |
39 | 41 | import org.scijava.plugin.Parameter; |
40 | 42 | import org.scijava.plugin.PluginService; |
41 | 43 | import org.scijava.thread.ThreadService; |
| 44 | +import org.scijava.util.DebugUtils; |
42 | 45 |
|
43 | 46 | /** |
44 | 47 | * Default implementation of {@link SearchOperation}. |
@@ -164,8 +167,46 @@ public void run() { |
164 | 167 | final boolean supported = searcher.supports(query); |
165 | 168 | final boolean enabled = searchService.enabled(searcher); |
166 | 169 | if (!valid) return; |
167 | | - final List<SearchResult> results = supported ? (enabled ? // |
168 | | - searcher.search(query, fuzzy) : Collections.emptyList()) : null; |
| 170 | + List<SearchResult> results; |
| 171 | + try { |
| 172 | + if (!supported) results = null; |
| 173 | + else if (!enabled) results = Collections.emptyList(); |
| 174 | + else results = searcher.search(query, fuzzy); |
| 175 | + } |
| 176 | + catch (final Throwable t) { |
| 177 | + // NB: Be defensive about errors. |
| 178 | + results = Collections.singletonList(new SearchResult() { |
| 179 | + |
| 180 | + private final Map<String, String> props = // |
| 181 | + Collections.singletonMap(null, errorMessage()); |
| 182 | + |
| 183 | + @Override |
| 184 | + public String name() { |
| 185 | + return "<error>"; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public String iconPath() { |
| 190 | + return null; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public Map<String, String> properties() { |
| 195 | + return props; |
| 196 | + } |
| 197 | + |
| 198 | + private String errorMessage() { |
| 199 | + if (t instanceof PatternSyntaxException) { |
| 200 | + return pre(t.getMessage()); |
| 201 | + } |
| 202 | + return pre(DebugUtils.getStackTrace(t)); |
| 203 | + } |
| 204 | + |
| 205 | + private String pre(final String s) { |
| 206 | + return "<pre style=\"font-size: 0.9em\">" + s + "</pre>"; |
| 207 | + } |
| 208 | + }); |
| 209 | + } |
169 | 210 | if (!valid) return; |
170 | 211 | for (final SearchListener l : listeners) { |
171 | 212 | l.searchCompleted(new SearchEvent(searcher, results, exclusive)); |
|
0 commit comments