Skip to content

Commit ea0d1b6

Browse files
Aggregate exceptions within ParseSetingsHashtable(Hashtable)
1 parent dca15a8 commit ea0d1b6

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

Engine/Settings.cs

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -324,70 +324,84 @@ private bool ParseSettingValueBoolean(object value, string settingName)
324324

325325
private void ParseSettingsHashtable(Hashtable settings)
326326
{
327+
IList<Exception> exceptions = new List<Exception>();
328+
327329
ISet<string> uniqueSettingKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
328330
foreach (DictionaryEntry setting in settings)
329331
{
330332
if (setting.Key is null)
331333
{
332-
throw new InvalidDataException(Strings.SettingKeyIsNull);
334+
exceptions.Add(new InvalidDataException(
335+
Strings.SettingKeyIsNull));
336+
continue;
333337
}
334338

335339
if (!(setting.Key is string))
336340
{
337-
throw new InvalidDataException(string.Format(
341+
exceptions.Add(new InvalidDataException(string.Format(
338342
Strings.SettingKeyIsNotStringType,
339-
setting.Key));
343+
setting.Key)));
344+
continue;
340345
}
341346
string settingName = setting.Key as string;
342347

343348
if (!uniqueSettingKeys.Add(settingName))
344349
{
345350
// setting.Key should be used instead of settingName because the former preserves information about the source casing.
346-
throw new InvalidDataException(string.Format(
351+
exceptions.Add(new InvalidDataException(string.Format(
347352
Strings.SettingKeyIsNotUniqueIgnoringCase,
348-
setting.Key));
353+
setting.Key)));
354+
continue;
349355
}
350356

351357
if (setting.Value is null)
352358
{
353-
throw new InvalidDataException(string.Format(
359+
exceptions.Add(new InvalidDataException(string.Format(
354360
Strings.SettingValueIsNull,
355-
settingName));
361+
settingName)));
362+
continue;
356363
}
357364

358365
// ToLowerInvariant is important to also work with turkish culture, see https://github.com/PowerShell/PSScriptAnalyzer/issues/1095
359366
switch (settingName.ToLowerInvariant())
360367
{
361368
case "severity":
369+
// TODO Aggregate exceptions.
362370
this.severities = ParseSettingValueStringOrStrings(setting.Value, settingName);
363371
break;
364372

365373
case "includerules":
374+
// TODO Aggregate exceptions.
366375
this.includeRules = ParseSettingValueStringOrStrings(setting.Value, settingName);
367376
break;
368377

369378
case "excluderules":
379+
// TODO Aggregate exceptions.
370380
this.excludeRules = ParseSettingValueStringOrStrings(setting.Value, settingName);
371381
break;
372382

373383
case "customrulepath":
384+
// TODO Aggregate exceptions.
374385
this.customRulePath = ParseSettingValueStringOrStrings(setting.Value, settingName);
375386
break;
376387

377388
case "includedefaultrules":
389+
// TODO Aggregate exceptions.
378390
this.includeDefaultRules = ParseSettingValueBoolean(setting.Value, settingName);
379391
break;
380392

381393
case "recursecustomrulepath":
394+
// TODO Aggregate exceptions.
382395
this.recurseCustomRulePath = ParseSettingValueBoolean(setting.Value, settingName);
383396
break;
384397

385398
case "rules":
386399
if (!(setting.Value is System.Collections.IDictionary))
387400
{
388-
throw new InvalidDataException(string.Format(
401+
exceptions.Add(new InvalidDataException(string.Format(
389402
Strings.SettingRulesValueIsNotDictionaryType,
390-
setting.Value));
403+
setting.Value)));
404+
continue;
391405
}
392406
Hashtable rules = setting.Value as Hashtable;
393407

@@ -397,38 +411,44 @@ private void ParseSettingsHashtable(Hashtable settings)
397411
{
398412
if (rule.Key is null)
399413
{
400-
throw new InvalidDataException(Strings.SettingRuleKeyIsNull);
414+
exceptions.Add(new InvalidDataException(
415+
Strings.SettingRuleKeyIsNull));
416+
continue;
401417
}
402418

403419
if (!(rule.Key is string))
404420
{
405-
throw new InvalidDataException(string.Format(
421+
exceptions.Add(new InvalidDataException(string.Format(
406422
Strings.SettingRuleKeyIsNotStringType,
407-
rule.Key));
423+
rule.Key)));
424+
continue;
408425
}
409426
string ruleName = rule.Key as string;
410427

411428
if (!uniqueRuleKeys.Add(ruleName))
412429
{
413430
// rule.Key should be used instead of ruleName because the former preserves information about the source casing.
414-
throw new InvalidDataException(string.Format(
431+
exceptions.Add(new InvalidDataException(string.Format(
415432
Strings.SettingRuleKeyIsNotUniqueIgnoringCase,
416-
rule.Key));
433+
rule.Key)));
434+
continue;
417435
}
418436

419437
if (rule.Value is null)
420438
{
421-
throw new InvalidDataException(string.Format(
439+
exceptions.Add(new InvalidDataException(string.Format(
422440
Strings.SettingRuleValueIsNull,
423-
ruleName));
441+
ruleName)));
442+
continue;
424443
}
425444

426445
if (!(rule.Value is System.Collections.IDictionary))
427446
{
428-
throw new InvalidDataException(string.Format(
447+
exceptions.Add(new InvalidDataException(string.Format(
429448
Strings.SettingRuleValueIsNotDictionaryType,
430449
ruleName,
431-
rule.Value));
450+
rule.Value)));
451+
continue;
432452
}
433453
Hashtable arguments = rule.Value as Hashtable;
434454

@@ -438,35 +458,39 @@ private void ParseSettingsHashtable(Hashtable settings)
438458
{
439459
if (argument.Key is null)
440460
{
441-
throw new InvalidDataException(string.Format(
461+
exceptions.Add(new InvalidDataException(string.Format(
442462
Strings.SettingRuleArgumentKeyIsNull,
443-
ruleName));
463+
ruleName)));
464+
continue;
444465
}
445466

446467
if (!(argument.Key is string))
447468
{
448-
throw new InvalidDataException(string.Format(
469+
exceptions.Add(new InvalidDataException(string.Format(
449470
Strings.SettingRuleArgumentKeyIsNotStringType,
450471
ruleName,
451-
argument.Key));
472+
argument.Key)));
473+
continue;
452474
}
453475
string argumentName = argument.Key as string;
454476

455477
if (!uniqueArgumentKeys.Add(argumentName))
456478
{
457479
// argument.Key should be used instead of argumentName because the former preserves information about the source casing.
458-
throw new InvalidDataException(string.Format(
480+
exceptions.Add(new InvalidDataException(string.Format(
459481
Strings.SettingRuleArgumentKeyIsNotUniqueIgnoringCase,
460482
ruleName,
461-
argument.Key));
483+
argument.Key)));
484+
continue;
462485
}
463486

464487
if (argument.Value is null)
465488
{
466-
throw new InvalidDataException(string.Format(
489+
exceptions.Add(new InvalidDataException(string.Format(
467490
Strings.SettingRuleArgumentValueIsNull,
468491
ruleName,
469-
argumentName));
492+
argumentName)));
493+
continue;
470494
}
471495

472496
parsedArguments[argumentName] = argument.Value;
@@ -479,11 +503,17 @@ private void ParseSettingsHashtable(Hashtable settings)
479503
break;
480504

481505
default:
482-
throw new InvalidDataException(string.Format(
506+
exceptions.Add(new InvalidDataException(string.Format(
483507
Strings.WrongKeyHashTable,
484-
settingName));
508+
settingName)));
509+
continue;
485510
}
486511
}
512+
513+
if (exceptions.Count > 0)
514+
{
515+
throw new AggregateException(exceptions);
516+
}
487517
}
488518

489519
private void ParseSettingsFile(string settingsFilePath)

0 commit comments

Comments
 (0)