diff options
Diffstat (limited to 'pydis_site')
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/infraction.py | 7 | ||||
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/user.py | 9 | 
2 files changed, 14 insertions, 2 deletions
| diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 8da82822..19b6f2d8 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -284,7 +284,12 @@ class InfractionViewSet(          """          try:              return super().create(request, *args, **kwargs) -        except IntegrityError as err: +        except IntegrityError as err:  # pragma: no cover - see below +            # Not covered: DRF handles this via `UniqueTogetherValidator` these +            # days, which means it's hard to test this branch specifically. +            # However, in a productive deployment, it's still very much +            # possible for two concurrent inserts to run into IntegrityError. +              # We need to use `__cause__` here, as Django reraises the internal              # UniqueViolation emitted by psycopg2 (which contains the attribute              # that we actually need) diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index c0b4ca0f..79e867c3 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -395,7 +395,14 @@ class UserViewSet(ModelViewSet):                      raise ParseError(detail={                          "source": ["The user may not be an alternate account of itself"]                      }) -                if err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships': +                if ( +                    err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships' +                ):  # pragma: no cover - see below +                    # This is not covered because newer DRF versions automatically validate this, +                    # however the validation is done via a SELECT query which may race concurrent +                    # inserts in prod. The only correct way is e.g. what Ecto does which is +                    # associating the validators to the unique constraint errors to match in +                    # errors, anything else may race.                      raise ParseError(detail={                          "source": ["This relationship has already been established"]                      }) | 
