From 037cc8a9bb9a20b57ed29c32cc183608da8fcd40 Mon Sep 17 00:00:00 2001 From: Bastian Doetsch Date: Tue, 1 Oct 2024 13:25:39 +0200 Subject: [PATCH] chore: output panic also on logger (#665) --- application/entrypoint/helpers.go | 13 ++++++- application/server/inline_values.go | 5 ++- application/server/server.go | 46 +++++++++++++++++++++++ infrastructure/code/template/details.html | 2 +- infrastructure/oss/inline_value.go | 5 +-- infrastructure/oss/template/details.html | 2 +- 6 files changed, 63 insertions(+), 10 deletions(-) diff --git a/application/entrypoint/helpers.go b/application/entrypoint/helpers.go index 27cf4576e..148db0f81 100644 --- a/application/entrypoint/helpers.go +++ b/application/entrypoint/helpers.go @@ -12,10 +12,19 @@ import ( func OnPanicRecover() { if err := recover(); err != nil { - fmt.Println("🚨 Panicking 🚨") + panickingMsg := "🚨 Panicking 🚨" + fmt.Println(panickingMsg) fmt.Println(err) debug.PrintStack() - er := sentry.NewSentryErrorReporter(config.CurrentConfig(), nil) + + c := config.CurrentConfig() + logger := c.Logger() + + logger.Error().Msg(panickingMsg) + logger.Error().Any("recovered panic", err).Send() + logger.Error().Msg(string(debug.Stack())) + + er := sentry.NewSentryErrorReporter(c, nil) er.CaptureError(fmt.Errorf("%v", err)) er.FlushErrorReporting() } diff --git a/application/server/inline_values.go b/application/server/inline_values.go index 60df77928..95b1a6bd7 100644 --- a/application/server/inline_values.go +++ b/application/server/inline_values.go @@ -18,8 +18,10 @@ package server import ( "context" + "github.com/creachadair/jrpc2" "github.com/creachadair/jrpc2/handler" + "github.com/snyk/snyk-ls/domain/snyk" "github.com/snyk/snyk-ls/application/config" @@ -34,7 +36,6 @@ func textDocumentInlineValueHandler() jrpc2.Handler { c := config.CurrentConfig() logger := c.Logger().With().Str("method", "textDocumentInlineValueHandler").Logger() documentURI := params.TextDocument.URI - logger.Debug().Msgf("Request for %s:%s RECEIVED", documentURI, params.Range.String()) defer logger.Debug().Msgf("Request for %s:%s DONE", documentURI, params.Range.String()) if s, ok := di.Scanner().(snyk.InlineValueProvider); ok { filePath := uri.PathFromUri(documentURI) @@ -43,7 +44,7 @@ func textDocumentInlineValueHandler() jrpc2.Handler { return nil, err } lspInlineValues := converter.ToInlineValues(values) - logger.Debug().Msgf("found %d inline values for %s", len(values), filePath) + logger.Trace().Msgf("found %d inline values for %s", len(values), filePath) return lspInlineValues, nil } return nil, nil diff --git a/application/server/server.go b/application/server/server.go index c00524943..c6fea2b32 100644 --- a/application/server/server.go +++ b/application/server/server.go @@ -18,7 +18,9 @@ package server import ( "context" + "errors" "fmt" + "net/http" "os" "runtime" "strings" @@ -436,6 +438,50 @@ func initializedHandler(srv *jrpc2.Server) handler.Func { }) } +func startOfflineDetection(c *config.Config) { //nolint:unused // this is gonna be used soon + go func() { + timeout := time.Second * 10 + client := c.Engine().GetNetworkAccess().GetUnauthorizedHttpClient() + client.Timeout = timeout - 1 + client.CheckRedirect = func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + } + + type logLevelConfigurable interface { + SetLogLevel(level zerolog.Level) + } + + if loggingRoundTripper, ok := client.Transport.(logLevelConfigurable); ok { + loggingRoundTripper.SetLogLevel(zerolog.ErrorLevel) + } + + for { + u := "https://downloads.snyk.io/cli/stable/version" // FIXME: which URL to use? + response, err := client.Get(u) + if err != nil { + if !c.Offline() { + msg := fmt.Sprintf("Cannot connect to %s. You need to fix your networking for Snyk to work.", u) + reportedErr := errors.Join(err, errors.New(msg)) + c.Logger().Err(reportedErr).Send() + di.Notifier().SendShowMessage(sglsp.Warning, msg) + } + c.SetOffline(true) + } else { + if c.Offline() { + msg := fmt.Sprintf("Snyk is active again. We were able to reach %s", u) + di.Notifier().SendShowMessage(sglsp.Info, msg) + c.Logger().Info().Msg(msg) + } + c.SetOffline(false) + } + if response != nil { + _ = response.Body.Close() + } + time.Sleep(timeout) + } + }() +} + func deleteExpiredCache() { w := workspace.Get() var folderList []string diff --git a/infrastructure/code/template/details.html b/infrastructure/code/template/details.html index 4fea96780..be457ab7b 100644 --- a/infrastructure/code/template/details.html +++ b/infrastructure/code/template/details.html @@ -25,7 +25,7 @@