Skip to content

Commit 5a15262

Browse files
committed
refactor: futer WebGL implementation cleanup
1 parent 50022ec commit 5a15262

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

src/Sentry.Unity/WebGL/SentryWebGL.cs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal class WebBackgroundWorker : IBackgroundWorker
5454
public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behaviour)
5555
{
5656
_behaviour = behaviour;
57-
_transport = new UnityWebRequestTransport(options, behaviour);
57+
_transport = new UnityWebRequestTransport(options);
5858
}
5959

6060
public bool EnqueueEnvelope(Envelope envelope)
@@ -72,7 +72,7 @@ internal class UnityWebRequestTransport : HttpTransportBase
7272
{
7373
private readonly SentryUnityOptions _options;
7474

75-
public UnityWebRequestTransport(SentryUnityOptions options, SentryMonoBehaviour behaviour)
75+
public UnityWebRequestTransport(SentryUnityOptions options)
7676
: base(options)
7777
{
7878
_options = options;
@@ -85,7 +85,8 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
8585
if (processedEnvelope.Items.Count > 0)
8686
{
8787
// Send envelope to ingress
88-
var www = CreateWebRequest(CreateRequest(processedEnvelope));
88+
var httpRequest = CreateRequest(processedEnvelope);
89+
var www = CreateWebRequest(httpRequest, processedEnvelope);
8990
yield return www.SendWebRequest();
9091

9192
var response = GetResponse(www);
@@ -96,28 +97,40 @@ internal IEnumerator SendEnvelopeAsync(Envelope envelope)
9697
}
9798
}
9899

99-
private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
100+
private UnityWebRequest CreateWebRequest(HttpRequestMessage message, Envelope envelope)
100101
{
101-
var www = new UnityWebRequest();
102-
www.url = message.RequestUri.ToString();
103-
www.method = message.Method.Method.ToUpperInvariant();
102+
// Note: In order to use the synchronous Envelope.Serialize() we ignore the `message.Content`
103+
// which is an `EnvelopeHttpContent` instance and use the actual envelope it wraps.
104+
var stream = new MemoryStream();
105+
try
106+
{
107+
envelope.Serialize(stream, _options.DiagnosticLogger);
108+
stream.Flush();
109+
}
110+
catch (Exception e)
111+
{
112+
_options.DiagnosticLogger?.LogError("Failed to serialize Envelope into the network stream", e);
113+
throw;
114+
}
115+
116+
var www = new UnityWebRequest
117+
{
118+
url = message.RequestUri.ToString(),
119+
method = message.Method.Method.ToUpperInvariant(),
120+
uploadHandler = new UploadHandlerRaw(stream.ToArray()),
121+
downloadHandler = new DownloadHandlerBuffer()
122+
};
104123

105124
foreach (var header in message.Headers)
106125
{
107126
www.SetRequestHeader(header.Key, string.Join(",", header.Value));
108127
}
109128

110-
var stream = new MemoryStream();
111-
_ = message.Content.CopyToAsync(stream).Wait(2000);
112-
stream.Flush();
113-
www.uploadHandler = new UploadHandlerRaw(stream.ToArray());
114-
www.downloadHandler = new DownloadHandlerBuffer();
115129
return www;
116130
}
117131

118132
private HttpResponseMessage? GetResponse(UnityWebRequest www)
119133
{
120-
121134
// if (www.result == UnityWebRequest.Result.ConnectionError) // unity 2021+
122135
if (www.isNetworkError) // Unity 2019
123136
{
@@ -134,7 +147,7 @@ private UnityWebRequest CreateWebRequest(HttpRequestMessage message)
134147
response.Headers.Add(header.Key, header.Value);
135148
}
136149
}
137-
response.Content = new ExposedStringContent(www.downloadHandler.text);
150+
response.Content = new StringContent(www.downloadHandler.text);
138151
return response;
139152
}
140153
}
@@ -147,29 +160,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage _, Can
147160
throw new InvalidOperationException("UnityWebRequestMessageHandler must be unused");
148161
}
149162
}
150-
151-
internal class ExposedStringContent : StringContent
152-
{
153-
internal readonly String Content;
154-
public ExposedStringContent(String data) : base(data) => Content = data;
155-
}
156-
157-
internal static class JsonExtensions
158-
{
159-
public static JsonElement? GetPropertyOrNull(this JsonElement json, string name)
160-
{
161-
if (json.ValueKind != JsonValueKind.Object)
162-
{
163-
return null;
164-
}
165-
166-
if (json.TryGetProperty(name, out var result) &&
167-
result.ValueKind is not JsonValueKind.Undefined and not JsonValueKind.Null)
168-
{
169-
return result;
170-
}
171-
172-
return null;
173-
}
174-
}
175163
}

0 commit comments

Comments
 (0)