Compare commits

..

2 Commits

Author SHA1 Message Date
Hugo Locurcio
3ff2e435e8
Merge 542bef343a into 9e6098432a 2024-11-21 00:24:28 +00:00
Hugo Locurcio
542bef343a
Improve string formatting error messages
- Print additional context to make troubleshooting easier.
- Add the placeholder where the error occurred as a prefix to the error message.
- Use sentence case for all error messages.
2024-11-21 01:24:22 +01:00
2 changed files with 4 additions and 4 deletions

View File

@ -5882,7 +5882,7 @@ String String::sprintf(const Array &values, bool *error) const {
} else {
if (c == '0' && min_chars == 0) {
if (left_justified) {
WARN_PRINT("[%0]: The \"0\" flag is ignored with \"-\" flag in string formatting.");
WARN_PRINT("[%%0]: The \"0\" flag is ignored with \"-\" flag in string formatting.");
} else {
pad_with_zeros = true;
}
@ -5904,7 +5904,7 @@ String String::sprintf(const Array &values, bool *error) const {
case '*': { // Dynamic width, based on value.
if (value_index >= values.size()) {
return vformat("[%*]: Not enough arguments for string formatting (%d provided, but more were expected). Check for extraneous %% placeholders or missing arguments.", value_index);
return vformat("[%%*]: Not enough arguments for string formatting (%d provided, but more were expected). Check for extraneous %% placeholders or missing arguments.", value_index);
}
Variant::Type value_type = values[value_index].get_type();
@ -5912,7 +5912,7 @@ String String::sprintf(const Array &values, bool *error) const {
value_type != Variant::VECTOR2 && value_type != Variant::VECTOR2I &&
value_type != Variant::VECTOR3 && value_type != Variant::VECTOR3I &&
value_type != Variant::VECTOR4 && value_type != Variant::VECTOR4I) {
return vformat("[%*]: A number (int/float) or a vector type (Vector2/3/4/2i/3i/4i) is required, but a %s was provided.", Variant().get_type_name(values[value_index].get_type()));
return vformat("[%%*]: A number (int/float) or a vector type (Vector2/3/4/2i/3i/4i) is required, but a %s was provided.", Variant::get_type_name(value_type));
}
int size = values[value_index];

View File

@ -1229,7 +1229,7 @@ TEST_CASE("[String] sprintf") {
args.push_back("sc");
output = format.sprintf(args, &error);
REQUIRE(error);
CHECK(output == "[%c]: Requires a number or a single-character string, but a string of 2 characters was provided.");
CHECK(output == "[%c]: A number or a single-character string is required, but a string of 2 characters was provided.");
// Character bad type.
format = "fish %c frog";