Golang:使用Http标头发送错误

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

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