-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentryGraphQLHttpFailedRequestHandler.cs
More file actions
106 lines (96 loc) · 4.26 KB
/
SentryGraphQLHttpFailedRequestHandler.cs
File metadata and controls
106 lines (96 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Runtime.ExceptionServices;
using Sentry.Internal;
using Sentry.Internal.Extensions;
using Sentry.Protocol;
namespace Sentry;
internal class SentryGraphQLHttpFailedRequestHandler : SentryFailedRequestHandler
{
private readonly IHub _hub;
private readonly SentryOptions _options;
internal const string MechanismType = "GraphqlInstrumentation";
private readonly SentryHttpFailedRequestHandler _httpFailedRequestHandler;
internal SentryGraphQLHttpFailedRequestHandler(IHub hub, SentryOptions options)
: base(hub, options)
{
_hub = hub;
_options = options;
_httpFailedRequestHandler = new SentryHttpFailedRequestHandler(hub, options);
}
protected internal override void DoEnsureSuccessfulResponse([NotNull] HttpRequestMessage request, [NotNull] HttpResponseMessage response)
{
JsonElement? json = null;
try
{
json = GraphQLContentExtractor.ExtractResponseContentAsync(response, _options).GetAwaiter().GetResult();
if (json is { } jsonElement)
{
if (jsonElement.TryGetProperty("errors", out var errorsElement))
{
// We just show the first error... maybe there's a better way to do this when multiple errors exist.
// We should check what the Java code is doing.
var errorMessage = errorsElement[0].GetProperty("message").GetString() ?? "GraphQL Error";
var exception = new GraphQLHttpRequestException(errorMessage);
#if NET5_0_OR_GREATER
// Add a full stack trace into the exception to improve Issue grouping,
// see https://github.com/getsentry/sentry-dotnet/issues/3582
ExceptionDispatchInfo.Throw(ExceptionDispatchInfo.SetCurrentStackTrace(exception));
#else
// Where SetCurrentStackTrace is not available, just throw the Exception
throw exception;
#endif
}
}
// No GraphQL errors, but we still might have an HTTP error status
_httpFailedRequestHandler.DoEnsureSuccessfulResponse(request, response);
}
catch (Exception exception)
{
exception.SetSentryMechanism(MechanismType, "GraphQL Failed Request Handler", false);
var @event = new SentryEvent(exception);
var hint = new SentryHint(HintTypes.HttpResponseMessage, response);
var sentryRequest = new SentryRequest
{
QueryString = request.RequestUri?.Query,
Method = request.Method.Method.ToUpperInvariant(),
ApiTarget = "graphql"
};
var responseContext = new Response
{
StatusCode = (short)response.StatusCode,
#if NET5_0_OR_GREATER
// Starting with .NET 5, the content and headers are guaranteed to not be null.
BodySize = response.Content?.Headers.ContentLength,
#else
BodySize = response.Content?.Headers?.ContentLength,
#endif
};
var requestContent = request.GetFused<GraphQLRequestContent>();
if (!_options.SendDefaultPii)
{
sentryRequest.Url = request.RequestUri?.HttpRequestUrl();
}
else
{
sentryRequest.Cookies = request.Headers.GetCookies();
sentryRequest.Data = requestContent?.RequestContent;
sentryRequest.Url = request.RequestUri?.AbsoluteUri;
sentryRequest.AddHeaders(request.Headers);
responseContext.Cookies = response.Headers.GetCookies();
responseContext.Data = json;
responseContext.AddHeaders(response.Headers);
}
@event.Request = sentryRequest;
@event.Contexts[Response.Type] = responseContext;
if (requestContent is not null)
{
@event.Fingerprint = new[]
{
requestContent.OperationNameOrFallback(),
requestContent.OperationTypeOrFallback(),
((int)response.StatusCode).ToString()
};
}
Hub.CaptureEvent(@event, hint: hint);
}
}
}