Пример #1
0
def _render_exception(e: Exception, title: str) -> Response:
    if plain_error():
        html.set_output_format("text")
        if title:
            title = "%s: " % title
        html.write("%s%s\n" % (title, e))

    elif not fail_silently():
        html.header(title, Breadcrumb())
        html.show_error(str(e))
        html.footer()

    return html.response
Пример #2
0
def _render_exception(e: Exception, title: str) -> Response:
    if plain_error():
        return Response(
            response=[
                "%s%s\n" % (("%s: " % title) if title else "", e),
            ],
            mimetype="text/plain",
        )

    if not fail_silently():
        html.header(title, Breadcrumb())
        html.show_error(str(e))
        html.footer()

    return response
Пример #3
0
def _process_request(environ, start_response) -> Response:  # pylint: disable=too-many-branches
    try:
        html.init_modes()

        # Make sure all plugins are available as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        page_handler = get_and_wrap_page(html.myfile)
        response = page_handler()
        # If page_handler didn't raise we assume everything is OK.
        response.status_code = http_client.OK
    except HTTPRedirect as e:
        # This can't be a new Response as it can have already cookies set/deleted by the pages.
        # We can't return the response because the Exception has been raised instead.
        # TODO: Remove all HTTPRedirect exceptions from all pages. Making the Exception a subclass
        #       of Response may also work as it can then be directly returned from here.
        response = html.response
        response.status_code = e.status
        response.headers["Location"] = e.url

    except FinalizeRequest as e:
        # TODO: Remove all FinalizeRequest exceptions from all pages and replace it with a `return`.
        #       It may be necessary to rewire the control-flow a bit as this exception could have
        #       been used to short-circuit some code and jump directly to the response. This
        #       needs to be changed as well.
        response = html.response
        response.status_code = e.status

    except livestatus.MKLivestatusNotFoundError as e:
        response = _render_exception(e, title=_("Data not found"))

    except MKUserError as e:
        response = _render_exception(e, title=_("Invalid user Input"))

    except MKAuthException as e:
        response = _render_exception(e, title=_("Permission denied"))

    except livestatus.MKLivestatusException as e:
        response = _render_exception(e, title=_("Livestatus problem"))
        response.status_code = http_client.BAD_GATEWAY

    except MKUnauthenticatedException as e:
        response = _render_exception(e, title=_("Not authenticated"))
        response.status_code = http_client.UNAUTHORIZED

    except MKConfigError as e:
        response = _render_exception(e, title=_("Configuration error"))
        logger.error("MKConfigError: %s", e)

    except MKGeneralException as e:
        response = _render_exception(e, title=_("General error"))
        logger.error("MKGeneralException: %s", e)

    except Exception:
        crash_reporting.handle_exception_as_gui_crash_report(
            plain_error=plain_error(),
            fail_silently=fail_silently(),
            show_crash_link=getattr(g, "may_see_crash_reports", False))
        # This needs to be cleaned up.
        response = html.response

    return response(environ, start_response)