internal: improve error handling for internal reverse proxy
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
48f4a971ef
commit
3e403fa348
|
@ -1,6 +1,7 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
@ -67,7 +68,18 @@ func (ws *WebServer) configureProxy() {
|
||||||
func (ws *WebServer) proxyErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
|
func (ws *WebServer) proxyErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
|
||||||
ws.log.Warning(err.Error())
|
ws.log.Warning(err.Error())
|
||||||
rw.WriteHeader(http.StatusBadGateway)
|
rw.WriteHeader(http.StatusBadGateway)
|
||||||
_, err = rw.Write([]byte("authentik starting..."))
|
em := fmt.Sprintf("failed to connect to authentik backend: %v", err)
|
||||||
|
if !ws.p.IsRunning() {
|
||||||
|
em = "authentik starting..."
|
||||||
|
}
|
||||||
|
// return json if the client asks for json
|
||||||
|
if req.Header.Get("Accept") == "application/json" {
|
||||||
|
eem, _ := json.Marshal(map[string]string{
|
||||||
|
"error": em,
|
||||||
|
})
|
||||||
|
em = string(eem)
|
||||||
|
}
|
||||||
|
_, err = rw.Write([]byte(em))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ws.log.WithError(err).Warning("failed to write error message")
|
ws.log.WithError(err).Warning("failed to write error message")
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue