Skip to content

Commit b8d0352

Browse files
committed
Issue #283: Add NameInfo information to SetValueExceptionError
1 parent 2377f59 commit b8d0352

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/CommandLine/Core/ReflectionExtensions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,24 @@ public static IEnumerable<Error> SetProperties<T>(
8686
Func<SpecificationProperty, bool> predicate,
8787
Func<SpecificationProperty, object> selector)
8888
{
89-
return specProps.Where(predicate).SelectMany(specProp => specProp.Property.SetValue(instance, selector(specProp)));
90-
}
91-
92-
private static IEnumerable<Error> SetValue<T>(this PropertyInfo property, T instance, object value)
93-
{
94-
try
95-
{
96-
property.SetValue(instance, value, null);
97-
return Enumerable.Empty<Error>();
98-
}
99-
catch (TargetInvocationException e)
100-
{
101-
return new[] { new SetValueExceptionError(e.InnerException) };
102-
}
103-
catch (Exception e)
104-
{
105-
return new[] { new SetValueExceptionError(e) };
106-
}
89+
return specProps.Where(predicate).SelectMany(specProp => specProp.SetValue(instance, selector(specProp)));
90+
}
91+
92+
private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value)
93+
{
94+
try
95+
{
96+
specProp.Property.SetValue(instance, value, null);
97+
return Enumerable.Empty<Error>();
98+
}
99+
catch (TargetInvocationException e)
100+
{
101+
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException) };
102+
}
103+
catch (Exception e)
104+
{
105+
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e) };
106+
}
107107
}
108108

109109
public static object CreateEmptyArray(this Type type)

src/CommandLine/Text/SentenceBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public override Func<Error, string> FormatError
138138
return "Option '".JoinTo(((RepeatedOptionError)error).NameInfo.NameText,
139139
"' is defined multiple times.");
140140
case ErrorType.SetValueExceptionError:
141-
return "Error setting option value: ".JoinTo(((SetValueExceptionError)error).Exception.Message);
141+
var setValueError = (SetValueExceptionError)error;
142+
return "Error setting value to option '".JoinTo(setValueError.NameInfo.NameText, "': ", setValueError.Exception.Message);
142143
}
143144
throw new InvalidOperationException();
144145
};

0 commit comments

Comments
 (0)