Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort formatting exception #375

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/SmartFormat.Tests/Core/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Formatting;
using SmartFormat.Core.Output;
using SmartFormat.Core.Settings;
Expand Down Expand Up @@ -344,4 +345,46 @@ public void Parallel_Smart_Format()
// Restore to saved value
ThreadSafeMode.SwitchTo(savedMode);
}
}

public class AbortFormatSource : ISource
{
public string? Message { get; set; }

public bool TryEvaluateSelector(ISelectorInfo selectorInfo)
{
if (selectorInfo.SelectorText == "abort")
{
if (Message == null)
throw new AbortFormattingException();
throw new AbortFormattingException(Message);
}
return false;
}
}

[TestCase("Some text {abort}", "Some text ")]
[TestCase("Some text {1.abort}", "Some text ")]
[TestCase("Some text {0.abort.some.selector}", "Some text ")]
[TestCase("Some text {abort.some.selector:plural:Apple|{} Apples}", "Some text ")]
public void AbortFormattingException_PreventsFurtherProcessing(string text, string expected)
{
var formatter = GetSimpleFormatter();
formatter.AddExtensions(new AbortFormatSource());

var actual = formatter.Format(text, 1, 2);
Assert.That(actual, Is.EqualTo(expected));
}

[TestCase("Some text {abort}", "", "Some text ")]
[TestCase("Some text {1.abort}", "Message", "Some text Message")]
[TestCase("Some text {0.abort.some.selector}", "A Message", "Some text A Message")]
[TestCase("Some text {abort.some.selector:plural:Apple|{} Apples}", "<Error>", "Some text <Error>")]
public void AbortFormattingException_PreventsFurtherProcessingAndIncludesMessage(string text, string message, string expected)
{
var formatter = GetSimpleFormatter();
formatter.AddExtensions(new AbortFormatSource { Message = message });

var actual = formatter.Format(text, 1, 2);
Assert.That(actual, Is.EqualTo(expected));
}
}
33 changes: 33 additions & 0 deletions src/SmartFormat/Core/Formatting/AbortFormattingException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright SmartFormat Project maintainers and contributors.
// Licensed under the MIT license.

using System;
using SmartFormat.Core.Extensions;

namespace SmartFormat.Core.Formatting;

/// <summary>
/// An exception designed to halt further processing for a specific format item.
/// This exception can be raised from an <see cref="ISource"/> to prevent additional processing,
/// particularly in scenarios where the data's state is not yet prepared or deemed valid.
/// </summary>
public class AbortFormattingException : Exception
{
/// <summary>
/// Creates a new instance of <see cref="AbortFormattingException"/>.
/// </summary>
/// <param name="text">Optional text to be used in the output string.</param>
public AbortFormattingException(string text)
{
Text = text;
}

///<inheritdoc/>
public AbortFormattingException() { }

/// <summary>
/// A message to be used in the output string.
/// </summary>
public string? Text { get; }
}
6 changes: 6 additions & 0 deletions src/SmartFormat/SmartFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ public void Format(FormattingInfo formattingInfo)
{
EvaluateSelectors(childFormattingInfo);
}
catch (AbortFormattingException ex)
{
if (ex.Text != null)
formattingInfo.Write(ex.Text);
continue;
}
catch (Exception ex)
{
// An error occurred while evaluation selectors
Expand Down