AK: Print a better error message when missing a SourceGenerator key

Previously, if you forgot to set a key on a SourceGenerator, you would
get this less-than-helpful error message:

> Generate_CSS_MediaFeatureID_cpp:
  /home/sam/serenity/Meta/Lagom/../../AK/Optional.h:174: T
  AK::Optional<T>::release_value() [with T = AK::String]: Assertion
  `m_has_value' failed.

Now, it instead looks like this:

> No key named `name:titlecase` set on SourceGenerator
  Generate_CSS_MediaFeatureID_cpp:
  /home/sam/serenity/Meta/Lagom/../../AK/SourceGenerator.h:44:
  AK::String AK::SourceGenerator::get(AK::StringView) const: Assertion
  `false' failed.
This commit is contained in:
Sam Atkins 2022-03-08 17:29:45 +00:00 committed by Andreas Kling
parent 12561327d6
commit a451810599

View file

@ -36,7 +36,15 @@ public:
SourceGenerator fork() { return SourceGenerator { m_builder, m_mapping, m_opening, m_closing }; }
void set(StringView key, String value) { m_mapping.set(key, value); }
String get(StringView key) const { return m_mapping.get(key).value(); }
String get(StringView key) const
{
auto result = m_mapping.get(key);
if (!result.has_value()) {
warnln("No key named `{}` set on SourceGenerator", key);
VERIFY_NOT_REACHED();
}
return result.release_value();
}
StringView as_string_view() const { return m_builder.string_view(); }
String as_string() const { return m_builder.build(); }