getsentry / sentry-dotnet

Sentry SDK for .NET
https://docs.sentry.io/platforms/dotnet
MIT License
566 stars 203 forks source link

Add the Refit Exception Content to Sentry #3314

Open TimVerheul opened 3 weeks ago

TimVerheul commented 3 weeks ago

Problem Statement

Refit is a populair library for making requests. If an exception occurs, only the Exception Message will be sent to Sentry. The Message is a very broad description (a 400 was received) without containing valuable information. This information is stored in the Content part of the Exception. Unfortunately, Sentry does not send this. So when a Refit Exception occurs, you won't know what exactly is causing this.

image

Solution Brainstorm

As a temporary solution I add my own EventExceptionProcessor but it would be nice to have something like .CatchRefit() to handle this.

jamescrosswell commented 3 weeks ago

Hey @TimVerheul , thanks for reaching out.

There are so many libraries out there and only so many hours in a day, so I'm not sure if we can add something specifically for Refit

However, I'm wondering if we could potentially create something a bit more generic. Maybe some way to configure the SentryHttpFailedRequestHandler to check for (and extract) JSON content from response bodies for certain request targets and/or HttpStatus codes.

@bitsandfoxes thoughts?

bitsandfoxes commented 3 weeks ago

This originates from a discussion on Discord.

We have a way that enables users to tailor the exception handling to their need if we lack some sort of integration with any specific framework. The way @TimVerheul managed to unblock himself looks like this

public class RefitExceptionProcessor : ISentryEventExceptionProcessor
{
    public void Process(Exception exception, SentryEvent sentryEvent)
    {
        if (exception is Refit.ApiException apiException)
        {
            sentryEvent.Message = apiException.Content;
            sentryEvent.Contexts["Refit"] = new
            {
                RefitMessage = apiException.Message,
                RefitContent = apiException.Content,
            }
        }
    }
}

I'm not sure about the benefit of crawling through whatever a framework/library has done to their own exception types and "guess" what bits might be relevant.

bitsandfoxes commented 3 weeks ago

I see I misread your comment. You wanted to check the content on the response bodies. Won't we run into all kinds of PII and therefor opt-in/out related issues when trying to parse and capture the bodies?

TimVerheul commented 3 weeks ago

I think having the code written here will help others who might got the same issue as I had. I agree, it's not possible to handle every custom exception for every library.