嘗試區(qū)分錯(cuò)誤的用戶 cookie 錯(cuò)誤與使用gorilla/sessionseg 的內(nèi)部錯(cuò)誤import "github.com/gorilla/sessions"sess, err := store.Get(r, sessName)if err != nil { // either user error (bad-cookie i.e. invalid HMAC) // http.Error(w, "not authenticated", http.StatusUnauthorized) // or server error (FileSystemStore i/o) // http.Error(w, "internal error", http.StatusInternalServerError) return}底層securecookie包有一個(gè)針對(duì)錯(cuò)誤用戶 cookie 的導(dǎo)出錯(cuò)誤ErrMacInvalid。所以通常人們只會(huì)檢查這個(gè)特定的錯(cuò)誤,但這不起作用:import "github.com/gorilla/securecookie"if err == securecookie.ErrMacInvalid { // bad user-cookie} else if err != nil { // otherwise internal error}它不起作用的原因 - 使用 say作為會(huì)話存儲(chǔ) - 是它將返回類型為securecookie.MultiError (一種類型)securecookie.NewCookieStore()的錯(cuò)誤,其值列在錯(cuò)誤片中。[]errorsecurecookie.ErrMacInvalid嘗試這樣的事情似乎很復(fù)雜:if e2, ok := err.(securecookie.MultiError); ok && len(e2) > 0 && e2[0] == securecookie.ErrMacInvalid { { // bad user-cookie} else if err != nil { // otherwise internal error}有更容易的方法嗎?
- 1 回答
- 0 關(guān)注
- 124 瀏覽
添加回答
舉報(bào)
0/150
提交
取消