Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/admin/autocomplete/create can throw 500 errors #1483

Open
eloquence opened this issue Aug 29, 2022 · 2 comments
Open

/admin/autocomplete/create can throw 500 errors #1483

eloquence opened this issue Aug 29, 2022 · 2 comments
Labels

Comments

@eloquence
Copy link
Member

Traceback from 2022-08-26:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "incident_charge_title_key"
DETAIL:  Key (title)=(penalties for political advertising) already exists.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.9/site-packages/wagtail/admin/auth.py", line 174, in decorated_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/wagtailautocomplete/views.py", line 109, in create
    instance = method(value)
  File "/django/incident/models/items.py", line 94, in autocomplete_create
    return kls.objects.create(title=value)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 453, in create
    obj.save(force_insert=True, using=self.db)
  File "/usr/local/lib/python3.9/site-packages/modelcluster/models.py", line 201, in save
    super().save(update_fields=real_update_fields, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 739, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 776, in save_base
    updated = self._save_table(
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 881, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 919, in _do_insert
    return manager._insert(
  File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "incident_charge_title_key"
DETAIL:  Key (title)=(penalties for political advertising) already exists.
@eloquence eloquence added the bug label Aug 29, 2022
@harrislapiroff
Copy link
Contributor

It's not clear if this one has been fixed in the course of autocomplete development in the past year or now. Someone should investigate.

@soleilera soleilera added someday and removed someday labels Oct 4, 2023
@chigby
Copy link
Contributor

chigby commented Nov 28, 2023

Version 0.11.0 of wagtail-autocomplete (the latest) supports a more graceful handling of ValidationErrors raised by the autocomplete_create method. Right now, according to the traceback in this issue, the autocomplete_create method is raising an IntegrityError that's not being handled at all. We can update our autocomplete_create methods to make sure the new object is valid before trying to save it, and raise a validation error otherwise, using some fairly straightforward additions as demonstrated in the wagtail-autocomplete docs:

https://github.com/wagtail/wagtail-autocomplete/blob/0cfc28d04acd58dc3b48d976eb4c7a1d43e6f68d/wagtailautocomplete/tests/testapp/models.py#L16-L21:

    @classmethod
    def autocomplete_create(kls: type, value: str):
        instance = kls(title=value)
        instance.full_clean()
        instance.save()
        return instance

If we do this, similar situations to the one that triggered the error in this issue should result in 400s with appropriate error messages to the client. See wagtail/wagtail-autocomplete#160 for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants