Skip to content

Commit 758bdae

Browse files
committed
EDM-2403: Handle initialization errors gracefully instead of panic
1 parent f7b0dc7 commit 758bdae

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

proxy/app.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func main() {
3838

3939
tlsConfig, err := bridge.GetTlsConfig()
4040
if err != nil {
41-
panic(err)
41+
log.WithError(err).Error("Failed to get TLS configuration")
42+
os.Exit(1)
4243
}
4344

4445
apiRouter.Handle("/flightctl/{forward:.*}", bridge.NewFlightCtlHandler(tlsConfig))
@@ -73,7 +74,8 @@ func main() {
7374
if config.OcpPlugin != "true" {
7475
authHandler, err := auth.NewAuth(tlsConfig)
7576
if err != nil {
76-
panic(err)
77+
log.WithError(err).Error("Failed to initialize authentication")
78+
os.Exit(1)
7779
}
7880
apiRouter.HandleFunc("/login", authHandler.Login)
7981
apiRouter.HandleFunc("/login/info", authHandler.GetUserInfo)
@@ -92,7 +94,8 @@ func main() {
9294
if config.TlsKeyPath != "" && config.TlsCertPath != "" {
9395
cert, err := tls.LoadX509KeyPair(config.TlsCertPath, config.TlsKeyPath)
9496
if err != nil {
95-
panic(err)
97+
log.WithError(err).Error("Failed to load TLS certificate")
98+
os.Exit(1)
9699
}
97100
serverTlsconfig = &tls.Config{
98101
Certificates: []tls.Certificate{cert},

proxy/bridge/handler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"net/http"
66
"net/http/httputil"
77
"net/url"
8+
"os"
89

910
"github.com/gorilla/mux"
1011

1112
"github.com/flightctl/flightctl-ui/config"
13+
"github.com/flightctl/flightctl-ui/log"
1214
)
1315

1416
type handler struct {
@@ -28,7 +30,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2830
func createReverseProxy(apiURL string) (*url.URL, *httputil.ReverseProxy) {
2931
target, err := url.Parse(apiURL)
3032
if err != nil {
31-
panic(err)
33+
log.GetLogger().WithError(err).Errorf("Failed to parse URL '%s'", apiURL)
34+
os.Exit(1)
3235
}
3336
proxy := httputil.NewSingleHostReverseProxy(target)
3437
proxy.ModifyResponse = func(r *http.Response) error {

0 commit comments

Comments
 (0)