Skip to content

Commit 273d1e6

Browse files
Fixes handling invalid value type for options. Closes #319 (#797)
1 parent a487f60 commit 273d1e6

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

dev-proxy/ProxyHost.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,52 @@ public ProxyHost()
225225
_rateOption.ArgumentHelpName = "failure rate";
226226
_rateOption.AddValidator((input) =>
227227
{
228-
int? value = input.GetValueForOption(_rateOption);
229-
if (value.HasValue && (value < 0 || value > 100))
228+
try
230229
{
231-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
230+
int? value = input.GetValueForOption(_rateOption);
231+
if (value.HasValue && (value < 0 || value > 100))
232+
{
233+
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
234+
}
235+
}
236+
catch (InvalidOperationException ex)
237+
{
238+
input.ErrorMessage = ex.Message;
232239
}
233240
});
234241

235242
_noFirstRunOption = new Option<bool?>(NoFirstRunOptionName, "Skip the first run experience");
236243

237244
_asSystemProxyOption = new Option<bool?>(AsSystemProxyOptionName, "Set Dev Proxy as the system proxy");
238245
_asSystemProxyOption.SetDefaultValue(true);
246+
_asSystemProxyOption.AddValidator(input =>
247+
{
248+
try
249+
{
250+
_ = input.GetValueForOption(_asSystemProxyOption);
251+
}
252+
catch (InvalidOperationException ex)
253+
{
254+
input.ErrorMessage = ex.Message;
255+
}
256+
});
239257

240258
_installCertOption = new Option<bool?>(InstallCertOptionName, "Install self-signed certificate");
241259
_installCertOption.SetDefaultValue(true);
242-
_installCertOption.AddValidator((input) =>
260+
_installCertOption.AddValidator(input =>
243261
{
244-
var asSystemProxy = input.GetValueForOption(_asSystemProxyOption) ?? true;
245-
var installCert = input.GetValueForOption(_installCertOption) ?? true;
246-
if (asSystemProxy && !installCert)
262+
try
263+
{
264+
var asSystemProxy = input.GetValueForOption(_asSystemProxyOption) ?? true;
265+
var installCert = input.GetValueForOption(_installCertOption) ?? true;
266+
if (asSystemProxy && !installCert)
267+
{
268+
input.ErrorMessage = $"Requires option '--{_asSystemProxyOption.Name}' to be 'false'";
269+
}
270+
}
271+
catch (InvalidOperationException ex)
247272
{
248-
input.ErrorMessage = $"Requires option '--{_asSystemProxyOption.Name}' to be 'false'";
273+
input.ErrorMessage = ex.Message;
249274
}
250275
});
251276

0 commit comments

Comments
 (0)