Skip to content

Commit

Permalink
Add exception handling to EvaluateAsync method
Browse files Browse the repository at this point in the history
The EvaluateAsync method in XmlTemplateReader class has been updated to handle exceptions. A new exception called TransformationEvaluationFailedException has been introduced and will be thrown when the evaluation of a transformation expression fails during the transformation of an XML document. Adjustments were also made to the TransformationFunctionMissingClosingBracketException class.
  • Loading branch information
X39 committed Apr 20, 2024
1 parent d341f85 commit 7d95ab2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .idea/.idea.X39.Solutions.PdfTemplate/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace X39.Solutions.PdfTemplate.Xml.Exceptions;

/// <summary>
/// Thrown when the evaluation of a transformation expression fails during the transformation of an XML document.
/// </summary>
public sealed class TransformationEvaluationFailedException : XmlTemplateTransformationException
{
/// <summary>
/// Represents an expression used in XML template transformation.
/// </summary>
public string Expression { get; }

internal TransformationEvaluationFailedException(string expression, XmlNode node, Exception exception)
: base(
$"Failed to evaluate expression '{expression}' at L{node.Line}:C{node.Column} with exception: {exception.Message}",
exception,
node
)
{
Expression = expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ public sealed class TransformationFunctionMissingClosingBracketException : XmlTe
/// The function text that failed to parse.
/// </summary>
public string FunctionText { get; }

/// <summary>
/// The number of brackets that are missing.
/// </summary>
public int BracketsMissing { get; }
public int BracketsMissing { get; }

internal TransformationFunctionMissingClosingBracketException(string text, XmlNode node, int bracketsMissing)
: base ($"Failed to parse function expression '{text}' at L{node.Line}:C{node.Column}, missing closing bracket (bracket count: {bracketsMissing}).",
node)
: base(
$"Failed to parse function expression '{text}' at L{node.Line}:C{node.Column}, missing closing bracket (bracket count: {bracketsMissing}).",
node
)
{
FunctionText = text;
BracketsMissing = bracketsMissing;
Expand Down
18 changes: 15 additions & 3 deletions source/X39.Solutions.PdfTemplate/Xml/XmlTemplateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,21 @@ private async Task<int> TransformNodeAsync(
if (!value.StartsWith('@'))
continue;
// Evaluate potential expressions
var result = await _templateData.EvaluateAsync(_cultureInfo, value[1..], cancellationToken)
.ConfigureAwait(false);
node.SetAttribute(key, result?.ToString() ?? string.Empty);
try
{
var result = await _templateData.EvaluateAsync(_cultureInfo, value[1..], cancellationToken)
.ConfigureAwait(false);

node.SetAttribute(key, result?.ToString() ?? string.Empty);
}
catch (Exception ex)
{
// Enrich the exception with additional information
throw new TransformationEvaluationFailedException(
value,
node,
ex);
}
}
if (node is {IsTextNode: true, Text: { } text} && (text.Contains('@') || text.Contains('{')))
{
Expand Down

0 comments on commit 7d95ab2

Please sign in to comment.