File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed
Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 33import java .util .Objects ;
44import java .util .regex .Pattern ;
55import org .jetbrains .annotations .NotNull ;
6+ import org .jetbrains .annotations .Nullable ;
67
78public final class FilterString {
89 private final @ NotNull String filterString ;
9- private final @ NotNull Pattern pattern ;
10+ private final @ Nullable Pattern pattern ;
1011
1112 public FilterString (@ NotNull String filterString ) {
1213 this .filterString = filterString ;
13- this .pattern = Pattern .compile (filterString );
14+ @ Nullable Pattern pattern = null ;
15+ try {
16+ pattern = Pattern .compile (filterString );
17+ } catch (Throwable t ) {
18+ Sentry .getCurrentScopes ()
19+ .getOptions ()
20+ .getLogger ()
21+ .log (
22+ SentryLevel .DEBUG ,
23+ "Only using filter string for String comparison as it could not be parsed as regex: %s" ,
24+ filterString );
25+ }
26+ this .pattern = pattern ;
1427 }
1528
1629 public @ NotNull String getFilterString () {
1730 return filterString ;
1831 }
1932
2033 public boolean matches (String input ) {
34+ if (pattern == null ) {
35+ return false ;
36+ }
2137 return pattern .matcher (input ).matches ();
2238 }
2339
Original file line number Diff line number Diff line change 1+ package io.sentry
2+
3+ import kotlin.test.Test
4+ import kotlin.test.assertEquals
5+ import kotlin.test.assertFalse
6+ import kotlin.test.assertTrue
7+
8+ class FilterStringTest {
9+
10+ @Test
11+ fun `turns string into pattern` () {
12+ val filterString = FilterString (" .*" )
13+ assertTrue(filterString.matches(" anything" ))
14+ assertEquals(" .*" , filterString.filterString)
15+ }
16+
17+ @Test
18+ fun `skips pattern if not a valid regex` () {
19+ // does not throw if the string is not a valid pattern
20+ val filterString = FilterString (" I love my mustache {" )
21+ assertFalse(filterString.matches(" I love my mustache {" ))
22+ assertEquals(" I love my mustache {" , filterString.filterString)
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments