Skip to content

Commit

Permalink
Merge pull request #6 from NikTheNak/main
Browse files Browse the repository at this point in the history
event error handling fix
  • Loading branch information
goenning authored Dec 28, 2023
2 parents e2b3917 + adf683f commit 547df10
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## 0.2.4

- Fixed memory leak with event errors
- Fixed event send error handling
- Reduced logging on errors

## 0.2.3

- use new session id format
- Use new session id format

## 0.2.2

Expand Down
2 changes: 1 addition & 1 deletion PACKAGE.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.aptabase",
"displayName": "Aptabase",
"version": "0.2.3",
"version": "0.2.4",
"unity": "2018.4",
"description": "Analytics for Apps. Privacy-First. Simple. Real-Time.",
"keywords": ["analytics"],
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Aptabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static int GetFlushInterval()

return _env.isDebug ? 2000 : 60000;
}

private static string NewSessionId()
{
var epochInSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Expand Down
14 changes: 7 additions & 7 deletions Runtime/Dispatcher/Dispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AptabaseSDK.TinyJson;
Expand Down Expand Up @@ -60,13 +59,13 @@ public async void Flush()
eventsToSend.Add(_events.Dequeue());

try
{
await SendEvents(eventsToSend);
{
var result = await SendEvents(eventsToSend);
if (!result) failedEvents.AddRange(eventsToSend);
}
catch (Exception e)
catch
{
failedEvents.AddRange(eventsToSend);
Debug.LogError(e);
}

} while (_events.Count > 0);
Expand All @@ -77,10 +76,11 @@ public async void Flush()
_flushInProgress = false;
}

private static async Task SendEvents(List<Event> events)
private static async Task<bool> SendEvents(List<Event> events)
{
var webRequest = _webRequestHelper.CreateWebRequest(_apiURL, _appKey, _environment, events.ToJson());
await _webRequestHelper.SendWebRequestAsync(webRequest);
var result = await _webRequestHelper.SendWebRequestAsync(webRequest);
return result;
}
}
}
13 changes: 6 additions & 7 deletions Runtime/Dispatcher/WebGLDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AptabaseSDK.TinyJson;
using UnityEngine;

namespace AptabaseSDK
{
Expand Down Expand Up @@ -56,12 +54,12 @@ public async void Flush()
var eventToSend = _events.Dequeue();
try
{
await SendEvent(eventToSend);
var result = await SendEvent(eventToSend);
if (!result) failedEvents.Add(eventToSend);
}
catch (Exception e)
catch
{
failedEvents.Add(eventToSend);
Debug.LogError(e);
}

} while (_events.Count > 0);
Expand All @@ -72,10 +70,11 @@ public async void Flush()
_flushInProgress = false;
}

private static async Task SendEvent(Event eventData)
private static async Task<bool> SendEvent(Event eventData)
{
var webRequest = _webRequestHelper.CreateWebRequest(_apiURL, _appKey, _environment, eventData.ToJson());
await _webRequestHelper.SendWebRequestAsync(webRequest);
var result = await _webRequestHelper.SendWebRequestAsync(webRequest);
return result;
}
}
}
14 changes: 11 additions & 3 deletions Runtime/WebRequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ public UnityWebRequest CreateWebRequest(string url, string appKey, EnvironmentIn
return webRequest;
}

public async Task SendWebRequestAsync(UnityWebRequest request)
public async Task<bool> SendWebRequestAsync(UnityWebRequest request)
{
var requestOp = request.SendWebRequest();
while (!requestOp.isDone)
await Task.Yield();

if (requestOp.webRequest.result != UnityWebRequest.Result.Success)
var success = requestOp.webRequest.result == UnityWebRequest.Result.Success;
if (success)
{
Debug.LogError($"Failed to perform web request due to {requestOp.webRequest.responseCode} and response body {requestOp.webRequest.error}");
request.Dispose();
}
else
{
Debug.LogWarning($"Failed to perform web request due to {requestOp.webRequest.responseCode} and response body {requestOp.webRequest.error}");
request.Dispose();
}

return success;
}
}
}

0 comments on commit 547df10

Please sign in to comment.