Skip to content

Commit 05262e4

Browse files
committed
Capture proxy middleware error
Signed-off-by: Vishal Rana <[email protected]>
1 parent b129098 commit 05262e4

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

middleware/proxy.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,22 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
9292
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9393
in, _, err := c.Response().Hijack()
9494
if err != nil {
95-
c.Error(fmt.Errorf("proxy raw, hijack error=%v, url=%s", t.URL, err))
95+
c.Set("_error", fmt.Sprintf("proxy raw, hijack error=%v, url=%s", t.URL, err))
9696
return
9797
}
9898
defer in.Close()
9999

100100
out, err := net.Dial("tcp", t.URL.Host)
101101
if err != nil {
102-
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err))
103-
c.Error(he)
102+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, dial error=%v, url=%s", t.URL, err)))
104103
return
105104
}
106105
defer out.Close()
107106

108107
// Write header
109108
err = r.Write(out)
110109
if err != nil {
111-
he := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err))
112-
c.Error(he)
110+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("proxy raw, request header copy error=%v, url=%s", t.URL, err)))
113111
return
114112
}
115113

@@ -123,7 +121,7 @@ func proxyRaw(t *ProxyTarget, c echo.Context) http.Handler {
123121
go cp(in, out)
124122
err = <-errCh
125123
if err != nil && err != io.EOF {
126-
c.Logger().Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err)
124+
c.Set("_error", fmt.Errorf("proxy raw, copy body error=%v, url=%s", t.URL, err))
127125
}
128126
})
129127
}
@@ -251,6 +249,9 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
251249
default:
252250
proxyHTTP(tgt, c, config).ServeHTTP(res, req)
253251
}
252+
if e, ok := c.Get("_error").(error); ok {
253+
err = e
254+
}
254255

255256
return
256257
}

middleware/proxy_1_11.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handle
1717
if tgt.Name != "" {
1818
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
1919
}
20-
c.Logger().Errorf("remote %s unreachable, could not forward: %v", desc, err)
21-
c.Error(echo.NewHTTPError(http.StatusServiceUnavailable))
20+
c.Set("_error", echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err)))
2221
}
2322
proxy.Transport = config.Transport
2423
return proxy

0 commit comments

Comments
 (0)