/// <summary> /// Checks and <see cref="IParsingResultsDictionary"/> for errors /// and acts according to settings <see cref="IUsingParsingResults.ParsingErrorBehavior"/> /// and if <see cref="IUsingParsingResults.ErrorFunction"/> is set, /// by either throwing an exception if necessary and expected /// or invoking <see cref="IUsingParsingResults.ErrorFunction"/> respectively. /// </summary> /// <param name="resultsParserUtilizer">The results parser utilizer.</param> /// <param name="resultsDictionary">The results dictionary.</param> /// <returns>The <see cref="IParsingResultsDictionary"/> provided as <paramref name="resultsDictionary"/></returns> public static IParsingResultsDictionary HandleResultsErrors(this IUsingParsingResults resultsParserUtilizer, IParsingResultsDictionary resultsDictionary) { if (!resultsDictionary.HasErrors) { return(resultsDictionary); } if (resultsParserUtilizer.ErrorFunction != null) { return(handleErrorsWithFunction(resultsParserUtilizer.ParsingErrorBehavior, resultsDictionary, resultsParserUtilizer.ErrorFunction)); } switch (resultsParserUtilizer.ParsingErrorBehavior) { case ParsingResultsErrorBehavior.None: return(resultsDictionary); case ParsingResultsErrorBehavior.OnFirstError: throw resultsDictionary.Errors.First(); default: case ParsingResultsErrorBehavior.Aggregate: throw new AggregateException(resultsDictionary.Errors); } }
public static IDictionary <string, string> ToDictionary(this IParsingResultsDictionary parsingResults) => parsingResults.ToDictionary(p => p.Key, p => p.Value);
private static IParsingResultsDictionary handleErrorsWithFunction(ParsingResultsErrorBehavior errorBehavior, IParsingResultsDictionary resultsDictionary, Action <IEnumerable <Exception> > errorFunction) { switch (errorBehavior) { case ParsingResultsErrorBehavior.OnFirstError: errorFunction(resultsDictionary.Errors.First().AsEnumerable().ToList()); break; case ParsingResultsErrorBehavior.Aggregate: errorFunction(resultsDictionary.Errors.ToList()); break; } return(ParsingResultsDictionary.CreateFailed(resultsDictionary.Errors.ToList())); }