Search Unity

Feature Request Unity Logger: Unroll inner exceptions correctly

Discussion in 'Editor & General Support' started by RobinBGbanga, May 31, 2022.

  1. RobinBGbanga

    RobinBGbanga

    Joined:
    Oct 20, 2014
    Posts:
    16
    Dear Unity team

    It often causes a lot of confusion that Unity's logger turns nested exceptions inside-out. To illustrate, you can run this method once in a .Net console application, and once inside Unity, e.g. from a MonoBehaviour:

    Code (CSharp):
    1. using System;
    2.  
    3. public static class InnerExceptionTest
    4. {
    5.     public static void Run()
    6.     {
    7.         try
    8.         {
    9.             try
    10.             {
    11.                 throw new Exception("Arcane details about a third-party library.");
    12.             }
    13.             catch (Exception e)
    14.             {
    15.                 throw new Exception("Exception inside a worker class. Details in the inner exception.", e);
    16.             }
    17.         }
    18.         catch (Exception e)
    19.         {
    20.             throw new Exception("A human-friendly high-level message. Details in the inner exception.", e);
    21.         }
    22.     }
    23. }

    When running this in a .Net console application, the stacktrace will unroll the inner exceptions in the order you would expect, starting at the highest level and drilling deeper:

    Code (CSharp):
    1. System.Exception: A human-friendly high-level message. Details in the inner exception.
    2. ---> System.Exception: Exception inside a worker class. Details in the inner exception.
    3. ---> System.Exception: Arcane details about a third-party library.
    4.    at InnerExceptionTest.Run() in C:\TestCase\InnerExceptionTest.cs:line 11
    5.    --- End of inner exception stack trace ---
    6.    at InnerExceptionTest.Run() in C:\TestCase\InnerExceptionTest.cs:line 15
    7.    --- End of inner exception stack trace ---
    8.    at InnerExceptionTest.Run() in C:\TestCase\InnerExceptionTest.cs:line 20
    9.    at TestCase.Program.Main(String[] args) in C:\TestCase\Program.cs:line 10

    When running the same code in Unity, the stacktrace will be turned inside-out:

    Code (CSharp):
    1. Exception: Arcane details about a third-party library.
    2. InnerExceptionTest.Run () (at Assets/InnerExceptionTest.cs:11)
    3. Rethrow as Exception: Exception inside a worker class. Details in the inner exception.
    4. InnerExceptionTest.Run () (at Assets/InnerExceptionTest.cs:15)
    5. Rethrow as Exception: A human-friendly high-level message. Details in the inner exception.
    6. InnerExceptionTest.Run () (at Assets/InnerExceptionTest.cs:20)
    7. TestCase.Start () (at Assets/TestCase.cs:7)

    We tried working around this by using a custom Logger, but found that this approach has other downsides that we can't, in turn, work around.

    Would it be feasible to unroll inner exceptions the way described? This would be a great productivity boost.
    Thanks!
     
  2. RobinBGbanga

    RobinBGbanga

    Joined:
    Oct 20, 2014
    Posts:
    16
    I wanted to give this a nudge, see if there's any developments on this front.

    This issue still causes us a lot of friction. We've somewhat worked around it by displaying nested exceptions in a sane, outside-in order on development builds, but we don't know how to work around this issue when exceptions get logged by Firebase Crashlytics, for example.

    This means we often get exception logs that got turned inside-out and then truncated by Crashlytics, so the useful outermost exception is completely lost to us.

    Any inputs, thoughts or pointers are appreciated.