|
| 1 | +--- |
| 2 | +title: Error handling |
| 3 | +order: 10 |
| 4 | +--- |
| 5 | + |
| 6 | +# Error handling |
| 7 | + |
| 8 | +When a task raises an exception, the worker doesn't crash. It catches the exception, logs it and stores it in the |
| 9 | +`TaskiqResult` along with everything else. What you do with it afterwards depends on where you want to handle the error: |
| 10 | +on the client side, where the result is awaited, or on the worker side, where the exception happened. |
| 11 | + |
| 12 | +## Handling errors on the client |
| 13 | + |
| 14 | +`TaskiqResult` has two fields related to errors: |
| 15 | + |
| 16 | +* `is_err` - `True` if the task has failed; |
| 17 | +* `error` - the exception itself, or `None` if the task succeeded. |
| 18 | + |
| 19 | +@[code python](../examples/errors/result_error.py) |
| 20 | + |
| 21 | +If you'd rather work with exceptions than with flags, use the `raise_for_error` method. It raises the stored exception |
| 22 | +if there is one and returns the result itself otherwise, so it can be chained right after `wait_result`. |
| 23 | + |
| 24 | +@[code python](../examples/errors/raise_for_error.py) |
| 25 | + |
| 26 | +Tracebacks are not transferred to the client. Only the exception type, its arguments and the chain of `__cause__`/`__context__` |
| 27 | +exceptions are serialized. Sending tracebacks over the network is unreliable and may leak sensitive information from the |
| 28 | +worker, so Taskiq intentionally doesn't do it. |
| 29 | + |
| 30 | +Also, the client has to be able to import the exception class to restore it. If the exception is defined in a module |
| 31 | +unavailable on the client side, Taskiq creates a synthetic exception class with the same name in the `taskiq.exceptions` |
| 32 | +namespace, so `except MyError` won't catch it, while `except Exception` still will. |
| 33 | + |
| 34 | +## Handling errors on the worker |
| 35 | + |
| 36 | +Client-side handling is only possible if you use a result backend and wait for the result. Fire-and-forget tasks have |
| 37 | +nobody to report to. Besides, sometimes you want to react to every failure in one place instead of duplicating |
| 38 | +the same handling in every caller. |
| 39 | + |
| 40 | +For these cases, use the `on_error` hook of a middleware. It's called on the worker right after the exception is caught, so the full traceback is still available. |
| 41 | + |
| 42 | +@[code python](../examples/errors/error_middleware.py) |
| 43 | + |
| 44 | +That's the recommended way to integrate taskiq with error tracking systems. For example, here's how you can send all task failures to Sentry: |
| 45 | + |
| 46 | +```python |
| 47 | +from typing import Any |
| 48 | + |
| 49 | +import sentry_sdk |
| 50 | +from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger |
| 51 | + |
| 52 | +from taskiq import ( |
| 53 | + TaskiqEvents, |
| 54 | + TaskiqMessage, |
| 55 | + TaskiqMiddleware, |
| 56 | + TaskiqResult, |
| 57 | + TaskiqState, |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +class SentryMiddleware(TaskiqMiddleware): |
| 62 | + def on_error( |
| 63 | + self, |
| 64 | + message: "TaskiqMessage", |
| 65 | + result: "TaskiqResult[Any]", |
| 66 | + exception: BaseException, |
| 67 | + ) -> None: |
| 68 | + sentry_sdk.capture_exception( |
| 69 | + exception, |
| 70 | + tags={ |
| 71 | + "taskiq_task_name": message.task_name, |
| 72 | + "taskiq_task_id": message.task_id, |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +# Here `broker` is the broker you defined for your project. |
| 78 | +broker = broker.with_middlewares(SentryMiddleware()) |
| 79 | + |
| 80 | + |
| 81 | +@broker.on_event(TaskiqEvents.WORKER_STARTUP) |
| 82 | +def init_sentry(_: TaskiqState) -> None: |
| 83 | + # We ignore this logger, because its events may overwrite the exception |
| 84 | + # we capture in the middleware with new tags. |
| 85 | + ignore_logger("taskiq.receiver.receiver") |
| 86 | + sentry_sdk.init( |
| 87 | + "https://key@your-sentry-instance", |
| 88 | + environment="dev", |
| 89 | + integrations=[LoggingIntegration()], |
| 90 | + ) |
| 91 | +``` |
| 92 | + |
| 93 | +If your framework integration initializes Sentry on its own, you can drop the startup event and keep the middleware only. |
| 94 | + |
| 95 | +::: tip Retries |
| 96 | + |
| 97 | +If you want failed tasks to be retried instead of being reported, take a look at the [`SimpleRetryMiddleware`](../available-components/middlewares.md) |
| 98 | +or the [`SmartRetryMiddleware`](../available-components/middlewares.md). |
| 99 | + |
| 100 | +::: |
| 101 | + |
| 102 | +## Errors in dependencies |
| 103 | + |
| 104 | +Generator dependencies can catch exceptions raised by the task they are injected into. It's useful if you want to roll |
| 105 | +back a transaction that was opened in the dependency. This is described in |
| 106 | +[State and Dependencies](./state-and-deps.md#exception-handling), along with the `--no-propagate-errors` option |
| 107 | +that turns this behaviour off. |
| 108 | + |
| 109 | +## Skipping the result |
| 110 | + |
| 111 | +Sometimes a task shouldn't store any result at all. Raising `NoResultError` inside a task tells the worker to skip |
| 112 | +saving the result to the result backend, so waiting for the result of such a task will time out. |
| 113 | +Note that `NoResultError` is still treated as an exception internally, so `on_error` middlewares are called for it as well. |
| 114 | + |
| 115 | +```python |
| 116 | +from taskiq import NoResultError |
| 117 | + |
| 118 | + |
| 119 | +@broker.task |
| 120 | +async def my_task() -> None: |
| 121 | + raise NoResultError |
| 122 | +``` |
0 commit comments