This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-23623 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 403a85dd02f8afecfcac6672b597c48a88cb513a Author: Claus Ibsen <[email protected]> AuthorDate: Wed May 27 18:08:53 2026 +0200 CAMEL-23623: Add Errors tab to Camel TUI Add a new Errors tab (key 0) showing captured routing errors from the ErrorRegistry in a master/detail layout. The master table displays AGO, ROUTE, NODE, HANDLED, EXCEPTION and MESSAGE columns. The detail pane shows exchange info, exception with stack trace, message history, variables, properties, headers and body with scrolling support. Co-Authored-By: Claude Opus 4.6 <[email protected]> --- .../dsl/jbang/core/commands/tui/CamelMonitor.java | 91 ++++++- .../dsl/jbang/core/commands/tui/ErrorInfo.java | 45 ++++ .../dsl/jbang/core/commands/tui/ErrorsTab.java | 300 +++++++++++++++++++++ .../jbang/core/commands/tui/IntegrationInfo.java | 1 + 4 files changed, 434 insertions(+), 3 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/CamelMonitor.java b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/CamelMonitor.java index 4995629b631a..31201caef382 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/CamelMonitor.java +++ b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/CamelMonitor.java @@ -104,7 +104,7 @@ public class CamelMonitor extends CamelCommand { private static final int MAX_ENDPOINT_CHART_POINTS = 60; private static final int MAX_LOG_LINES = 3000; private static final int MAX_TRACES = 200; - private static final int NUM_TABS = 9; + private static final int NUM_TABS = 10; // Tab indices private static final int TAB_OVERVIEW = 0; @@ -116,6 +116,7 @@ public class CamelMonitor extends CamelCommand { private static final int TAB_HEALTH = 6; private static final int TAB_HISTORY = 7; private static final int TAB_CIRCUIT_BREAKER = 8; + private static final int TAB_ERRORS = 9; // Overview sort columns private static final String[] OVERVIEW_SORT_COLUMNS = { "pid", "name", "version", "status", "total", "fail" }; @@ -263,6 +264,7 @@ public class CamelMonitor extends CamelCommand { private HealthTab healthTab; private HistoryTab historyTab; private CircuitBreakerTab circuitBreakerTab; + private ErrorsTab errorsTab; private ClassLoader classLoader; @@ -311,6 +313,7 @@ public class CamelMonitor extends CamelCommand { healthTab = new HealthTab(ctx); historyTab = new HistoryTab(ctx, traces, traceFilePositions); circuitBreakerTab = new CircuitBreakerTab(ctx, cbSuccessHistory, cbFailHistory); + errorsTab = new ErrorsTab(ctx); // Initial data load (synchronous before TUI starts) refreshDataSync(); @@ -459,6 +462,9 @@ public class CamelMonitor extends CamelCommand { if (ke.isChar('9')) { return handleTabKey(TAB_CIRCUIT_BREAKER); } + if (ke.isChar('0')) { + return handleTabKey(TAB_ERRORS); + } } // Tab cycling (check Shift+Tab before Tab since Tab binding also matches Shift+Tab) @@ -967,6 +973,7 @@ public class CamelMonitor extends CamelCommand { Line.from(" 7 Health "), Line.from(" 8 Inspect "), Line.from(" 9 Circuit Breaker "), + Line.from(" 0 Errors "), }; Tabs tabs = Tabs.builder() @@ -986,7 +993,7 @@ public class CamelMonitor extends CamelCommand { int badgeY = area.y(); int dividerW = CharWidth.of(" | "); - String[] badgeTexts = { "", "", "", "", "", "", "", "", "" }; + String[] badgeTexts = { "", "", "", "", "", "", "", "", "", "" }; Style[] badgeStyles = new Style[labels.length]; Style yellow = Style.EMPTY.fg(Color.YELLOW).bold(); Style cyan = Style.EMPTY.fg(Color.CYAN).bold(); @@ -1028,6 +1035,11 @@ public class CamelMonitor extends CamelCommand { } else if (cbCount > 0) { badgeTexts[TAB_CIRCUIT_BREAKER] = "(" + cbCount + ")"; } + int errorCount = hasSelection ? sel.errors.size() : 0; + if (errorCount > 0) { + badgeTexts[TAB_ERRORS] = "(" + errorCount + ")"; + badgeStyles[TAB_ERRORS] = red; + } int tabX = 0; for (int i = 0; i < labels.length; i++) { @@ -1068,6 +1080,7 @@ public class CamelMonitor extends CamelCommand { case TAB_HEALTH -> healthTab; case TAB_HISTORY -> historyTab; case TAB_HTTP -> httpTab; + case TAB_ERRORS -> errorsTab; default -> null; }; } @@ -2996,6 +3009,60 @@ public class CamelMonitor extends CamelCommand { } } + // Parse errors from error registry + JsonObject errorsObj = (JsonObject) root.get("errors"); + if (errorsObj != null) { + JsonArray errorList = (JsonArray) errorsObj.get("errors"); + if (errorList != null) { + for (Object e : errorList) { + JsonObject ej = (JsonObject) e; + ErrorInfo ei = new ErrorInfo(); + ei.routeId = ej.getString("routeId"); + ei.nodeId = ej.getString("nodeId"); + ei.exchangeId = ej.getString("exchangeId"); + ei.handled = ej.getBooleanOrDefault("handled", false); + Long ts = ej.getLong("timestamp"); + if (ts != null) { + ei.timestamp = ts; + } + ei.location = ej.getString("location"); + ei.threadName = ej.getString("threadName"); + Long elapsed = ej.getLong("elapsed"); + if (elapsed != null) { + ei.elapsed = elapsed; + } + ei.endpointUri = ej.getString("endpointUri"); + ei.fromEndpointUri = ej.getString("fromEndpointUri"); + // exception + JsonObject ex = (JsonObject) ej.get("exception"); + if (ex != null) { + ei.exceptionType = ex.getString("type"); + ei.exceptionMessage = ex.getString("message"); + ei.stackTrace = ex.getString("stackTrace"); + } + // message history + Object mhObj = ej.get("messageHistory"); + if (mhObj instanceof JsonArray mhArr) { + ei.messageHistory = new String[mhArr.size()]; + for (int i = 0; i < mhArr.size(); i++) { + ei.messageHistory[i] = mhArr.get(i).toString(); + } + } + // message (body, headers) + JsonObject msg = (JsonObject) ej.get("message"); + if (msg != null) { + ei.body = msg.getString("body"); + ei.bodyType = msg.getString("bodyType"); + parseKvArray(msg.getCollection("headers"), ei.headers, ei.headerTypes); + } + // exchange properties and variables + parseKvArray(ej.getCollection("exchangeProperties"), ei.properties, ei.propertyTypes); + parseKvArray(ej.getCollection("exchangeVariables"), ei.variables, ei.variableTypes); + info.errors.add(ei); + } + } + } + // Parse REST DSL services JsonObject restsObj = (JsonObject) root.get("rests"); if (restsObj != null) { @@ -3107,6 +3174,24 @@ public class CamelMonitor extends CamelCommand { } } + @SuppressWarnings("unchecked") + private static void parseKvArray(JsonArray arr, Map<String, Object> values, Map<String, String> types) { + if (arr == null) { + return; + } + for (Object o : arr) { + JsonObject jo = (JsonObject) o; + String key = jo.getString("key"); + if (key != null) { + values.put(key, jo.get("value")); + String type = jo.getString("type"); + if (type != null) { + types.put(key, type); + } + } + } + } + // ---- Helpers ---- private IntegrationInfo findSelectedIntegration() { @@ -3208,7 +3293,7 @@ public class CamelMonitor extends CamelCommand { private static final String[] TAB_NAMES = { "Overview", "Log", "Routes", "Consumers", "Endpoints", - "HTTP", "Health", "Inspect", "Circuit Breaker" + "HTTP", "Health", "Inspect", "Circuit Breaker", "Errors" }; Buffer getLastBuffer() { diff --git a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorInfo.java b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorInfo.java new file mode 100644 index 000000000000..d01b5026412e --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorInfo.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.tui; + +import java.util.LinkedHashMap; +import java.util.Map; + +class ErrorInfo { + String routeId; + String nodeId; + String exchangeId; + boolean handled; + long timestamp; + String location; + String threadName; + long elapsed; + String endpointUri; + String fromEndpointUri; + String exceptionType; + String exceptionMessage; + String stackTrace; + String[] messageHistory; + String body; + String bodyType; + final Map<String, Object> headers = new LinkedHashMap<>(); + final Map<String, String> headerTypes = new LinkedHashMap<>(); + final Map<String, Object> properties = new LinkedHashMap<>(); + final Map<String, String> propertyTypes = new LinkedHashMap<>(); + final Map<String, Object> variables = new LinkedHashMap<>(); + final Map<String, String> variableTypes = new LinkedHashMap<>(); +} diff --git a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorsTab.java b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorsTab.java new file mode 100644 index 000000000000..c74e3c7ceb39 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ErrorsTab.java @@ -0,0 +1,300 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.tui; + +import java.util.ArrayList; +import java.util.List; + +import dev.tamboui.layout.Constraint; +import dev.tamboui.layout.Layout; +import dev.tamboui.layout.Rect; +import dev.tamboui.style.Color; +import dev.tamboui.style.Style; +import dev.tamboui.terminal.Frame; +import dev.tamboui.text.Line; +import dev.tamboui.text.Span; +import dev.tamboui.tui.event.KeyEvent; +import dev.tamboui.widgets.block.Block; +import dev.tamboui.widgets.block.BorderType; +import dev.tamboui.widgets.scrollbar.ScrollbarState; +import dev.tamboui.widgets.table.Cell; +import dev.tamboui.widgets.table.Row; +import dev.tamboui.widgets.table.Table; +import dev.tamboui.widgets.table.TableState; + +import static org.apache.camel.dsl.jbang.core.commands.tui.MonitorContext.*; + +class ErrorsTab implements MonitorTab { + + private static final String[] SORT_COLUMNS = { "age", "route", "exception" }; + + private final MonitorContext ctx; + private final TableState tableState = new TableState(); + private final ScrollbarState detailScrollState = new ScrollbarState(); + private String sort = "age"; + private int sortIndex; + private boolean sortReversed; + private int detailScroll; + private int detailHScroll; + private boolean wordWrap = true; + + ErrorsTab(MonitorContext ctx) { + this.ctx = ctx; + } + + @Override + public void onTabSelected() { + IntegrationInfo info = ctx.findSelectedIntegration(); + if (info != null && !info.errors.isEmpty() && tableState.selected() == null) { + tableState.select(0); + } + } + + @Override + public boolean handleKeyEvent(KeyEvent ke) { + if (ke.isChar('s')) { + sortIndex = (sortIndex + 1) % SORT_COLUMNS.length; + sort = SORT_COLUMNS[sortIndex]; + sortReversed = false; + return true; + } + if (ke.isChar('S')) { + sortReversed = !sortReversed; + return true; + } + if (ke.isCharIgnoreCase('w')) { + wordWrap = !wordWrap; + return true; + } + if (ke.isPageUp()) { + detailScroll = Math.max(0, detailScroll - 5); + return true; + } + if (ke.isPageDown()) { + detailScroll += 5; + return true; + } + if (ke.isLeft() && !wordWrap) { + detailHScroll = Math.max(0, detailHScroll - 4); + return true; + } + if (ke.isRight() && !wordWrap) { + detailHScroll += 4; + return true; + } + return false; + } + + @Override + public boolean handleEscape() { + return false; + } + + @Override + public void navigateUp() { + detailScroll = 0; + tableState.selectPrevious(); + } + + @Override + public void navigateDown() { + detailScroll = 0; + IntegrationInfo info = ctx.findSelectedIntegration(); + tableState.selectNext(info != null ? info.errors.size() : 0); + } + + @Override + public void render(Frame frame, Rect area) { + IntegrationInfo info = ctx.findSelectedIntegration(); + if (info == null) { + renderNoSelection(frame, area); + return; + } + + List<ErrorInfo> sorted = new ArrayList<>(info.errors); + sorted.sort(this::sortError); + + List<Row> rows = new ArrayList<>(); + for (ErrorInfo ei : sorted) { + String ago = ei.timestamp > 0 + ? org.apache.camel.util.TimeUtils.printSince(ei.timestamp) : ""; + String handledStr = ei.handled ? "true" : "false"; + Style handledStyle = ei.handled + ? Style.EMPTY.fg(Color.GREEN) : Style.EMPTY.fg(Color.LIGHT_RED); + String shortException = shortExceptionType(ei.exceptionType); + + rows.add(Row.from( + Cell.from(ago), + Cell.from(Span.styled(ei.routeId != null ? ei.routeId : "", Style.EMPTY.fg(Color.CYAN))), + Cell.from(ei.nodeId != null ? ei.nodeId : ""), + Cell.from(Span.styled(handledStr, handledStyle)), + Cell.from(shortException), + Cell.from(ei.exceptionMessage != null ? ei.exceptionMessage : ""))); + } + + if (rows.isEmpty()) { + rows.add(Row.from( + Cell.from(Span.styled("No errors captured", Style.EMPTY.dim())), + Cell.from(""), Cell.from(""), Cell.from(""), + Cell.from(""), Cell.from(""))); + } + + ErrorInfo selectedError = null; + Integer sel = tableState.selected(); + if (sel != null && sel >= 0 && sel < sorted.size()) { + selectedError = sorted.get(sel); + } + boolean showDetail = selectedError != null; + List<Rect> chunks = showDetail + ? Layout.vertical() + .constraints(Constraint.length(Math.min(sorted.size() + 3, 12)), Constraint.fill()) + .split(area) + : List.of(area); + + Table table = Table.builder() + .rows(rows) + .header(Row.from( + Cell.from(Span.styled(sortLabel("AGO", "age"), sortStyle("age"))), + Cell.from(Span.styled(sortLabel("ROUTE", "route"), sortStyle("route"))), + Cell.from(Span.styled("NODE", Style.EMPTY.bold())), + Cell.from(Span.styled("HANDLED", Style.EMPTY.bold())), + Cell.from(Span.styled(sortLabel("EXCEPTION", "exception"), sortStyle("exception"))), + Cell.from(Span.styled("MESSAGE", Style.EMPTY.bold())))) + .widths( + Constraint.length(8), + Constraint.length(20), + Constraint.length(20), + Constraint.length(8), + Constraint.length(30), + Constraint.fill()) + .highlightStyle(Style.EMPTY.fg(Color.WHITE).bold().onBlue()) + .highlightSpacing(Table.HighlightSpacing.ALWAYS) + .block(Block.builder().borderType(BorderType.ROUNDED).title(" Errors ").build()) + .build(); + + frame.renderStatefulWidget(table, chunks.get(0), tableState); + + if (showDetail) { + renderDetail(frame, chunks.get(1), selectedError); + } + } + + @Override + public void renderFooter(List<Span> spans) { + hint(spans, "Esc", "back"); + hint(spans, "↑↓", "navigate"); + hint(spans, "s", "sort"); + hint(spans, "w", "wrap"); + hint(spans, "PgUp/Dn", "scroll"); + hint(spans, "1-0", "tabs"); + } + + private void renderDetail(Frame frame, Rect area, ErrorInfo ei) { + List<Line> lines = new ArrayList<>(); + + HistoryTab.addExchangeInfoLines(lines, + ei.exchangeId, ei.routeId, ei.nodeId, null, ei.location, + ei.elapsed, ei.threadName, !ei.handled); + + // exception with stack trace + String exception = null; + if (ei.exceptionType != null) { + StringBuilder sb = new StringBuilder(); + sb.append(ei.exceptionType); + if (ei.exceptionMessage != null) { + sb.append(": ").append(ei.exceptionMessage); + } + if (ei.stackTrace != null) { + sb.append("\n").append(ei.stackTrace); + } + exception = sb.toString(); + } + HistoryTab.addExceptionLines(lines, exception); + + // message history + if (ei.messageHistory != null && ei.messageHistory.length > 0) { + lines.add(Line.from(Span.styled(" Message History:", Style.EMPTY.fg(Color.MAGENTA).bold()))); + for (String step : ei.messageHistory) { + lines.add(Line.from(Span.raw(" " + step))); + } + lines.add(Line.from(Span.raw(""))); + } + + // variables, properties, headers + if (!ei.variables.isEmpty()) { + HistoryTab.addKvLines(lines, " Variables:", ei.variables, ei.variableTypes); + } + if (!ei.properties.isEmpty()) { + HistoryTab.addKvLines(lines, " Properties:", ei.properties, ei.propertyTypes); + } + if (!ei.headers.isEmpty()) { + HistoryTab.addKvLines(lines, " Headers:", ei.headers, ei.headerTypes); + } + + // body + HistoryTab.addBodyLines(lines, ei.body, ei.bodyType); + + int[] scroll = { detailScroll }; + int[] hScroll = { detailHScroll }; + HistoryTab.renderDetailPanel(frame, area, lines, wordWrap, hScroll, scroll, detailScrollState); + detailScroll = scroll[0]; + detailHScroll = hScroll[0]; + } + + private String sortLabel(String label, String column) { + return MonitorContext.sortLabel(label, column, sort, sortReversed); + } + + private Style sortStyle(String column) { + return MonitorContext.sortStyle(column, sort); + } + + private int sortError(ErrorInfo a, ErrorInfo b) { + int result = switch (sort) { + case "route" -> compareStr(a.routeId, b.routeId); + case "exception" -> compareStr(a.exceptionType, b.exceptionType); + default -> Long.compare(b.timestamp, a.timestamp); // newest first + }; + return sortReversed ? -result : result; + } + + private static String shortExceptionType(String type) { + if (type == null) { + return ""; + } + int dot = type.lastIndexOf('.'); + if (dot > 0) { + return type.substring(dot + 1); + } + return type; + } + + @Override + public SelectionContext getSelectionContext() { + IntegrationInfo info = ctx.findSelectedIntegration(); + if (info == null || info.errors.isEmpty()) { + return null; + } + List<ErrorInfo> sorted = new ArrayList<>(info.errors); + sorted.sort(this::sortError); + List<String> items = sorted.stream() + .map(e -> e.exchangeId != null ? e.exchangeId : "") + .toList(); + Integer sel = tableState.selected(); + return new SelectionContext("table", items, sel != null ? sel : -1, items.size(), "Errors"); + } +} diff --git a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/IntegrationInfo.java b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/IntegrationInfo.java index 921ab26ad816..ffaa18c73fbb 100644 --- a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/IntegrationInfo.java +++ b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/IntegrationInfo.java @@ -62,6 +62,7 @@ class IntegrationInfo { final List<HealthCheckInfo> healthChecks = new ArrayList<>(); final List<EndpointInfo> endpoints = new ArrayList<>(); final List<CircuitBreakerInfo> circuitBreakers = new ArrayList<>(); + final List<ErrorInfo> errors = new ArrayList<>(); final List<HttpEndpointInfo> httpEndpoints = new ArrayList<>(); String httpServer; String readmeFiles;
