gnodet-bot commented on code in PR #28586: URL: https://github.com/apache/camel/pull/28586#discussion_r4391747446
########## dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/RouteDiagramProblems.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.action; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.camel.catalog.CamelCatalog; +import org.apache.camel.catalog.DefaultCamelCatalog; +import org.apache.camel.diagram.RouteDiagramLayoutEngine.NodeInfo; +import org.apache.camel.diagram.RouteDiagramLayoutEngine.RouteInfo; +import org.apache.camel.dsl.jbang.core.commands.ai.RouteAssist; +import org.apache.camel.dsl.jbang.core.commands.ai.SourceValidator; + +/** + * The problems of the source files of a route diagram, on the steps they are of: the checks of {@code camel validate} + * for a YAML route, and for Java and XML the checks that read the source without compiling it ({@link RouteAssist}), as + * the route diagram reads Java routes without compiling them. + */ +final class RouteDiagramProblems { + + /** A problem of a line of the source; line 0 for a problem of the file. */ + record Problem(int line, String text) { + } + + private static final Pattern MESSAGE_LINE = Pattern.compile("^Line (\\d+): (.*)$", Pattern.DOTALL); + // the step and route a message names first, which the diagram shows already: "setBody: route orders: " + private static final Pattern STEP_PREFIX = Pattern.compile("^[A-Za-z][\\w-]*: route [^:\\s]+: "); + + private RouteDiagramProblems() { + } + + /** + * Puts the problems of the source files on the steps of their routes ({@link NodeInfo#problems}): a problem on the + * step whose line is the last one before it (the line of the option or expression that is wrong is inside its + * step), a problem of the file on the first step. + */ + static void attach(List<RouteInfo> routes, List<String> files) { + CamelCatalog catalog = null; + for (String file : files) { + Path path = Path.of(file); + if (!Files.isRegularFile(path)) { + continue; + } + String name = path.getFileName().toString(); + List<NodeInfo> nodes = routes.stream() + .filter(r -> sourceOf(r, name)) + .flatMap(r -> r.nodes.stream()) + .toList(); + if (nodes.isEmpty()) { + continue; + } + String content; + try { + content = Files.readString(path); + } catch (IOException e) { + continue; Review Comment: 💡 **`catch (IOException e) { continue; }` silently skips readable-path failures** At this point the file passed `Files.isRegularFile(path)` and the user explicitly listed it as a source, but if `Files.readString` throws (e.g. a permission error or encoding issue), the exception is swallowed and the file produces no problems — as if it had none. The user gets no feedback. Consider logging a warning (compare with `SemanticAudit.java` in the same package) or re-throwing as `UncheckedIOException` so the caller can decide how to handle it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
