webhook failed to execute the view
See original GitHub issueDescribe the bug I’m using dj-stripe on my application. it’s a simple app that listens to a subscription-created event and does something after that. it was working fine, but a couple of days ago it keep raising server errors after the subscription was created. it’s funny, there is nothing in the database (all djstripe records are deleted) and I myself resend the failed event (I mean it’s not duplicate requests causing this)
{"timestamp": 2022-02-07 08:40:48,129, "module": django.request, "level": ERROR, "data": Internal Server Error: /stripe/webhook/}
Traceback (most recent call last):
File "/venv/lib/python3.8/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 "djstripe_subscription_id_key"
DETAIL: Key (id)=(sub_1KOeVoHTnq9PoVa0rlLQ5udP) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/djstripe/models/base.py", line 693, in _get_or_create_from_stripe_object
cls._create_from_stripe_object(
File "/venv/lib/python3.8/site-packages/djstripe/models/base.py", line 585, in _create_from_stripe_object
instance.save(force_insert=True)
File "/venv/lib/python3.8/site-packages/django/db/models/base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "/venv/lib/python3.8/site-packages/django/db/models/base.py", line 763, in save_base
updated = self._save_table(
File "/venv/lib/python3.8/site-packages/django/db/models/base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/venv/lib/python3.8/site-packages/django/db/models/base.py", line 906, in _do_insert
return manager._insert(
File "/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "/venv/lib/python3.8/site-packages/sentry_sdk/integrations/django/__init__.py", line 500, in execute
return real_execute(self, sql, params)
File "/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/venv/lib/python3.8/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 "djstripe_subscription_id_key"
DETAIL: Key (id)=(sub_1KOeVoHTnq9PoVa0rlLQ5udP) already exists.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/venv/lib/python3.8/site-packages/sentry_sdk/integrations/django/views.py", line 67, in sentry_wrapped_callback
return callback(request, *args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/venv/lib/python3.8/site-packages/djstripe/views.py", line 36, in post
trigger = WebhookEventTrigger.from_request(request)
File "/venv/lib/python3.8/site-packages/djstripe/models/webhooks.py", line 208, in from_request
raise e
File "/venv/lib/python3.8/site-packages/djstripe/models/webhooks.py", line 194, in from_request
obj.process(save=False)
File "/venv/lib/python3.8/site-packages/djstripe/models/webhooks.py", line 285, in process
self.event = Event.process(self.json_body)
File "/venv/lib/python3.8/site-packages/djstripe/models/core.py", line 1525, in process
ret.invoke_webhook_handlers()
File "/venv/lib/python3.8/site-packages/djstripe/models/core.py", line 1537, in invoke_webhook_handlers
webhooks.call_handlers(event=self)
File "/venv/lib/python3.8/site-packages/djstripe/webhooks.py", line 98, in call_handlers
handler_func(event=event)
File "/venv/lib/python3.8/site-packages/djstripe/event_handlers.py", line 161, in customer_subscription_webhook_handler
_handle_crud_like_event(
File "/venv/lib/python3.8/site-packages/djstripe/event_handlers.py", line 427, in _handle_crud_like_event
obj = target_cls.sync_from_stripe_data(data)
File "/venv/lib/python3.8/site-packages/djstripe/models/base.py", line 935, in sync_from_stripe_data
instance, created = cls._get_or_create_from_stripe_object(
File "/venv/lib/python3.8/site-packages/djstripe/models/base.py", line 708, in _get_or_create_from_stripe_object
return cls.stripe_objects.get(id=id_), False
File "/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/venv/lib/python3.8/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
djstripe.models.billing.Subscription.DoesNotExist: Subscription matching query does not exist.
I have nightly, staging, and production environments, but this happened in the nightly Environment. BTW, I can’t access Plan and Price models on django admin. it raise server error:
{"timestamp": 2022-02-07 09:10:55,276, "module": django.request, "level": ERROR, "data": Internal Server Error: /secureadmin/djstripe/plan/}
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/django/db/models/options.py", line 608, in get_field
return self.fields_map[field_name]
KeyError: '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/django/contrib/admin/utils.py", line 265, in lookup_field
f = _get_non_gfk_field(opts, name)
File "/venv/lib/python3.8/site-packages/django/contrib/admin/utils.py", line 296, in _get_non_gfk_field
field = opts.get_field(name)
File "/venv/lib/python3.8/site-packages/django/db/models/options.py", line 610, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Plan has no field named '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 204, in _get_response
response = response.render()
File "/venv/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/venv/lib/python3.8/site-packages/sentry_sdk/integrations/django/templates.py", line 73, in rendered_content
return real_rendered_content.fget(self)
File "/venv/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/venv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/venv/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/venv/lib/python3.8/site-packages/django/contrib/admin/templatetags/base.py", line 33, in render
return super().render(context)
File "/venv/lib/python3.8/site-packages/django/template/library.py", line 214, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "/venv/lib/python3.8/site-packages/django/contrib/admin/templatetags/admin_list.py", line 308, in result_list
'results': list(results(cl)),
File "/venv/lib/python3.8/site-packages/django/contrib/admin/templatetags/admin_list.py", line 284, in results
yield ResultList(None, items_for_result(cl, res, None))
File "/venv/lib/python3.8/site-packages/django/contrib/admin/templatetags/admin_list.py", line 275, in __init__
super().__init__(*items)
File "/venv/lib/python3.8/site-packages/django/contrib/admin/templatetags/admin_list.py", line 200, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/venv/lib/python3.8/site-packages/django/contrib/admin/utils.py", line 278, in lookup_field
value = attr()
File "/venv/lib/python3.8/site-packages/djstripe/models/billing.py", line 1186, in __str__
return f"{self.human_readable_price} for {self.product.name} ({subscriptions} subscriptions)"
File "/venv/lib/python3.8/site-packages/djstripe/models/billing.py", line 1200, in human_readable_price
tier_1 = self.tiers[0]
TypeError: 'NoneType' object is not subscriptable
[pid: 95|app: 0|req: 6/10] 192.168.240.1 () {56 vars in 1097 bytes} [Mon Feb 7 09:10:55 2022] GET /secureadmin/djstripe/plan/ => generated 2591 bytes in 64 msecs (HTTP/1.0 500) 7 headers in 368 bytes (2 switches on core 0)
Note that the database is completely empty and nothing djstripe related exists. FYI: my app is hosted on AWS EC2 machines and they’re up 247. it’s not like this issue
To Reproduce Steps to reproduce the behavior: I couldn’t reproduce the issue.
Expected behavior it should execute my view that has a @webhooks.handler(‘customer.subscription.created’).
Environment
- dj-stripe version: 2.6.0
- Your Stripe account’s default API version: 2019-12-03
- Database: PostgreSQL 13.0
- Python version: 3.8.10
- Django version: 3.2.5
Can you reproduce the issue with the latest version of master? No
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Done.
@CodeWithEmad This issue has been fixed by https://github.com/dj-stripe/dj-stripe/pull/1580
@jleclanche Please consider releasing
2.6.1
with #1580