Skip to content

Commit dd0738d

Browse files
committed
feat: support for handling custom exceptions in middleware.
1 parent 0b53745 commit dd0738d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

taskiq/middlewares/simple_retry_middleware.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from logging import getLogger
2-
from typing import Any
2+
from typing import Any, Iterable, Optional
33

44
from taskiq.abc.middleware import TaskiqMiddleware
55
from taskiq.exceptions import NoResultError
@@ -18,10 +18,12 @@ def __init__(
1818
default_retry_count: int = 3,
1919
default_retry_label: bool = False,
2020
no_result_on_retry: bool = True,
21+
types_of_exceptions: Optional[Iterable[type[BaseException]]] = None,
2122
) -> None:
2223
self.default_retry_count = default_retry_count
2324
self.default_retry_label = default_retry_label
2425
self.no_result_on_retry = no_result_on_retry
26+
self.types_of_exceptions = types_of_exceptions
2527

2628
async def on_error(
2729
self,
@@ -42,6 +44,11 @@ async def on_error(
4244
:param result: execution result.
4345
:param exception: found exception.
4446
"""
47+
if self.types_of_exceptions is not None and not isinstance(
48+
exception, tuple(self.types_of_exceptions)
49+
):
50+
return
51+
4552
# Valid exception
4653
if isinstance(exception, NoResultError):
4754
return

taskiq/middlewares/smart_retry_middleware.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
import random
33
from logging import getLogger
4-
from typing import Any, Optional
4+
from typing import Any, Optional, Iterable
55

66
from taskiq import ScheduleSource
77
from taskiq.abc.middleware import TaskiqMiddleware
@@ -35,6 +35,7 @@ def __init__(
3535
use_delay_exponent: bool = False,
3636
max_delay_exponent: float = 60,
3737
schedule_source: Optional[ScheduleSource] = None,
38+
types_of_exceptions: Optional[Iterable[type[BaseException]]] = None,
3839
) -> None:
3940
"""
4041
Initialize retry middleware.
@@ -48,6 +49,7 @@ def __init__(
4849
:param max_delay_exponent: Maximum allowed delay when using backoff.
4950
:param schedule_source: Schedule source to use for scheduling.
5051
If None, the default broker will be used.
52+
:param types_of_exceptions: Types of exceptions to retry from.
5153
"""
5254
super().__init__()
5355
self.default_retry_count = default_retry_count
@@ -58,6 +60,7 @@ def __init__(
5860
self.use_delay_exponent = use_delay_exponent
5961
self.max_delay_exponent = max_delay_exponent
6062
self.schedule_source = schedule_source
63+
self.types_of_exceptions = types_of_exceptions
6164

6265
if not isinstance(schedule_source, (ScheduleSource, type(None))):
6366
raise TypeError(
@@ -138,6 +141,11 @@ async def on_error(
138141
:param result: Execution result.
139142
:param exception: Caught exception.
140143
"""
144+
if self.types_of_exceptions is not None and not isinstance(
145+
exception, tuple(self.types_of_exceptions)
146+
):
147+
return
148+
141149
if isinstance(exception, NoResultError):
142150
return
143151

0 commit comments

Comments
 (0)