Skip to content

Issues with serializing exceptions with System.Text.Json #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jakejscott opened this issue Aug 11, 2022 · 2 comments
Closed

Issues with serializing exceptions with System.Text.Json #151

jakejscott opened this issue Aug 11, 2022 · 2 comments
Assignees
Labels
area/logging Core logging utility bug Unexpected, reproducible and unintended software behaviour

Comments

@jakejscott
Copy link

jakejscott commented Aug 11, 2022

Expected Behaviour

Some Exceptions have types on them that are not serializable by System.Text.Json for example 'System.Type' and 'System.IntPtr'

See related issue here:
dotnet/runtime#43026

Not sure of the best way to handle this. I think libraries like Serilog implement their own custom JSON serialization.

Current Behaviour

When I log certain exceptions System.NotSupportedException is thrown

Code snippet

[Fact]
public void ReproduceIssue()
{
    var myEx = new MyException
    {
        Handle = (IntPtr)1
    };

    var value = JsonSerializer.Serialize(myEx);

    // System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.Handle.
}

public class MyException : Exception
{
    public IntPtr Handle { get; set; }
}

Possible Solution

Possibly implement a JsonConverter which can serialize exceptions (Message, StackTrace)

Steps to Reproduce

[Fact]
public void ReproduceIssue()
{
    var myEx = new MyException
    {
        Handle = (IntPtr)1
    };

    var value = JsonSerializer.Serialize(myEx);

    // System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.Handle.
}

public class MyException : Exception
{
    public IntPtr Handle { get; set; }
}

AWS Lambda Powertools for .NET version

latest

AWS Lambda function runtime

dotnet6

Debugging logs

No response

@jakejscott jakejscott added bug Unexpected, reproducible and unintended software behaviour triage Pending triage from maintainers labels Aug 11, 2022
@amirkaws amirkaws added the area/logging Core logging utility label Aug 16, 2022
@amirkaws
Copy link
Contributor

Thank you @jakejscott for opening the issue. As you mentioned, System.Text.Json does not support Type & IntPtr serialization but the Logger.LogError method does not serialize the exception. Instead it adds the error message to the log object. Technically you should not get error in that case. I’ll appreciate if you please provide a bit more context on how you are using the Logger to raise the exception.

[Fact]
public void ReproduceIssue()
{
    var myEx = new MyException
    {
        Handle = (IntPtr)1
    };

    Logger.LogError(myEx);
}

public class MyException : Exception
{
    public IntPtr Handle { get; set; }
}

it’s also understandable that there might be a need to log more details on the exception than just the message. One of the way would be implementing a custom JSON serialization to handle exceptions per suggested. I’ll discuss internally to find out the best way forward and update the ticket accordingly.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Sep 12, 2022
@sliedig sliedig removed triage Pending triage from maintainers researching labels Sep 12, 2022
@amirkaws
Copy link
Contributor

Released v0.0.2-preview.

@hjgraca hjgraca removed the pending-release Fix or implementation already in dev waiting to be released label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/logging Core logging utility bug Unexpected, reproducible and unintended software behaviour
Projects
None yet
Development

No branches or pull requests

4 participants