policies: improve error handling when using bindings without policy

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-09-16 11:04:31 +02:00
parent ebf9f0ca63
commit 17503365f7
1 changed files with 4 additions and 4 deletions

View File

@ -81,11 +81,11 @@ class PolicyEngine:
.iterator() .iterator()
) )
def _check_policy_type(self, policy: Policy): def _check_policy_type(self, binding: PolicyBinding):
"""Check policy type, make sure it's not the root class as that has no logic implemented""" """Check policy type, make sure it's not the root class as that has no logic implemented"""
# pyright: reportGeneralTypeIssues=false # pyright: reportGeneralTypeIssues=false
if policy.__class__ == Policy: if binding.policy is not None and binding.policy.__class__ == Policy:
raise TypeError(f"Policy '{policy}' is root type") raise TypeError(f"Policy '{binding.policy}' is root type")
def build(self) -> "PolicyEngine": def build(self) -> "PolicyEngine":
"""Build wrapper which monitors performance""" """Build wrapper which monitors performance"""
@ -102,7 +102,7 @@ class PolicyEngine:
for binding in self._iter_bindings(): for binding in self._iter_bindings():
self.__expected_result_count += 1 self.__expected_result_count += 1
self._check_policy_type(binding.policy) self._check_policy_type(binding)
key = cache_key(binding, self.request) key = cache_key(binding, self.request)
cached_policy = cache.get(key, None) cached_policy = cache.get(key, None)
if cached_policy and self.use_cache: if cached_policy and self.use_cache: