Skip to content

Commit

Permalink
Merge pull request #1162 from procrastinate-org/wrap-django-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Aug 15, 2024
2 parents 3629782 + fee2e94 commit 7f69c10
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ repos:
- django-stubs==5.0.4
- django==4.2.15
- importlib-metadata==8.2.0
- importlib-resources==6.4.0
- importlib-resources==6.4.2
- psycopg2-binary==2.9.9
- psycopg[pool]==3.2.1
- python-dateutil==2.9.0.post0
- sphinx==7.1.2
- sqlalchemy==2.0.32
- typing-extensions==4.12.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.7
rev: v0.6.0
hooks:
- id: ruff
args: [--fix, --unsafe-fixes]
Expand Down
64 changes: 32 additions & 32 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion procrastinate/contrib/django/django_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)

import asgiref.sync
from django import db as django_db
from django.core import exceptions as django_exceptions
from django.db import connections
from django.db.backends.base.base import BaseDatabaseWrapper
Expand Down Expand Up @@ -41,7 +42,14 @@ def wrap_exceptions() -> Generator[None, None, None]:
)

with wrap():
yield
try:
yield
except django_db.DatabaseError as exc:
# __cause__ is always defined but might be None, it's set by Django
# (using `raise x from y) to the original db driver exception
if exc.__cause__:
raise exc.__cause__
raise exc


class DjangoConnector(connector.BaseAsyncConnector):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pytest-mock = "*"
migra = "*"
# migra depends on schemainspect, which has an implicit dependency on setuptools
# (pkg_resources).
setuptools = { version = "*", python = ">=3.12" }
setuptools = { version = "*" }

[tool.poetry.group.docs.dependencies]
django = ">=2.2"
Expand Down
21 changes: 16 additions & 5 deletions tests/integration/contrib/django/test_django_connector.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import pytest
from django.core import exceptions
from django.core import exceptions as django_exceptions

from procrastinate import psycopg_connector
import procrastinate.contrib.django
from procrastinate import exceptions, psycopg_connector
from procrastinate.contrib.aiopg import aiopg_connector
from procrastinate.contrib.django import django_connector as django_connector_module

Expand All @@ -13,6 +14,16 @@ def django_connector(db):
return django_connector_module.DjangoConnector(alias="default")


def test_wrap_exceptions__integrity_error(db):
@procrastinate.contrib.django.app.task(queueing_lock="foo")
def foo():
pass

with pytest.raises(exceptions.AlreadyEnqueued):
foo.defer()
foo.defer()


def test_get_sync_connector(django_connector):
assert django_connector.get_sync_connector() is django_connector

Expand All @@ -22,7 +33,7 @@ def test_open(django_connector):


def test_open_pool(django_connector):
with pytest.raises(exceptions.ImproperlyConfigured):
with pytest.raises(django_exceptions.ImproperlyConfigured):
django_connector.open(pool=object())


Expand All @@ -35,7 +46,7 @@ async def test_open_async(django_connector):


async def test_open_pool_async(django_connector):
with pytest.raises(exceptions.ImproperlyConfigured):
with pytest.raises(django_exceptions.ImproperlyConfigured):
await django_connector.open_async(pool=object())


Expand Down Expand Up @@ -112,5 +123,5 @@ async def test_get_worker_connector__error(django_connector, mocker):
return_value=False,
)

with pytest.raises(exceptions.ImproperlyConfigured):
with pytest.raises(django_exceptions.ImproperlyConfigured):
django_connector.get_worker_connector()
20 changes: 20 additions & 0 deletions tests/unit/contrib/django/test_django_connector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

import pytest
from django import db as django_db
from psycopg import errors as psycopg_errors

from procrastinate import exceptions
from procrastinate.contrib.django import django_connector as django_connector_module


def test_wrap_exceptions__no_cause():
with pytest.raises(django_db.DatabaseError):
with django_connector_module.wrap_exceptions():
raise django_db.DatabaseError


def test_wrap_exceptions__with_cause():
with pytest.raises(exceptions.ConnectorException):
with django_connector_module.wrap_exceptions():
raise django_db.DatabaseError from psycopg_errors.Error()

0 comments on commit 7f69c10

Please # to comment.