Skip to content

Commit 67e91ab

Browse files
authored
unit tests + code refactor (#4)
1 parent 3a0f458 commit 67e91ab

35 files changed

+821
-254
lines changed
File renamed without changes.

src/BacktraceClient.cs

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class BacktraceClient : MonoBehaviour, IBacktraceClient
1616
{
1717
public BacktraceConfiguration Configuration;
1818

19+
public bool Enabled { get; private set; }
20+
1921
/// <summary>
2022
/// Backtrace database instance that allows to manage minidump files
2123
/// </summary>
@@ -29,12 +31,27 @@ public Action<Exception> OnServerError
2931
{
3032
get
3133
{
32-
return BacktraceApi.OnServerError;
34+
return BacktraceApi?.OnServerError;
3335
}
3436

3537
set
3638
{
37-
BacktraceApi.OnServerError = value;
39+
if (ValidClientConfiguration())
40+
{
41+
BacktraceApi.OnServerError = value;
42+
}
43+
}
44+
}
45+
46+
public Func<string, BacktraceData, BacktraceResult> RequestHandler
47+
{
48+
get { return BacktraceApi?.RequestHandler; }
49+
set
50+
{
51+
if (ValidClientConfiguration())
52+
{
53+
BacktraceApi.RequestHandler = value;
54+
}
3855
}
3956
}
4057

@@ -45,19 +62,22 @@ public Action<BacktraceResult> OnServerResponse
4562
{
4663
get
4764
{
48-
return BacktraceApi.OnServerResponse;
65+
return BacktraceApi?.OnServerResponse;
4966
}
5067

5168
set
5269
{
53-
BacktraceApi.OnServerResponse = value;
70+
if (ValidClientConfiguration())
71+
{
72+
BacktraceApi.OnServerResponse = value;
73+
}
5474
}
5575
}
5676

5777
/// <summary>
5878
/// Get or set minidump type
5979
/// </summary>
60-
public MiniDumpType MiniDumpType { get; set; } = MiniDumpType.Normal;
80+
public MiniDumpType MiniDumpType { get; set; } = MiniDumpType.None;
6181

6282
/// <summary>
6383
/// Set event executed when client site report limit reached
@@ -66,7 +86,10 @@ public Action<BacktraceReport> OnClientReportLimitReached
6686
{
6787
set
6888
{
69-
BacktraceApi.SetClientRateLimitEvent(value);
89+
if (ValidClientConfiguration())
90+
{
91+
BacktraceApi.SetClientRateLimitEvent(value);
92+
}
7093
}
7194
}
7295

@@ -102,26 +125,31 @@ internal IBacktraceApi BacktraceApi
102125
Database?.SetApi(_backtraceApi);
103126
}
104127
}
105-
106-
private void Awake()
128+
public void Refresh()
107129
{
108130
Database = GetComponent<BacktraceDatabase>();
109131
if (Configuration == null || !Configuration.IsValid())
110132
{
111133
Debug.LogWarning("Configuration doesn't exists or provided serverurl/token are invalid");
112134
return;
113135
}
136+
Enabled = true;
114137
if (Configuration.HandleUnhandledExceptions)
115138
{
116139
HandleUnhandledExceptions();
117140
}
118141
BacktraceApi = new BacktraceApi(
119-
credentials: new BacktraceCredentials(Configuration.ServerUrl, Configuration.Token),
142+
credentials: new BacktraceCredentials(Configuration.GetValidServerUrl(), Configuration.Token),
120143
reportPerMin: Convert.ToUInt32(Configuration.ReportPerMin));
121144

122145
Database?.SetApi(BacktraceApi);
123146
}
124147

148+
private void Awake()
149+
{
150+
Refresh();
151+
}
152+
125153
/// <summary>
126154
/// Change maximum number of reportrs sending per one minute
127155
/// </summary>
@@ -166,15 +194,7 @@ public void Send(BacktraceReport report, Action<BacktraceResult> sendCallback =
166194
var record = Database?.Add(report, Attributes, MiniDumpType);
167195
//create a JSON payload instance
168196
BacktraceData data = null;
169-
try
170-
{
171-
data = record?.BacktraceData ?? report.ToBacktraceData(Attributes);
172-
}
173-
catch (Exception e)
174-
{
175-
Debug.Log(e);
176-
}
177-
197+
data = record?.BacktraceData ?? report.ToBacktraceData(Attributes);
178198
//valid user custom events
179199
data = BeforeSend?.Invoke(data) ?? data;
180200

@@ -213,6 +233,7 @@ private void HandleException(string condition, string stackTrace, LogType type)
213233
if (type == LogType.Exception || type == LogType.Error)
214234
{
215235
var exception = new BacktraceUnhandledException(condition, stackTrace);
236+
OnUnhandledApplicationException?.Invoke(exception);
216237
var report = new BacktraceReport(exception);
217238
Send(report);
218239
}
@@ -232,5 +253,19 @@ private IEnumerator HandleInnerException(BacktraceReport report, Action<Backtrac
232253
}
233254
Send(innerExceptionReport, callback);
234255
}
256+
257+
/// <summary>
258+
/// Validate if current client configuration is valid
259+
/// </summary>
260+
/// <returns>True if client allows to setup events, otherwise false</returns>
261+
private bool ValidClientConfiguration()
262+
{
263+
var invalidConfiguration = BacktraceApi == null || !Enabled;
264+
if (invalidConfiguration)
265+
{
266+
Debug.Log($"Cannot set method if configuration contain invalid url to Backtrace server or client is disabled");
267+
}
268+
return !invalidConfiguration;
269+
}
235270
}
236271
}

src/BacktraceDatabase.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,34 @@ private string DatabasePath
5656
/// <summary>
5757
/// Determine if BacktraceDatabase is enable and library can store reports
5858
/// </summary>
59-
private bool _enable = false;
59+
public bool Enable { get; private set; }
6060

61-
private void Awake()
61+
public void Reload()
6262
{
63-
Configuration = GetComponent<BacktraceClient>().Configuration;
63+
if (Configuration == null)
64+
{
65+
Configuration = GetComponent<BacktraceClient>().Configuration;
66+
}
6467
if (Configuration == null || !Configuration.IsValid())
6568
{
6669
Debug.LogWarning("Configuration doesn't exists or provided serverurl/token are invalid");
67-
_enable = false;
70+
Enable = false;
6871
return;
6972
}
7073

7174
DatabaseSettings = new BacktraceDatabaseSettings(Configuration);
7275
if (DatabaseSettings == null)
7376
{
74-
_enable = false;
77+
Enable = false;
7578
return;
7679
}
7780
if (Configuration.CreateDatabase)
7881
{
7982
Directory.CreateDirectory(Configuration.DatabasePath);
8083
}
81-
_enable = Configuration.Enabled && BacktraceConfiguration.ValidateDatabasePath(Configuration.DatabasePath);
84+
Enable = Configuration.Enabled && BacktraceConfiguration.ValidateDatabasePath(Configuration.DatabasePath);
8285

83-
if (!_enable)
86+
if (!Enable)
8487
{
8588
return;
8689
}
@@ -91,10 +94,14 @@ private void Awake()
9194
BacktraceDatabaseFileContext = new BacktraceDatabaseFileContext(DatabasePath, DatabaseSettings.MaxDatabaseSize, DatabaseSettings.MaxRecordCount);
9295
BacktraceApi = new BacktraceApi(Configuration.ToCredentials(), Convert.ToUInt32(Configuration.ReportPerMin));
9396
}
97+
private void Awake()
98+
{
99+
Reload();
100+
}
94101

95102
private void Update()
96103
{
97-
if (!_enable)
104+
if (!Enable)
98105
{
99106
return;
100107
}
@@ -114,7 +121,7 @@ private void Update()
114121

115122
private void Start()
116123
{
117-
if (!_enable)
124+
if (!Enable)
118125
{
119126
return;
120127
}
@@ -162,7 +169,7 @@ public void Clear()
162169
/// </summary>
163170
public BacktraceDatabaseRecord Add(BacktraceReport backtraceReport, Dictionary<string, object> attributes, MiniDumpType miniDumpType = MiniDumpType.Normal)
164171
{
165-
if (!_enable || backtraceReport == null)
172+
if (!Enable || backtraceReport == null)
166173
{
167174
return null;
168175
}
@@ -210,7 +217,7 @@ public void Delete(BacktraceDatabaseRecord record)
210217
/// </summary>
211218
public void Flush()
212219
{
213-
if (!_enable || !BacktraceDatabaseContext.Any())
220+
if (!Enable || !BacktraceDatabaseContext.Any())
214221
{
215222
return;
216223
}

src/Common/FormDataHelper.cs

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/Common/TypeHelper.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Backtrace.Unity.Common
5+
{
6+
public static class TypeHelper
7+
{
8+
private static readonly HashSet<Type> NumericTypes = new HashSet<Type>
9+
{
10+
typeof(int), typeof(double), typeof(decimal),
11+
typeof(long), typeof(short), typeof(sbyte),
12+
typeof(byte), typeof(ulong), typeof(ushort),
13+
typeof(uint), typeof(float)
14+
};
15+
16+
public static bool IsNumeric(Type myType)
17+
{
18+
return NumericTypes.Contains(Nullable.GetUnderlyingType(myType) ?? myType);
19+
}
20+
}
21+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Interfaces/IBacktraceAPI.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ public interface IBacktraceApi
2828
void SetClientRateLimitEvent(Action<BacktraceReport> onClientReportLimitReached);
2929

3030
void SetClientRateLimit(uint rateLimit);
31+
32+
/// <summary>
33+
/// Setup custom request method
34+
/// </summary>
35+
Func<string, BacktraceData, BacktraceResult> RequestHandler { get; set; }
3136
}
3237
}

0 commit comments

Comments
 (0)