Golang:使用HTTP头发送错误信息

huangapple go评论63阅读模式
英文:

Golang: Send errors using Http Header

问题

我想将我收到的所有错误通过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)
  }
}

如果我提供一个有效的模板,我会在Header中得到**"successfull" : "A Go Web Server",但如果我提供一个不存在的模板,我会得到502 Bad Gateway**,并且Header中会有以下内容:

HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Mon, 06 Jul 2015 15:19:31 GMT
Content-Type: text/html
Content-Length: 574
Connection: keep-alive

我想知道是否有一种方法可以通过Header发送我得到的错误,比如templates/VastPlayer/TempVide_.txt: no such file or directory

提前谢谢你。

英文:

I want to send all the errors that I get to another service(Consume my service) using Http Header

Ex: I tried this but it doesn't work:

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)
		  }
  }

if I give a valide template I got "successfull" : "A Go Web Server" on the Header, but if I give no existing tempalte I got 502 Bad Gateway and this on the header

HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Mon, 06 Jul 2015 15:19:31 GMT
Content-Type: text/html
Content-Length: 574
Connection: keep-alive

I want to know if there is a way to send the Error that i got through a header,
I mean templates/VastPlayer/TempVide_.txt: no such file or directory

Thank you in advance

答案1

得分: 1

502响应是由nginx返回的,所以首先在端口6001上直接测试go服务器。此外,查看服务器进程的输出,错误将在那里打印出来。

在设置错误头并调用http.Error之后,你需要加上return,否则将继续执行处理程序的其余部分。由于如果出现错误,tmpl为nil,调用tmpl.Execute会导致空指针解引用,从而使服务器发生崩溃。

(而且你一开始设置了一个“成功”的头部,所以即使出现错误,它也会一直存在。)

英文:

The 502 response, is coming from nginx, so first test the go server directly on port 6001. Also, look at the output for your server's process, there error will be printed there.

After you set your error header and call http.Error you need a return, otherwise you're going to continue executing the rest of the handler. Since tmpl is nil if there was an error, calling tmpl.Execute causes a nil pointer dereference, and the server panics.

(And you start out setting a "successful" header, so that will always be there, even if there's an error.)

huangapple
  • 本文由 发表于 2015年7月6日 22:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/31248242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定