Skip to content

Commit

Permalink
proxy: improve error handling if there is no response
Browse files Browse the repository at this point in the history
This patch allows to distinguish between status codes
* 502 - network error, failed to dial
* 504 - network error, dial timeout
* 500 - other error

Relates to elazarl#51
  • Loading branch information
mmatczuk committed Nov 8, 2022
1 parent 5261ef8 commit c670f39
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,29 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
resp = proxy.filterResponse(resp, ctx)

if resp == nil {
var errorString string
if ctx.Error != nil {
errorString = "error read response " + r.URL.Host + " : " + ctx.Error.Error()
ctx.Logf(errorString)
http.Error(w, ctx.Error.Error(), 502)
var (
errorString string
code int
)

if e, ok := ctx.Error.(net.Error); ok {
if e.Timeout() {
errorString = "timed out connecting to remote host: " + e.Error()
code = http.StatusGatewayTimeout
} else {
errorString = "failed to connect to remote host: " + e.Error()
code = http.StatusBadGateway
}
} else if ctx.Error != nil {
errorString = "failed to read response from remote host: " + e.Error()
code = http.StatusInternalServerError
} else {
errorString = "error read response " + r.URL.Host
ctx.Logf(errorString)
http.Error(w, errorString, 502)
errorString = "failed to read response from remote host: " + r.URL.Host
code = http.StatusInternalServerError
}

ctx.Logf(errorString)
http.Error(w, errorString, code)
return
}
ctx.Logf("Copying response to client %v [%d]", resp.Status, resp.StatusCode)
Expand Down

0 comments on commit c670f39

Please sign in to comment.