google-app-engine - 中间件错误处理

标签 google-app-engine go

我有一个链 claw http.Handler 中间件,我的第一个处理程序可能会在其中写入错误响应:

http.Error(w, err.Error(), http.StatusUnauthorized)

但是我的其他中间件继续执行,但我不希望它继续执行。最好的方法是什么?我尝试在调用 http.Error() 后检查状态 header ,看看它是否不是 200:

status := w.Header().Get("Status")

但是状态是一个空字符串。

最佳答案

您可以在错误发生后立即使用“裸”return 来停止中间件链的执行。

来自 http documentation :

Error replies to the request with the specified error message and HTTP code. It does not otherwise end the request; the caller should ensure no further writes are done to w. The error message should be plain text.

从这里Custom Handlers and Avoiding Globals in Go Web Applications :

func myHandler(w http.ResponseWriter, r *http.Request) {
    session, err := store.Get(r, "myapp")
    if err != nil {
        http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
        return // Forget to return, and the handler will continue on
    }
    .../...
}

关于google-app-engine - 中间件错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39285029/

相关文章:

google-app-engine - 从 ComputeEngine 访问 DataStore

android - 如何使用带有 Cloud Endpoints 的 Google App Engine 从 Android 设备上的数据存储中检索数据?

http - 为什么我的接口(interface)不包含一个值,如果我明确 "associated"

用于遍历 map 的 Golang 不会返回不一致的值

go - 在 Go 中启动后进行守护进程

go - 如何在http客户端上使用Socks4

python - DeadlineExceededError : ApplicationError: 5 in using urllib2. urlopen() 函数

Python App Engine 队列任务

python - Google App Engine 不断部署新实例,或者根本不部署(服务器错误)

go - 如何在 Go Gin 中使用模板获取动态内容