# Buggy code
def response(self, request, exception):
    """Fetches and executes an exception handler and returns a response
    object

    :param request: Request
    :param exception: Exception to handle
    :return: Response object
    """
    handler = self.handlers.get(type(exception), self.default)
    try:
        response = handler(request=request, exception=exception)
    except Exception:
        self.log(format_exc())
        if self.debug:
            response_message = (
                'Exception raised in exception handler "{}" for uri: "{}"\n{}'
            ).format(handler.__name__, request.url, format_exc())
            log.error(response_message)
            return text(response_message, 500)
        else:
            return text("An error occurred while handling an error", 500)
    return response


# Traceback (AttributeError)
2017-02-24 07:53:18,845 ERROR Process-22219 MainThread sanic.handlers.log:73    Traceback (most recent call last):
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/sanic/handlers.py", line 54, in response
response = handler(request=request, exception=exception)
File "app.py", line 486, in default
sentry.captureException()
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/raven/base.py", line 799, in captureException
'raven.events.Exception', exc_info=exc_info, **kwargs)
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/raven/base.py", line 615, in capture
elif not self.should_capture(exc_info):
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/raven/base.py", line 803, in should_capture
exc_name = '%s.%s' % (exc_type.__module__, exc_type.__name__)
AttributeError: 'NoneType' object has no attribute '__module__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/sanic/server.py", line 201, in write_error
response = self.error_handler.response(self.request, exception)
File "/home/kpath/kpath/venv/lib/python3.6/site-packages/sanic/handlers.py", line 61, in response
handler.__name__, request.url, format_exc())
AttributeError: 'NoneType' object has no attribute 'url'

2017-02-24 07:53:18,845 ERROR Process-22219 MainThread sanic.server.bail_out:226   Writing error failed, connection closed AttributeError("'NoneType' object has no attribute 'url'",)

# Fixed code
def response(self, request, exception):
    """Fetches and executes an exception handler and returns a response
    object

    :param request: Request
    :param exception: Exception to handle
    :return: Response object
    """
    handler = self.handlers.get(type(exception), self.default)
    try:
        response = handler(request=request, exception=exception)
    except Exception:
        self.log(format_exc())
        if self.debug:
            url = getattr(request, "url", "unknown")
            response_message = (
                'Exception raised in exception handler "{}" for uri: "{}"\n{}'
            ).format(handler.__name__, url, format_exc())
            log.error(response_message)
            return text(response_message, 500)
        else:
            return text("An error occurred while handling an error", 500)
    return response