我想使用 Http Header 将我得到的所有错误发送到另一个服务(使用我的服务)例如:我试过这个,但它不起作用:func main() { http.HandleFunc("/", foo) log.Println("Listening...") http.ListenAndServe(":6001", nil) }func foo(w http.ResponseWriter, r *http.Request) { w.Header().Set("successfull", "A Go Web Server") fi := path.Join("templates/VastPlayer", "TempVide_.txt") tmpl, err := template.ParseFiles(fi) if err != nil { w.Header().Set("Error", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) } if err := tmpl.Execute(w, ""); err != nil{ w.Header().Set("Error", err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) } }如果我提供一个验证模板,我会在标题上获得“成功”:“A Go Web Server”,但是如果我没有提供现有模板,我会在标题上获得502 Bad Gateway和 thisHTTP/1.1 502 Bad GatewayServer: nginx/1.8.0Date: Mon, 06 Jul 2015 15:19:31 GMTContent-Type: text/htmlContent-Length: 574Connection: keep-alive我想知道是否有办法发送我通过标题得到的错误,我的意思是模板/VastPlayer/TempVide_.txt:没有这样的文件或目录
1 回答
猛跑小猪
TA贡献1858条经验 获得超8个赞
的502
响应,从nginx的到来,所以先测试围棋服务器直接在端口6001同样,看看输出为您的服务器的过程中,有错误将在那里打印。
设置错误标头并调用后,http.Error
您需要一个return
,否则您将继续执行处理程序的其余部分。由于tmpl
如果出现错误则为 nil,因此调用tmpl.Execute
会导致 nil 指针取消引用,并且服务器会发生混乱。
(并且您开始设置“成功”标头,因此即使出现错误,它也将始终存在。)
- 1 回答
- 0 关注
- 211 浏览
添加回答
举报
0/150
提交
取消