第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

未能中止()上下文 - 杜松子酒

未能中止()上下文 - 杜松子酒

Go
江戶川亂折騰 2021-11-08 10:57:04
我有以下 Gin 中間件:func CheckAppId(appC *core.Context) gin.HandlerFunc {    return func(c *gin.Context) {        //get Basic Auth credentials        appId, token, _ := c.Request.BasicAuth()        if appId == "" {            c.JSON(http.StatusOK, gin.H{"code": "MISSING_APP_ID", "message": "Your request is missing an application id"})            return //this is being ignored???        }        c.Next() //this still gets hit    }}但是如果appId == ""JSON 被返回c.Next()并被執(zhí)行。這是預(yù)期的行為嗎?編輯 我以為問(wèn)題已售出,但似乎正在發(fā)生同樣的事情。我現(xiàn)在有:func CheckAppId(appC *core.Context) gin.HandlerFunc {    return func(c *gin.Context) {        //get Basic Auth credentials        appId, token, _ := c.Request.BasicAuth()        if appId == "" {            //I'm getting this JSON in the response            c.JSON(http.StatusOK, gin.H{"code": "MISSING_APP_ID", "message": "Your request is missing an application id"})            c.Abort()        }        //find app_id in database        app := core.App{}        err := appC.Database.C("apps").Find(bson.M{"id" : appId}).One(&app)        if err != nil { //no app found            //and I'm getting this JSON in the response            c.JSON(http.StatusOK, gin.H{"code": "INVALID_APP_ID", "message": "The application id provided could not be found"})            c.Abort()        }        c.Next()    }}在調(diào)用 API 時(shí),我同時(shí)收到“MISSING_APP_ID”Json 和“INVALID_APP_ID”Json
查看完整描述

2 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

查看 Gin API 文檔,您需要調(diào)用context.Abort()而不是從您的方法返回。

Abort 停止系統(tǒng)繼續(xù)調(diào)用鏈中的掛起處理程序。假設(shè)您有一個(gè)授權(quán)中間件,如果授權(quán)失敗(密碼不匹配),它會(huì)驗(yàn)證請(qǐng)求是否獲得授權(quán)。應(yīng)調(diào)用此方法 (Abort()) 以停止實(shí)際處理程序的執(zhí)行。

所以在你的具體情況下

if appId == "" {
    c.JSON(http.StatusOK, gin.H{"code": "MISSING_APP_ID", "message": "Your request is missing an application id"})
    c.Abort()
    return
    }


查看完整回答
反對(duì) 回復(fù) 2021-11-08
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

你需要做:


if condition {

  c.Abort()

} else {

  c.Next()

}

解釋


(注意:這建立在@Landers 的回答之上)


c.Abort()只是設(shè)置一些內(nèi)部標(biāo)志,以某種方式標(biāo)記上下文,指示異常終止。這是有道理的,它是一個(gè)不返回任何內(nèi)容的無(wú)參數(shù)方法,因此您只是為了它的副作用而調(diào)用它。


同樣的事情 c.Next()


由于 go 的控制流(沒(méi)有異常或跳轉(zhuǎn)),很明顯這兩個(gè)方法不會(huì)立即采取任何行動(dòng),而是為下一階段“設(shè)置舞臺(tái)”,想象一下調(diào)用您的CheckAppIdfunc 的Gins 代碼:


// gin code

doStuff()

ctx := getContext()


// this is your CheckAppId

nextAction := FindAction()

nextAction(&ctx)


// here the context is evaluated to see if you either abort or continue.

checkContextStatus(&ctx)

因此,如果您正在調(diào)用c.Abort(),然后c.Next()您將覆蓋中止指示。


查看完整回答
反對(duì) 回復(fù) 2021-11-08
  • 2 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)