Skip to content

Commit 4034c55

Browse files
committed
server/event: Handle filters with incompatible types
1 parent ba7099f commit 4034c55

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

server/polar/meter/filter.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,26 @@ def matches(self, event: Event) -> bool:
145145
return self._compare(actual_value, self.value)
146146

147147
def _compare(self, actual: Any, expected: str | int | bool) -> bool:
148-
if self.operator == FilterOperator.eq:
149-
return actual == expected
150-
elif self.operator == FilterOperator.ne:
151-
return actual != expected
152-
elif self.operator == FilterOperator.gt:
153-
return actual > expected
154-
elif self.operator == FilterOperator.gte:
155-
return actual >= expected
156-
elif self.operator == FilterOperator.lt:
157-
return actual < expected
158-
elif self.operator == FilterOperator.lte:
159-
return actual <= expected
160-
elif self.operator == FilterOperator.like:
161-
return str(expected) in str(actual)
162-
elif self.operator == FilterOperator.not_like:
163-
return str(expected) not in str(actual)
164-
return False
148+
try:
149+
if self.operator == FilterOperator.eq:
150+
return actual == expected
151+
elif self.operator == FilterOperator.ne:
152+
return actual != expected
153+
elif self.operator == FilterOperator.gt:
154+
return actual > expected
155+
elif self.operator == FilterOperator.gte:
156+
return actual >= expected
157+
elif self.operator == FilterOperator.lt:
158+
return actual < expected
159+
elif self.operator == FilterOperator.lte:
160+
return actual <= expected
161+
elif self.operator == FilterOperator.like:
162+
return str(expected) in str(actual)
163+
elif self.operator == FilterOperator.not_like:
164+
return str(expected) not in str(actual)
165+
return False
166+
except TypeError:
167+
return False
165168

166169

167170
class FilterConjunction(StrEnum):

server/tests/meter/test_filter.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ def test_matches_metadata_number_gt(self, organization: Organization) -> None:
354354
)
355355
assert clause.matches(event) is True
356356

357+
def test_matches_type_mismatch_returns_false(
358+
self, organization: Organization
359+
) -> None:
360+
clause = FilterClause(property="name", operator=FilterOperator.gte, value=1)
361+
event = Event(
362+
name="test.event",
363+
organization_id=organization.id,
364+
source=EventSource.user,
365+
user_metadata={},
366+
)
367+
assert clause.matches(event) is False
368+
357369

358370
class TestFilterMatches:
359371
def test_matches_and_conjunction_all_true(self, organization: Organization) -> None:

0 commit comments

Comments
 (0)