Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
richardelms committed Jun 5, 2024
1 parent ae03f24 commit b617554
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public Configuration GetConfig()
{
config.DiscardClasses.Add(new System.Text.RegularExpressions.Regex(discardedClass));
}
catch (Exception e)
catch (ArgumentException e)
{
Debug.LogError("Regex error adding discard class: " + e.Message);
Debug.LogError("Invalid Regex pattern for discard class: " + e.Message);
}
}
if (EnabledReleaseStages != null && EnabledReleaseStages.Length > 0)
Expand All @@ -113,9 +113,9 @@ public Configuration GetConfig()
{
config.RedactedKeys.Add(new System.Text.RegularExpressions.Regex(key));
}
catch (Exception e)
catch (ArgumentException e)
{
Debug.LogError("Regex error adding redacted key: " + e.Message);
Debug.LogError("Invalid Regex pattern for redacted key: " + e.Message);
}
}
if (string.IsNullOrEmpty(ReleaseStage))
Expand Down
4 changes: 2 additions & 2 deletions src/BugsnagUnity/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class Configuration : IMetadataEditor, IFeatureFlagStore
public List<Regex> RedactedKeys = new List<Regex>{new Regex(".*password.*",RegexOptions.IgnoreCase)};
public bool KeyIsRedacted(string key)
{
if (RedactedKeys == null || RedactedKeys.Count == 0)
if (RedactedKeys == null)
{
return false;
}
Expand All @@ -77,7 +77,7 @@ public bool KeyIsRedacted(string key)

internal bool ErrorClassIsDiscarded(string className)
{
if (DiscardClasses == null || DiscardClasses.Count == 0)
if (DiscardClasses == null)
{
return false;
}
Expand Down
12 changes: 10 additions & 2 deletions src/BugsnagUnity/Native/Android/NativeInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ AndroidJavaObject CreateNativeConfig(Configuration config)
{
patternsAsStrings[config.DiscardClasses.IndexOf(pattern)] = pattern.ToString();
}

obj.Call("setDiscardClasses", GetAndroidRegexPatternSetFromArray(patternsAsStrings));
}

Expand Down Expand Up @@ -525,8 +526,15 @@ private AndroidJavaObject GetAndroidRegexPatternSetFromArray(string[] array)

foreach (var item in array)
{
AndroidJavaObject pattern = patternClass.CallStatic<AndroidJavaObject>("compile", item);
set.Call<bool>("add", pattern);
try
{
AndroidJavaObject pattern = patternClass.CallStatic<AndroidJavaObject>("compile", item);
set.Call<bool>("add", pattern);
}
catch (AndroidJavaException e)
{
Debug.LogWarning("Failed to compile regex pattern: " + item + " " + e.Message);
}
}

return set;
Expand Down
22 changes: 2 additions & 20 deletions tests/BugsnagUnity.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void RedactedKeysTest()
var config = new Configuration("foo");

// Default redacted keys
Assert.IsTrue(config.KeyIsRedacted("password"));
Assert.IsTrue(config.KeyIsRedacted("user-password"));
Assert.IsFalse(config.KeyIsRedacted("username"));

var config2 = new Configuration("foo");
Expand All @@ -101,15 +101,7 @@ public void RedactedKeysTest()
Assert.IsTrue(config2.KeyIsRedacted("secret"));
Assert.IsTrue(config2.KeyIsRedacted("token"));
Assert.IsTrue(config2.KeyIsRedacted("password"));

var config3 = new Configuration("foo");

// Regex pattern keys
config3.RedactedKeys.Add(new Regex(".*api_key.*", RegexOptions.IgnoreCase));
config3.RedactedKeys.Add(new Regex(".*token_value.*", RegexOptions.IgnoreCase));
Assert.IsTrue(config3.KeyIsRedacted("api_key"));
Assert.IsTrue(config3.KeyIsRedacted("token_value"));
Assert.IsTrue(config3.KeyIsRedacted("password"));
Assert.IsFalse(config2.KeyIsRedacted("app_id"));
}

[Test]
Expand All @@ -128,16 +120,6 @@ public void DiscardedClassesTest()
Assert.IsTrue(config2.ErrorClassIsDiscarded("System.Exception"));
Assert.IsTrue(config2.ErrorClassIsDiscarded("System.NullReferenceException"));
Assert.IsFalse(config2.ErrorClassIsDiscarded("System.ArgumentException"));

var config3 = new Configuration("foo");

// Regex pattern discard classes
config3.DiscardClasses.Add(new Regex("^System\\..*Exception$", RegexOptions.IgnoreCase));
config3.DiscardClasses.Add(new Regex(".*ReferenceException", RegexOptions.IgnoreCase));
Assert.IsTrue(config3.ErrorClassIsDiscarded("System.Exception"));
Assert.IsTrue(config3.ErrorClassIsDiscarded("System.NullReferenceException"));
Assert.IsTrue(config3.ErrorClassIsDiscarded("CustomReferenceException"));
Assert.IsFalse(config3.ErrorClassIsDiscarded("ArgumentError"));
}
}
}

0 comments on commit b617554

Please sign in to comment.