コード例 #1
0
    def is_healthy(self, ctx) -> bool:
        db_health = self.health_repository.ping(ctx)
        if not db_health:
            LOG.error("health_check_db_not_healthy", extra=log_extra(ctx))
            return False

        # TODO: add more health checks?

        LOG.info("health_check_success", extra=log_extra(ctx))
        return True
コード例 #2
0
def handle_error(ctx, e: Exception):
    if isinstance(e, NotFoundError):
        return _error(404, str(e)), 404
    elif isinstance(e, UnauthorizedError):
        return _error(403, str(e)), 403
    elif isinstance(
            e, ValueError) or isinstance(e, ValidationError) or isinstance(
                e, NetworkManagerReadError) or isinstance(
                    e, AlreadyExistsError):
        return _error(400, str(e)), 400
    else:
        LOG.error('Fatal exception: ' + str(e), extra=log_extra(ctx))
        return _error(500, "The server encountered an unexpected error"), 500
コード例 #3
0
ファイル: with_context.py プロジェクト: minet/adh6
    def wrapper(*args,**kwargs):
        """
        Wrap http_api function.
        """
        s = session_handler.session() if session_handler else db.session()

        if "token_info" not in connexion.context:
            LOG.warning('could_not_extract_token_info_kwargs')
            raise UnauthenticatedError("Not token informations")

        token_info = connexion.context["token_info"]
        testing = current_app.config["TESTING"]
        ctx = build_context(
            session=s,
            testing=testing,
            request_id=str(uuid.uuid4()),  # Generates an unique ID on this request so we can track it.
            url=request.url,
            admin=token_info.get("uid", ""),
            roles=token_info.get("scope", [])
        )
        kwargs["ctx"] = ctx
        try:
            result = f(*args, **kwargs)

            # It makes things clearer and less error-prone.
            if not isinstance(result, tuple) or len(result) <= 1:
                raise ValueError("Please always pass the result AND the HTTP code.")

            status_code = result[1]
            msg = result[0]
            if result[0] == NoContent:
                msg = None
            if status_code and (200 <= status_code <= 299 or status_code == 302):
                s.commit()
            else:
                LOG.info("rollback_sql_transaction_non_200_http_code",
                         extra=log_extra(ctx, code=status_code, message=msg))
                s.rollback()
            return result

        except Exception as e:
            LOG.error("rollback_sql_transaction_exception_caught",
                      extra=log_extra(ctx, exception=str(e), traceback=traceback.format_exc()))
            s.rollback()
            raise

        finally:
            # When running unit tests, we don't close the session so tests can actually perform some work on that
            # session.
            if not testing:
                s.close()