From 74c22372966bcd90fbe31d3c6f1188d01b1f57a7 Mon Sep 17 00:00:00 2001 From: Tim Stack Date: Tue, 16 Aug 2022 22:39:24 -0700 Subject: [PATCH] [loader] add some help text to the error for values without a corresponding capture --- src/log_format.cc | 20 ++++++++++++++++++- ...1d6f35e8a9ae4fd3e91ffaa82a037b5a847ab7.err | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/log_format.cc b/src/log_format.cc index b9932de5..72d08542 100644 --- a/src/log_format.cc +++ b/src/log_format.cc @@ -1904,6 +1904,8 @@ external_log_format::build(std::vector& errors) } if (this->elf_type == elf_type_t::ELF_TYPE_TEXT) { + std::set available_captures; + bool found_in_pattern = false; for (const auto& pat : this->elf_patterns) { auto cap_index = pat.second->p_pcre->name_index( @@ -1912,8 +1914,20 @@ external_log_format::build(std::vector& errors) found_in_pattern = true; break; } + + for (auto name_iter = pat.second->p_pcre->named_begin(); + name_iter != pat.second->p_pcre->named_end(); + ++name_iter) + { + available_captures.insert(name_iter->pnc_name); + } } if (!found_in_pattern) { + auto notes + = attr_line_t("the following captures are available:\n ") + .join(available_captures, + VC_ROLE.value(role_t::VCR_SYMBOL), + ", "); errors.emplace_back( lnav::console::user_message::error( attr_line_t("invalid value ") @@ -1924,7 +1938,11 @@ external_log_format::build(std::vector& errors) .with_reason( attr_line_t("no patterns have a capture named ") .append_quoted(vd->vd_meta.lvm_name.get())) - .with_snippets(this->get_snippets())); + .with_note(notes) + .with_snippets(this->get_snippets()) + .with_help("values are populated from captures in " + "patterns, so at least one pattern must " + "have a capture with this value name")); } } diff --git a/test/expected/test_format_loader.sh_3f1d6f35e8a9ae4fd3e91ffaa82a037b5a847ab7.err b/test/expected/test_format_loader.sh_3f1d6f35e8a9ae4fd3e91ffaa82a037b5a847ab7.err index 69a66d99..5394f53c 100644 --- a/test/expected/test_format_loader.sh_3f1d6f35e8a9ae4fd3e91ffaa82a037b5a847ab7.err +++ b/test/expected/test_format_loader.sh_3f1d6f35e8a9ae4fd3e91ffaa82a037b5a847ab7.err @@ -101,6 +101,9 @@ ✘ error: invalid value “/invalid_props_log/value/non-existent” reason: no patterns have a capture named “non-existent”  --> {test_dir}/bad-config/formats/invalid-properties/format.json:4 + = note: the following captures are available: + body, pid, timestamp + = help: values are populated from captures in patterns, so at least one pattern must have a capture with this value name ✘ error: invalid value for property “/invalid_props_log/timestamp-field” reason: “ts” was not found in the pattern at /invalid_props_log/regex/std  --> {test_dir}/bad-config/formats/invalid-properties/format.json:4