Example #1
0
    def default(self, request, exception):
        """
        Provide a default behavior for the objects of :class:`ErrorHandler`.
        If a developer chooses to extent the :class:`ErrorHandler` they can
        provide a custom implementation for this method to behave in a way
        they see fit.

        :param request: Incoming request
        :param exception: Exception object

        :type request: :class:`sanic.request.Request`
        :type exception: :class:`sanic.exceptions.SanicException` or
            :class:`Exception`
        :return:
        """
        quiet = getattr(exception, "quiet", False)
        if quiet is False:
            try:
                url = repr(request.url)
            except AttributeError:
                url = "unknown"

            self.log(format_exc())
            logger.exception("Exception occurred while handling uri: %s", url)

        return exception_response(request, exception, self.debug)
Example #2
0
def test_should_return_html_valid_setting(fake_request, fallback, content_type,
                                          exception, status):
    if fallback:
        fake_request.app.config.FALLBACK_ERROR_FORMAT = fallback

    try:
        raise exception("bad stuff")
    except Exception as e:
        response = exception_response(fake_request, e, True)

    assert isinstance(response, HTTPResponse)
    assert response.status == status
    assert response.content_type == content_type
Example #3
0
def test_combinations_for_auto(fake_request, accept, content_type, expected):
    if accept:
        fake_request.headers["accept"] = accept
    else:
        del fake_request.headers["accept"]

    if content_type:
        fake_request.headers["content-type"] = content_type

    try:
        raise Exception("bad stuff")
    except Exception as e:
        response = exception_response(
            fake_request,
            e,
            True,
            base=HTMLRenderer,
            fallback="auto",
        )

    assert response.content_type == expected
Example #4
0
    def default(self, request, exception):
        """
        Provide a default behavior for the objects of :class:`ErrorHandler`.
        If a developer chooses to extent the :class:`ErrorHandler` they can
        provide a custom implementation for this method to behave in a way
        they see fit.

        :param request: Incoming request
        :param exception: Exception object

        :type request: :class:`sanic.request.Request`
        :type exception: :class:`sanic.exceptions.SanicException` or
            :class:`Exception`
        :return:
        """
        self.log(request, exception)
        return exception_response(
            request,
            exception,
            debug=self.debug,
            base=self.base,
            fallback=self.fallback,
        )