溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行FaultWrapper解析

發(fā)布時間:2021-12-28 15:42:00 來源:億速云 閱讀:136 作者:柒染 欄目:云計(jì)算

這篇文章將為大家詳細(xì)講解有關(guān)如何進(jìn)行FaultWrapper解析,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

FaultWrapper解析

class FaultWrapper(base_wsgi.Middleware):#繼承基礎(chǔ)的WSGI中間件"""調(diào)用中間件堆棧,將exceptions封裝成faults。"""    _status_to_type = {}@staticmethod    def status_to_type(status):
        #根據(jù)狀態(tài)碼轉(zhuǎn)換成webob相應(yīng)的類型實(shí)例if not FaultWrapper._status_to_type:for clazz in utils.walk_class_hierarchy(webob.exc.HTTPError):# 遍歷HTTPError類層次結(jié)構(gòu) 
                FaultWrapper._status_to_type[clazz.code] = clazzreturn FaultWrapper._status_to_type.get(
                                  status, webob.exc.HTTPInternalServerError)()def _error(self, inner, req):
        LOG.exception(_LE("Caught error: %s"), inner)

        safe = getattr(inner, 'safe', False)
        headers = getattr(inner, 'headers', None)
        status = getattr(inner, 'code', 500)if status is None:
            status = 500        msg_dict = dict(url=req.url, status=status)
        LOG.info(_LI("%(url)s returned with HTTP %(status)d"), msg_dict)
        outer = self.status_to_type(status)#調(diào)用exception轉(zhuǎn)換方法if headers:
            outer.headers = headers# NOTE(johannes): We leave the explanation empty here on        # purpose. It could possibly have sensitive information        # that should not be returned back to the user. See        # bugs 868360 and 874472        # NOTE(eglynn): However, it would be over-conservative and        # inconsistent with the EC2 API to hide every exception,        # including those that are safe to expose, see bug 1021373        if safe:
            #處理翻譯
            user_locale = req.best_match_language()
            inner_msg = translate(inner.message, user_locale)
            outer.explanation = '%s: %s' % (inner.__class__.__name__,
                                            inner_msg)

        notifications.send_api_fault(req.url, status, inner)#調(diào)用rpc發(fā)送api錯誤到消息隊(duì)列return wsgi.Fault(outer) #返回webob.exc.HTTPException錯誤對象

    @webob.dec.wsgify(RequestClass=wsgi.Request)def __call__(self, req):try:return req.get_response(self.application) #執(zhí)行wsgi應(yīng)用程序except Exception as ex:return self._error(ex, req)#遇到exception后,調(diào)用此方法處理exception,封裝成http錯誤返回

關(guān)于如何進(jìn)行FaultWrapper解析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI