當前位置: 首頁>>代碼示例>>Python>>正文


Python httpexceptions.HTTPException方法代碼示例

本文整理匯總了Python中pyramid.httpexceptions.HTTPException方法的典型用法代碼示例。如果您正苦於以下問題:Python httpexceptions.HTTPException方法的具體用法?Python httpexceptions.HTTPException怎麽用?Python httpexceptions.HTTPException使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyramid.httpexceptions的用法示例。


在下文中一共展示了httpexceptions.HTTPException方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _capture_exception

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def _capture_exception(exc_info):
    # type: (ExcInfo) -> None
    if exc_info[0] is None or issubclass(exc_info[0], HTTPException):
        return
    hub = Hub.current
    if hub.get_integration(PyramidIntegration) is None:
        return

    # If an integration is there, a client has to be there.
    client = hub.client  # type: Any

    event, hint = event_from_exception(
        exc_info,
        client_options=client.options,
        mechanism={"type": "pyramid", "handled": False},
    )

    hub.capture_event(event, hint=hint) 
開發者ID:getsentry,項目名稱:sentry-python,代碼行數:20,代碼來源:pyramid.py

示例2: log_uncaught_exceptions

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def log_uncaught_exceptions(handler, registry):
    """Tween to log all uncaught exceptions."""

    def log_uncaught_exceptions_tween(request):
        try:
            return handler(request)
        except HTTPException:
            raise
        except Exception:
            lines = ["Uncaught exception while processing request:\n"]
            lines.append("%s %s\n" % (request.method, request.path_url))
            lines.append(safer_format_traceback(*sys.exc_info()))
            mozsvc.logger.error("".join(lines))
            raise

    return log_uncaught_exceptions_tween 
開發者ID:mozilla-services,項目名稱:shavar,代碼行數:18,代碼來源:tweens.py

示例3: handle_view_exception

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def handle_view_exception(exception, status_code, error_message=None):
    def handle_view_exception_decorator(func):
        def handle_exception(request):
            try:
                return func(request)
            except exception as e:
                if not isinstance(e, HTTPException):
                    raise exception_response(
                        status_code,
                        detail=error_message or repr(e)
                    )
                else:
                    raise e
        return handle_exception
    return handle_view_exception_decorator 
開發者ID:Yelp,項目名稱:schematizer,代碼行數:17,代碼來源:decorators.py

示例4: fuzz_backoff_headers

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def fuzz_backoff_headers(handler, registry):
    """Add some random fuzzing to the value of various backoff headers.

    This can help to avoid a "dogpile" effect where all backed-off clients
    retry at the same time and overload the server.
    """

    HEADERS = ["Retry-After", "X-Backoff", "X-Weave-Backoff"]

    def fuzz_response(response):
        for header in HEADERS:
            value = response.headers.get(header)
            if value is not None:
                # The header value is a backoff duration in seconds.  Fuzz
                # it upward by up to 5% or 5 seconds, whichever is greater.
                value = int(value)
                max_fuzz = max(int(value * 0.05), 5)
                value += random.randint(0, max_fuzz)
                response.headers[header] = str(value)

    def fuzz_backoff_headers_tween(request):
        try:
            response = handler(request)
        except HTTPException as response:
            fuzz_response(response)
            raise
        else:
            fuzz_response(response)
            return response

    return fuzz_backoff_headers_tween 
開發者ID:mozilla-services,項目名稱:shavar,代碼行數:33,代碼來源:tweens.py

示例5: __call__

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def __call__(self, request):
        ctx = tracer.extract(ot.Format.HTTP_HEADERS, request.headers)        
        scope = tracer.start_active_span('http', child_of=ctx)

        scope.span.set_tag(ext.SPAN_KIND, ext.SPAN_KIND_RPC_SERVER)
        scope.span.set_tag("http.host", request.host)
        scope.span.set_tag(ext.HTTP_METHOD, request.method)
        scope.span.set_tag(ext.HTTP_URL, request.path)

        if request.matched_route is not None:
            scope.span.set_tag("http.path_tpl", request.matched_route.pattern)

        if hasattr(agent, 'extra_headers') and agent.extra_headers is not None:
            for custom_header in agent.extra_headers:
                # Headers are available in this format: HTTP_X_CAPTURE_THIS
                h = ('HTTP_' + custom_header.upper()).replace('-', '_')
                if h in request.headers:
                    scope.span.set_tag("http.%s" % custom_header, request.headers[h])

        if len(request.query_string):
            scrubbed_params = strip_secrets(request.query_string, agent.secrets_matcher, agent.secrets_list)
            scope.span.set_tag("http.params", scrubbed_params)

        response = None
        try:
            response = self.handler(request)
            
            tracer.inject(scope.span.context, ot.Format.HTTP_HEADERS, response.headers)
            response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id
        except HTTPException as e:
            response = e
            raise
        except BaseException as e:
            scope.span.set_tag("http.status", 500)

            # we need to explicitly populate the `message` tag with an error here
            # so that it's picked up from an SDK span
            scope.span.set_tag("message", str(e))            
            scope.span.log_exception(e)
            
            logger.debug("Pyramid Instana tween", exc_info=True)
        finally:
            if response:
                scope.span.set_tag("http.status", response.status_int)
                
                if 500 <= response.status_int <= 511:
                    if response.exception is not None:
                        message = str(response.exception)
                        scope.span.log_exception(response.exception)
                    else:
                        message = response.status
                        
                    scope.span.set_tag("message", message)
                    scope.span.assure_errored()

            scope.close()

        return response 
開發者ID:instana,項目名稱:python-sensor,代碼行數:60,代碼來源:tweens.py

示例6: send_backoff_responses

# 需要導入模塊: from pyramid import httpexceptions [as 別名]
# 或者: from pyramid.httpexceptions import HTTPException [as 別名]
def send_backoff_responses(handler, registry):
    """Send backoff/unavailable responses to a percentage of clients.

    This tween allows the server to respond to a set percentage of traffic with
    an X-Backoff header and/or a "503 Service Unavilable" response.  The two
    probabilities are controlled by config options 'mozsvc.backoff_probability'
    and 'mozsvc.unavailable_probability' respectively.  If neither option is
    set then the tween is not activated, avoiding overhead in the (hopefully!)
    common case.
    """
    settings = registry.settings
    backoff_probability = settings.get("mozsvc.backoff_probability", 0)
    unavailable_probability = settings.get("mozsvc.unavailable_probability", 0)
    retry_after = settings.get("mozsvc.retry_after", 1800)

    if backoff_probability:

        backoff_probability = float(backoff_probability)

        def add_backoff_header(response):
            if "X-Backoff" not in response.headers:
                if "X-Weave-Backoff" not in response.headers:
                    response.headers["X-Backoff"] = str(retry_after)
                    response.headers["X-Weave-Backoff"] = str(retry_after)

        def send_backoff_header_tween(request, handler=handler):
            try:
                response = handler(request)
            except HTTPException as response:
                if random.random() < backoff_probability:
                    add_backoff_header(response)
                raise
            else:
                if random.random() < backoff_probability:
                    add_backoff_header(response)
                return response

        handler = send_backoff_header_tween

    if unavailable_probability:

        unavailable_probability = float(unavailable_probability)

        def send_unavailable_response_tween(request, handler=handler):
            if random.random() < unavailable_probability:
                return HTTPServiceUnavailable(body="0",
                                              retry_after=retry_after,
                                              content_type="application/json")
            return handler(request)

        handler = send_unavailable_response_tween

    return handler 
開發者ID:mozilla-services,項目名稱:shavar,代碼行數:55,代碼來源:tweens.py


注:本文中的pyramid.httpexceptions.HTTPException方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。