コード例 #1
0
def common_error_handler(exception: BaseException) -> flask.Response:
    """Used to capture connexion exceptions and add link to the type field."""
    if isinstance(exception, ProblemException):

        link = EXCEPTIONS_LINK_MAP.get(exception.status)
        if link:
            response = problem(
                status=exception.status,
                title=exception.title,
                detail=exception.detail,
                type=link,
                instance=exception.instance,
                headers=exception.headers,
                ext=exception.ext,
            )
        else:
            response = problem(
                status=exception.status,
                title=exception.title,
                detail=exception.detail,
                type=exception.type,
                instance=exception.instance,
                headers=exception.headers,
                ext=exception.ext,
            )
    else:
        if not isinstance(exception, werkzeug.exceptions.HTTPException):
            exception = werkzeug.exceptions.InternalServerError()

        response = problem(title=exception.name,
                           detail=exception.description,
                           status=exception.code)

    return FlaskApi.get_response(response)
コード例 #2
0
    def common_error_handler(exception):
        """
        :type exception: Exception
        """
        if isinstance(exception, ProblemException):
            # exception.title show message like 'Bad Request' which is not very helpful
            response = ErrorHanlder.problem(status=exception.status,
                                            title=exception.detail,
                                            detail=exception.detail,
                                            type=exception.type,
                                            instance=exception.instance,
                                            headers=exception.headers,
                                            ext=exception.ext)

        elif isinstance(exception, ServiceException):
            response = ErrorHanlder.problem(status=exception.error_code,
                                            title=exception.error_title,
                                            detail=exception.error_msg)
        else:
            if not isinstance(exception, werkzeug.exceptions.HTTPException):
                common_exception = werkzeug.exceptions.InternalServerError()
                if os.getenv('FLASK_CONFIG', 'development') == 'development':
                    raise exception
                response = ErrorHanlder.problem(
                    title=common_exception.name,
                    detail=common_exception.description,
                    ext=str(exception),
                    status=common_exception.code)
            else:
                response = ErrorHanlder.problem(title=exception.name,
                                                detail=exception.description,
                                                ext=str(exception),
                                                status=exception.code)
        traceback.print_exc()
        return FlaskApi.get_response(response)
コード例 #3
0
def exception_handler(exception):
    log.critical("{}:{}:{}-{}".format(
        getattr(exception, 'name', 'uncaught'),
        getattr(exception, 'code', 500),
        getattr(exception, 'description', 'uncaught'),
        getattr(exception, 'type', 'about:blank')))

    trace_exception(exception, log)

    response = problem(title=getattr(exception, 'name', 'uncaught'),
                       status=getattr(exception, 'code', 500),
                       detail=getattr(exception, 'description', 'uncaught'),
                       type=getattr(exception, 'type', 'about:blank'))
    return FlaskApi.get_response(response)
def handle_phabricator_api_exception(exc):
    sentry.captureException()
    logger.error(
        "phabricator exception",
        extra={"error_code": exc.error_code, "error_info": exc.error_info},
        exc_info=exc,
    )
    return FlaskApi.get_response(
        problem(
            500,
            "Phabricator Error",
            "An unexpected error was received from Phabricator",
            type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500",
        )
    )
コード例 #5
0
def handle_phabricator_api_exception(exc):
    sentry.captureException()
    logger.error('phabricator exception',
                 extra={
                     'error_code': exc.error_code,
                     'error_info': exc.error_info,
                 },
                 exc_info=exc)
    return FlaskApi.get_response(
        problem(
            500,
            'Phabricator Error',
            'An unexpected error was received from Phabricator',
            type='https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500'
        ))
コード例 #6
0
ファイル: rest_api.py プロジェクト: edeka-spatt/checkmk
 def log_error(self, exception):
     _, exc_val, exc_tb = sys.exc_info()
     if hasattr(exception, 'name'):
         resp = problem(
             title=exception.name,
             detail=exception.description,
             status=exception.code,
         )
     else:
         resp = problem(
             title=repr(exc_val),
             detail=''.join(traceback.format_tb(exc_tb)),
             status=500,
         )
     return FlaskApi.get_response(resp)
コード例 #7
0
def handle_treestatus_exception(exc):
    sentry.captureException()
    logger.error("Tree Status exception", exc_info=exc)

    if current_app.propagate_exceptions:
        # Mimic the behaviour of Flask.handle_exception() and re-raise the full
        # traceback in test and debug environments.
        raise exc

    return FlaskApi.get_response(
        problem(
            500,
            "Tree Status Error",
            "An unexpected error was received from Tree Status",
            type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500",
        ))
コード例 #8
0
ファイル: rest_api.py プロジェクト: surajrb/checkmk
 def _make_error_response(self, exception):
     exc_info = sys.exc_info()
     logger.exception("Exception caught", exc_info=exc_info)
     _, exc_val, exc_tb = exc_info
     if hasattr(exception, 'to_problem'):
         resp = exception.to_problem()
     elif hasattr(exception, 'name'):
         resp = problem(
             title=exception.name,
             detail=exception.description,
             status=exception.code,
         )
     else:
         resp = problem(
             title=repr(exc_val),
             detail=''.join(traceback.format_tb(exc_tb)),
             status=500,
         )
     return FlaskApi.get_response(resp)
コード例 #9
0
ファイル: views.py プロジェクト: cyBerta/api
def render_generic_exception(exception):
    if not isinstance(exception, werkzeug.exceptions.HTTPException):
        exc_name = "{}.{}".format(
            type(exception).__module__,
            type(exception).__name__)
        exc_desc = str(exception)
        if hasattr(exception, "__traceback__"):
            current_app.logger.error("".join(
                traceback.format_tb(exception.__traceback__)))
        current_app.logger.error("Unhandled error occurred, {}: {}".format(
            exc_name, exc_desc))
        exception = werkzeug.exceptions.InternalServerError(
            description="An unhandled application error occurred: {}".format(
                exc_name))

    response = problem(title=exception.name,
                       detail=exception.description,
                       status=exception.code)
    return FlaskApi.get_response(response)
コード例 #10
0
ファイル: hooks.py プロジェクト: cgsheeh/lando-api
def handle_phabricator_api_exception(exc):
    sentry.captureException()
    logger.error(
        "phabricator exception",
        extra={"error_code": exc.error_code, "error_info": exc.error_info},
        exc_info=exc,
    )

    if current_app.propagate_exceptions:
        # Mimic the behaviour of Flask.handle_exception() and re-raise the full
        # traceback in test and debug environments.
        raise exc

    return FlaskApi.get_response(
        problem(
            500,
            "Phabricator Error",
            "An unexpected error was received from Phabricator",
            type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500",
        )
    )
コード例 #11
0
def connexion_error_handler(exception: Exception):
    """Handle exception from request process"""
    if flask.has_request_context:
        ext = {"request_id": request_id()}
    else:
        ext = None
    if isinstance(exception, ProblemException):
        if ext:
            if exception.ext is None:
                exception.ext = ext
            else:
                exception.ext.update(ext)
        response = exception.to_problem()
        extra = {
            "status": exception.status,
            "title": exception.title,
            "detail": exception.detail,
        }
        status_code = exception.status
        log_func = _status2log(exception.status, response_logger)
    else:
        if not isinstance(exception, HTTPException):
            exception = InternalServerError()
        response = problem(
            title=exception.name,
            detail=exception.description,
            status=exception.code,
            ext=ext,
        )
        extra = {
            "status": exception.code,
            "title": exception.name,
            "detail": exception.description,
        }
        status_code = exception.code

    log_func = _status2log(status_code, response_logger)
    log_func("HTTP error (%d)", response.status_code, extra=extra)

    return FlaskApi.get_response(response)
コード例 #12
0
def problem_exception_handler(exception):
    log.error("{}:{}:{}-{}".format(exception.title, exception.status,
                                   exception.detail, exception.type))
    trace_exception(exception, log)
    response = exception.to_problem()
    return FlaskApi.get_response(response)
コード例 #13
0
ファイル: views.py プロジェクト: cyBerta/api
def render_problem_exception(exception):
    response = exception.to_problem()
    return FlaskApi.get_response(response)