private void FormatError(FormatItem errorItem, Exception innerException, int startIndex, FormattingInfo formattingInfo) { OnFormattingFailure?.Invoke(this, new FormattingErrorEventArgs(errorItem.RawText, startIndex, Settings.FormatErrorAction != ErrorAction.ThrowError)); switch (Settings.FormatErrorAction) { case ErrorAction.Ignore: return; case ErrorAction.ThrowError: throw innerException as FormattingException ?? new FormattingException(errorItem, innerException, startIndex); case ErrorAction.OutputErrorInResult: formattingInfo.FormatDetails.FormattingException = innerException as FormattingException ?? new FormattingException(errorItem, innerException, startIndex); formattingInfo.Write(innerException.Message); formattingInfo.FormatDetails.FormattingException = null; break; case ErrorAction.MaintainTokens: formattingInfo.Write(formattingInfo.Placeholder?.RawText ?? "'null'"); break; } }
/// <summary> /// Format the <see cref="FormattingInfo" /> argument. /// </summary> /// <param name="formattingInfo"></param> public void Format(FormattingInfo formattingInfo) { // Before we start, make sure we have at least one source extension and one formatter extension: CheckForExtensions(); if (formattingInfo.Format is null) { return; } foreach (var item in formattingInfo.Format.Items) { if (item is LiteralText literalItem) { formattingInfo.Write(literalItem.ToString()); continue; } // Otherwise, the item must be a placeholder. var placeholder = (Placeholder)item; var childFormattingInfo = formattingInfo.CreateChild(placeholder); try { EvaluateSelectors(childFormattingInfo); } catch (Exception ex) { // An error occurred while evaluation selectors var errorIndex = placeholder.Format?.startIndex ?? placeholder.Selectors.Last().endIndex; FormatError(item, ex, errorIndex, childFormattingInfo); continue; } try { EvaluateFormatters(childFormattingInfo); } catch (Exception ex) { // An error occurred while evaluating formatters var errorIndex = placeholder.Format?.startIndex ?? placeholder.Selectors.Last().endIndex; FormatError(item, ex, errorIndex, childFormattingInfo); } } }