# Buggy code
def report_error(error, request=None, extra_data=None, level="warning"):
    """Wrapper for error reporting

    This can be used for store exceptions in error reporting solutions as
    rollbar while handling error gracefully and giving user cleaner message.
    """
    if HAS_ROLLBAR and hasattr(settings, "ROLLBAR"):
        rollbar.report_exc_info(request=request, extra_data=extra_data, level=level)

    if HAS_RAVEN and hasattr(settings, "RAVEN_CONFIG"):
        raven_client.captureException(request=request, extra=extra_data, level=level)

    LOGGER.error(
        "Handled exception %s: %s", error.__class__.__name__, force_text(error)
    )


# Traceback (ConnectionRefusedError)
2019-05-01 15:08:46,128 ERROR Internal Server Error: /create/component/
Traceback (most recent call last):
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/views/create.py", line 195, in dispatch
return super(CreateComponent, self).dispatch(request, *args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 142, in post
return self.form_valid(form)
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/views/create.py", line 143, in form_valid
result = super(CreateComponent, self).form_valid(form)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/views/generic/edit.py", line 125, in form_valid
self.object = form.save()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/forms/models.py", line 458, in save
self.instance.save()
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/models/component.py", line 1619, in save
self.create_translations()
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/models/component.py", line 1289, in create_translations
translation.notify_new(request)
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/models/translation.py", line 166, in notify_new
author=request.user if request else None,
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/models/change.py", line 148, in create
return super(ChangeManager, self).create(user=user, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/opt/bitnami/apps/weblate/weblate/weblate/trans/models/change.py", line 452, in save
notify_change.delay(self.pk)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/task.py", line 427, in delay
return self.apply_async(args, kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/task.py", line 552, in apply_async
link=link, link_error=link_error, **options)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/task.py", line 774, in apply
ret = tracer(task_id, args, kwargs, request)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/trace.py", line 399, in trace_task
I, R, state, retval = on_error(task_request, exc, uuid)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/celery/app/base.py", line 472, in run
return task._orig_run(*args, **kwargs)
File "/opt/bitnami/apps/weblate/weblate/weblate/accounts/tasks.py", line 78, in notify_change
with get_connection() as connection:
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/base.py", line 45, in __enter__
self.open()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/opt/bitnami/python/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/bitnami/python/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

# Fixed code
def report_error(
    error, request=None, extra_data=None, level="warning", prefix="Handled exception"
):
    """Wrapper for error reporting

    This can be used for store exceptions in error reporting solutions as
    rollbar while handling error gracefully and giving user cleaner message.
    """
    if HAS_ROLLBAR and hasattr(settings, "ROLLBAR"):
        rollbar.report_exc_info(request=request, extra_data=extra_data, level=level)

    if HAS_RAVEN and hasattr(settings, "RAVEN_CONFIG"):
        raven_client.captureException(request=request, extra=extra_data, level=level)

    LOGGER.error("%s: %s: %s", prefix, error.__class__.__name__, force_text(error))