Skip to content

Commit cc1f1c7

Browse files
committed
Preventing catching KeyError exceptions from callback function
1 parent 40a7754 commit cc1f1c7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

cachebox/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,17 @@ def _wrapped(*args, **kwds):
247247
# try to get result from cache
248248
try:
249249
result = cache[key]
250+
except KeyError:
251+
pass
252+
else:
253+
# A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback`
254+
# so don't wrap it with try except
250255
hits += 1
251256

252257
if callback is not None:
253258
callback(EVENT_HIT, key, result)
254259

255260
return _copy_if_need(result, level=copy_level)
256-
except KeyError:
257-
pass
258261

259262
with locks[key]:
260263
if exceptions.get(key, None) is not None:
@@ -327,6 +330,11 @@ async def _wrapped(*args, **kwds):
327330
# try to get result from cache
328331
try:
329332
result = cache[key]
333+
except KeyError:
334+
pass
335+
else:
336+
# A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback`
337+
# so don't wrap it with try except
330338
hits += 1
331339

332340
if callback is not None:
@@ -335,8 +343,6 @@ async def _wrapped(*args, **kwds):
335343
await awaitable
336344

337345
return _copy_if_need(result, level=copy_level)
338-
except KeyError:
339-
pass
340346

341347
async with locks[key]:
342348
if exceptions.get(key, None) is not None:

0 commit comments

Comments
 (0)