From 758bdae17755b9045c9cf29c5ec9477dc30d48df Mon Sep 17 00:00:00 2001 From: Celia Amador Date: Thu, 23 Oct 2025 11:01:33 +0200 Subject: [PATCH] EDM-2403: Handle initialization errors gracefully instead of panic --- proxy/app.go | 9 ++++++--- proxy/bridge/handler.go | 5 ++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/proxy/app.go b/proxy/app.go index e3331d32f..7e989fe6a 100644 --- a/proxy/app.go +++ b/proxy/app.go @@ -38,7 +38,8 @@ func main() { tlsConfig, err := bridge.GetTlsConfig() if err != nil { - panic(err) + log.WithError(err).Error("Failed to get TLS configuration") + os.Exit(1) } apiRouter.Handle("/flightctl/{forward:.*}", bridge.NewFlightCtlHandler(tlsConfig)) @@ -73,7 +74,8 @@ func main() { if config.OcpPlugin != "true" { authHandler, err := auth.NewAuth(tlsConfig) if err != nil { - panic(err) + log.WithError(err).Error("Failed to initialize authentication") + os.Exit(1) } apiRouter.HandleFunc("/login", authHandler.Login) apiRouter.HandleFunc("/login/info", authHandler.GetUserInfo) @@ -92,7 +94,8 @@ func main() { if config.TlsKeyPath != "" && config.TlsCertPath != "" { cert, err := tls.LoadX509KeyPair(config.TlsCertPath, config.TlsKeyPath) if err != nil { - panic(err) + log.WithError(err).Error("Failed to load TLS certificate") + os.Exit(1) } serverTlsconfig = &tls.Config{ Certificates: []tls.Certificate{cert}, diff --git a/proxy/bridge/handler.go b/proxy/bridge/handler.go index c19e611af..39222d092 100644 --- a/proxy/bridge/handler.go +++ b/proxy/bridge/handler.go @@ -5,10 +5,12 @@ import ( "net/http" "net/http/httputil" "net/url" + "os" "github.com/gorilla/mux" "github.com/flightctl/flightctl-ui/config" + "github.com/flightctl/flightctl-ui/log" ) type handler struct { @@ -28,7 +30,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func createReverseProxy(apiURL string) (*url.URL, *httputil.ReverseProxy) { target, err := url.Parse(apiURL) if err != nil { - panic(err) + log.GetLogger().WithError(err).Errorf("Failed to parse URL '%s'", apiURL) + os.Exit(1) } proxy := httputil.NewSingleHostReverseProxy(target) proxy.ModifyResponse = func(r *http.Response) error {