我有一个用Go编写的服务,使用包。net/httphttp.HandleFunc("/hello", Hello)log.Fatal(http.ListenAndServe(":8080", nil))目前为止,一切都好。在 里面,代码存根如下:func Hellofunc Hello(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("token") if err != nil { if err == http.ErrNoCookie { // If the cookie is not set, return an unauthorized status w.WriteHeader(http.StatusUnauthorized) log.WithFields(log.Fields{ "fnc": "Hello", }).Fatal(err) return //exit status 1 < Server stops here. } w.WriteHeader(http.StatusBadRequest) log.WithFields(log.Fields{ "fnc": "Hello", }).Fatal(err) return }问题是当代码流到达请求中找不到cookie的地方时,程序/服务器在返回语句之后存在,控制台显示,并且在客户端收到错误 ErrNoCookie exit status 1 Error: read ECONNRESET但是,作为一项服务,我希望它继续运行并响应后续请求。我做错了什么?
1 回答
慕田峪4524236
TA贡献1875条经验 获得超5个赞
你称之为:
log.WithFields(log.Fields{
"fnc": "Hello",
}).Fatal(err)
就在回来之前。实际上以代码 1 退出。https://godoc.org/github.com/sirupsen/logrus#FatalFatal
尝试删除对 的所有调用。Fatal
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报
0/150
提交
取消