Skip to content

Commit

Permalink
standalone method to get exception stack trace and message
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Oct 22, 2024
1 parent a05adc9 commit 3987e07
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/Neo/SmartContract/ApplicationEngine.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neo.SmartContract
{
public partial class ApplicationEngine : ExecutionEngine
{
public string? GetEngineErrorInfo()
public string? GetEngineStackInfoOnFault(bool exceptionStackTrace = true, bool exceptionMessage = true)

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 22 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (State != VMState.FAULT || FaultException == null)
return null;
Expand All @@ -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)

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/Neo/SmartContract/ApplicationEngine.Helper.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
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();
}
}
}
4 changes: 2 additions & 2 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 3987e07

Please sign in to comment.