0

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

6
  • how are you going to resolve the ParseFiles incorrect path issue? Commented Jul 6, 2015 at 14:28
  • why doesn't work , this is my question when I check the header don't find "Error" on the header. I tiried without panic doesn't work. I don't want to resolve **ParseFiles ** I just want to send the message through a header Commented Jul 6, 2015 at 14:33
  • 1
    @Aziz: it doesn't work because you're calling panic and aborting the handler. Don't do that. Commented Jul 6, 2015 at 14:35
  • @JimB I tried without panic,it doesn't work. Commented Jul 6, 2015 at 14:42
  • Put some debugging statements in there to make sure it's doing what you expect. We can't tell what this code does out of context, (plus job_1.Complete(health.Panic) sounds like it might panic too). Please make a reproducible example if you can't figure out what's going on. Commented Jul 6, 2015 at 14:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great the problem was the return I put it after the http.Error and it works perfectly now. Gracias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.