diff --git a/src/Neo/SmartContract/ApplicationEngine.Helper.cs b/src/Neo/SmartContract/ApplicationEngine.Helper.cs index 687e8d84f3..8da61efb03 100644 --- a/src/Neo/SmartContract/ApplicationEngine.Helper.cs +++ b/src/Neo/SmartContract/ApplicationEngine.Helper.cs @@ -12,7 +12,6 @@ using Neo.SmartContract.Native; using Neo.VM; using System; -using System.Collections.Generic; using System.Linq; using System.Text; @@ -20,7 +19,7 @@ namespace Neo.SmartContract { public partial class ApplicationEngine : ExecutionEngine { - public string? GetEngineErrorInfo() + public string? GetEngineStackInfoOnFault(bool exceptionStackTrace = true, bool exceptionMessage = true) { if (State != VMState.FAULT || FaultException == null) return null; @@ -36,11 +35,22 @@ public partial class ApplicationEngine : ExecutionEngine string contextContractName = NativeContract.ContractManagement.GetContract(SnapshotCache, contextScriptHash)?.Manifest.Name; traceback.AppendLine($"\tInstructionPointer={context.InstructionPointer}, OpCode {context.CurrentInstruction?.OpCode}, Script Length={context.Script.Length} {contextScriptHash}[{contextContractName}]"); } - Exception baseException = FaultException.GetBaseException(); - traceback.AppendLine(baseException.StackTrace); - traceback.AppendLine(baseException.Message); + traceback.Append(GetEngineExceptionInfo(exceptionStackTrace: exceptionStackTrace, exceptionMessage: exceptionMessage)); return traceback.ToString(); } + + public string? GetEngineExceptionInfo(bool exceptionStackTrace = true, bool exceptionMessage = true) + { + if (State != VMState.FAULT || FaultException == null) + return null; + StringBuilder traceback = new(); + Exception baseException = FaultException.GetBaseException(); + if (exceptionStackTrace) + traceback.AppendLine(baseException.StackTrace); + if (exceptionMessage) + traceback.AppendLine(baseException.Message); + return traceback.ToString(); + } } } diff --git a/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs b/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs index 4166a7a914..7b379d5eb1 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs @@ -167,10 +167,10 @@ public void TestSystem_Contract_Call_Permissions() }; var currentScriptHash = engine.EntryScriptHash; - Assert.IsNull(engine.GetEngineErrorInfo()); + Assert.IsNull(engine.GetEngineStackInfoOnFault()); Assert.AreEqual(VMState.FAULT, engine.Execute()); Assert.IsTrue(engine.FaultException.ToString().Contains($"Cannot Call Method disallowed Of Contract {scriptHash.ToString()}")); - string traceback = engine.GetEngineErrorInfo(); + string traceback = engine.GetEngineStackInfoOnFault(); Assert.IsTrue(traceback.Contains($"Cannot Call Method disallowed Of Contract {scriptHash.ToString()}")); Assert.IsTrue(traceback.Contains("CurrentScriptHash")); Assert.IsTrue(traceback.Contains("EntryScriptHash"));