MINOR: http-rules: suggest approaching action names on mismatch
authorWilly Tarreau <w@1wt.eu>
Fri, 12 Mar 2021 11:01:34 +0000 (12:01 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 12 Mar 2021 13:13:21 +0000 (14:13 +0100)
This adds support for action_suggest() in http-request, http-response
and http-after-response rulesets. For example:

   parsing [/dev/stdin:2]: 'http-request' expects (...), but got 'del-hdr'. Did you mean 'del-header' maybe ?

src/http_rules.c

index 9271112..a34c560 100644 (file)
@@ -76,7 +76,7 @@ struct action_kw *action_http_after_res_custom(const char *kw)
 struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
 {
        struct act_rule *rule;
-       struct action_kw *custom = NULL;
+       const struct action_kw *custom = NULL;
        int cur_arg;
 
        rule = calloc(1, sizeof(*rule));
@@ -104,10 +104,15 @@ struct act_rule *parse_http_req_cond(const char **args, const char *file, int li
                }
        }
        else {
+               const char *best = action_suggest(args[0], &http_req_keywords.list, NULL);
+
                action_build_list(&http_req_keywords.list, &trash);
-               ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.\n",
-                        file, linenum, trash.area,
-                        args[0], *args[0] ? "" : " (missing argument)");
+               ha_alert("parsing [%s:%d]: 'http-request' expects %s, but got '%s'%s.%s%s%s\n",
+                        file, linenum, trash.area,
+                        args[0], *args[0] ? "" : " (missing argument)",
+                        best ? " Did you mean '" : "",
+                        best ? best : "",
+                        best ? "' maybe ?" : "");
                goto out_err;
        }
 
@@ -140,7 +145,7 @@ struct act_rule *parse_http_req_cond(const char **args, const char *file, int li
 struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
 {
        struct act_rule *rule;
-       struct action_kw *custom = NULL;
+       const struct action_kw *custom = NULL;
        int cur_arg;
 
        rule = calloc(1, sizeof(*rule));
@@ -168,10 +173,15 @@ struct act_rule *parse_http_res_cond(const char **args, const char *file, int li
                }
        }
        else {
+               const char *best = action_suggest(args[0], &http_res_keywords.list, NULL);
+
                action_build_list(&http_res_keywords.list, &trash);
-               ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.\n",
-                        file, linenum, trash.area,
-                        args[0], *args[0] ? "" : " (missing argument)");
+               ha_alert("parsing [%s:%d]: 'http-response' expects %s, but got '%s'%s.%s%s%s\n",
+                        file, linenum, trash.area,
+                        args[0], *args[0] ? "" : " (missing argument)",
+                        best ? " Did you mean '" : "",
+                        best ? best : "",
+                        best ? "' maybe ?" : "");
                goto out_err;
        }
 
@@ -205,7 +215,7 @@ struct act_rule *parse_http_res_cond(const char **args, const char *file, int li
 struct act_rule *parse_http_after_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
 {
        struct act_rule *rule;
-       struct action_kw *custom = NULL;
+       const struct action_kw *custom = NULL;
        int cur_arg;
 
        rule = calloc(1, sizeof(*rule));
@@ -233,10 +243,15 @@ struct act_rule *parse_http_after_res_cond(const char **args, const char *file,
                }
        }
        else {
+               const char *best = action_suggest(args[0], &http_after_res_keywords.list, NULL);
+
                action_build_list(&http_after_res_keywords.list, &trash);
-               ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.\n",
-                        file, linenum, trash.area,
-                        args[0], *args[0] ? "" : " (missing argument)");
+               ha_alert("parsing [%s:%d]: 'http-after-response' expects %s, but got '%s'%s.%s%s%s\n",
+                        file, linenum, trash.area,
+                        args[0], *args[0] ? "" : " (missing argument)",
+                        best ? " Did you mean '" : "",
+                        best ? best : "",
+                        best ? "' maybe ?" : "");
                goto out_err;
        }