174{
175 auto result = string_to_enum_case_insensitive<EnumType>(str);
176 if (!result.has_value()) {
177 std::string error_msg = "Invalid enum value: '" + std::string(str) + "'";
178 if (!context.empty()) {
179 error_msg += " for " + std::string(context);
180 }
181
182 error_msg += ". Valid values are: ";
183 auto names = get_enum_names_lowercase<EnumType>();
184 for (size_t i = 0; i < names.size(); ++i) {
185 if (i > 0)
186 error_msg += ", ";
187 error_msg += names[i];
188 }
189
190 throw std::invalid_argument(error_msg);
191 }
192 return *result;
193}