Handle unexpected statuscodes from the log provider

**What**
- Use NotImplemented when we get a 404 from the log provider.  When we
get a 200, process the log request, as normal.  For all other status
codes, return a sever error with a message stating that the
response was unexpected.  The message will contain the original status
code to assist with debugging

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
Lucas Roesler 2019-06-23 20:46:34 +02:00 committed by Alex Ellis
parent 02ccccbe60
commit 74d08126b3

View File

@ -2,6 +2,7 @@ package handlers
import ( import (
"context" "context"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -66,17 +67,25 @@ func NewLogHandlerFunc(logProvider url.URL, timeout time.Duration) http.HandlerF
} }
defer logResp.Body.Close() defer logResp.Body.Close()
// watch for connection closures and stream data switch logResp.StatusCode {
// connections and contexts should have cancel methods deferred already case http.StatusNotFound, http.StatusNotImplemented:
select { w.WriteHeader(http.StatusNotImplemented)
case err := <-copyNotify(&unbufferedWriter{wf}, logResp.Body): return
if err != nil { case http.StatusOK:
log.Printf("LogProxy: error while copy: %s", err.Error()) // watch for connection closures and stream data
// connections and contexts should have cancel methods deferred already
select {
case err := <-copyNotify(&unbufferedWriter{wf}, logResp.Body):
if err != nil {
log.Printf("LogProxy: error while copy: %s", err.Error())
return
}
case <-cn.CloseNotify():
log.Printf("LogProxy: client connection closed")
return return
} }
case <-cn.CloseNotify(): default:
log.Printf("LogProxy: client connection closed") http.Error(w, fmt.Sprintf("unknown log request error (%v)", logResp.StatusCode), http.StatusInternalServerError)
return
} }
return return