Skip to content

Commit

Permalink
Add EnableDisplayInnerException for ScriptRuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
Xigi committed Feb 12, 2021
1 parent db4e371 commit a0c23bb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Scriban.Tests/Scriban.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
53 changes: 53 additions & 0 deletions src/Scriban.Tests/ScriptRuntimeExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Scriban.Parsing;
using Scriban.Runtime;
using Scriban.Syntax;
using NSubstitute;

namespace Scriban.Tests
{
public class ScriptRuntimeExceptionTests
{
[Test]
public void TestInnerExceptionExists()
{
ScriptRuntimeException.EnableDisplayInnerException = true;
SourceSpan testSourcespanObject = new SourceSpan("fileName", new TextPosition(0, 0, 0), new TextPosition(0, 0, 0));
Exception exception = Substitute.For<Exception>();
exception.StackTrace.Returns("TestStacTrace");
exception.Message.Returns("Test RunTime message");

ScriptRuntimeException testScriptruntimeObject = new ScriptRuntimeException(testSourcespanObject, "Any string", exception);

Assert.True(testScriptruntimeObject.ToString().Contains("TestStacTrace"));
Assert.True(testScriptruntimeObject.ToString().Contains("Test RunTime message"));
}

[Test]
public void TestInnerExceptiondosentExists()
{
ScriptRuntimeException.EnableDisplayInnerException = true;
SourceSpan testSourcespanObject = new SourceSpan("fileName", new TextPosition(0, 0, 0), new TextPosition(0, 0, 0));

ScriptRuntimeException testScriptruntimeObject = new ScriptRuntimeException(testSourcespanObject, "Any string");

Assert.AreEqual(testScriptruntimeObject.ToString(), testScriptruntimeObject.Message);
}

[Test]
public void TestInnerExceptionDisabled()
{
ScriptRuntimeException.EnableDisplayInnerException = false;
SourceSpan testSourcespanObject = new SourceSpan("fileName", new TextPosition(0, 0, 0), new TextPosition(0, 0, 0));
Exception exception = Substitute.For<Exception>();
exception.StackTrace.Returns("TestStacTrace");
exception.Message.Returns("Test RunTime message");

ScriptRuntimeException testScriptruntimeObject = new ScriptRuntimeException(testSourcespanObject, "Any string", exception);

Assert.AreEqual(testScriptruntimeObject.ToString(), testScriptruntimeObject.Message);
}
}
}
13 changes: 11 additions & 2 deletions src/Scriban/Syntax/ScriptRuntimeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public override string Message
return new LogMessage(ParserMessageType.Error, Span, base.Message).ToString();
}
}
public static bool EnableDisplayInnerException
{
get;
set;
}

/// <summary>
/// Provides the exception message without the source span prefix.
Expand All @@ -51,11 +56,15 @@ public string OriginalMessage
}

public override string ToString()
{
{
if (ScriptRuntimeException.EnableDisplayInnerException && InnerException != null)
{
return base.ToString();
}

return Message;
}
}

#if SCRIBAN_PUBLIC
public
#else
Expand Down

0 comments on commit a0c23bb

Please sign in to comment.