Improve conversion error messages

When a conversion to an enum fails, list all the valid enum values in the error message.

Message before this commit:
> Error: heimday is not a valid value for DayOfWeek.

Message after this commit:
> Error: Failed to convert 'heimday' to DayOfWeek. Valid values are 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
This commit is contained in:
Cédric Luthi
2023-01-24 19:38:03 +01:00
parent 7b9553dd22
commit de847b90e4
8 changed files with 83 additions and 10 deletions

View File

@ -84,8 +84,18 @@ internal static class CommandValueResolver
throw CommandRuntimeException.NoConverterFound(mapped.Parameter);
}
object? value;
try
{
value = converter.ConvertFromInvariantString(mapped.Value ?? string.Empty);
}
catch (Exception exception)
{
throw CommandRuntimeException.ConversionFailed(mapped, converter, exception);
}
// Assign the value to the parameter.
binder.Bind(mapped.Parameter, resolver, converter.ConvertFromInvariantString(mapped.Value ?? string.Empty));
binder.Bind(mapped.Parameter, resolver, value);
}
}