gnodet-bot commented on code in PR #29347:
URL: https://github.com/apache/camel/pull/29347#discussion_r4494556926


##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/ChaosRecipes.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+
+/**
+ * The known rules of camel-chaos (the recipes of the chaos model of the 
catalog, {@code CamelCatalog.chaosModelJSon()},
+ * CAMEL-26689): what goes wrong in plain words, each a one-line rule with 
{@code ${name}} placeholders, and where to
+ * pick the value of a placeholder from in the integration (the endpoints its 
routes send to, the endpoints it polls,
+ * its routes, its steps, or listed values).
+ */
+final class ChaosRecipes {
+
+    /** A placeholder of a recipe. */
+    record Placeholder(String name, String type, String description, String 
defaultValue, String example, String pickFrom,
+            List<String> schemes, List<String> values) {
+    }
+
+    /** A known rule. */
+    record Recipe(String id, String group, String title, String description, 
String rule, List<Placeholder> placeholders) {
+
+        /** The rule with the values of the placeholders; a placeholder 
without a value stays {@code ${name}}. */
+        String fill(Map<String, String> values) {
+            String answer = rule;
+            for (Placeholder p : placeholders) {
+                String value = values.get(p.name());
+                if (value != null && !value.isBlank()) {
+                    answer = answer.replace("${" + p.name() + "}", 
value.strip());
+                }
+            }
+            return answer;
+        }
+    }
+
+    private final List<Recipe> recipes;
+
+    ChaosRecipes(List<Recipe> recipes) {
+        this.recipes = List.copyOf(recipes);
+    }
+
+    List<Recipe> recipes() {
+        return recipes;
+    }
+
+    /**
+     * The recipes of the catalog of the Camel CLI, or null when its catalog 
has no chaos model (a Camel version before
+     * it).
+     */
+    static ChaosRecipes load() {
+        try {
+            String json = new DefaultCamelCatalog().chaosModelJSon();
+            return json != null ? parse(json) : null;
+        } catch (RuntimeException | LinkageError e) {
+            // an older catalog: the rule is typed as one line
+            return null;
+        }
+    }
+

Review Comment:
   💡 **Minor nit:** `RuntimeException` is too broad here — the comment says "an 
older catalog" which is a classloading concern (`NoSuchMethodError`), so only 
`LinkageError` is needed. `RuntimeException` would also swallow unexpected bugs 
silently.
   
   ```suggestion
           } catch (NoSuchMethodError e) {
   ```
   
   Or if other `LinkageError` subtypes are also possible:
   ```
           } catch (LinkageError e) {
   ```



-- 
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]

Reply via email to