Skip to content

Commit

Permalink
chore: output panic also on logger (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch authored Oct 1, 2024
1 parent e38c7a6 commit 037cc8a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
13 changes: 11 additions & 2 deletions application/entrypoint/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 3 additions & 2 deletions application/server/inline_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand Down
46 changes: 46 additions & 0 deletions application/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package server

import (
"context"
"errors"
"fmt"
"net/http"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/code/template/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<style nonce="${nonce}">
:root {
--default-font: ui-sans-serif, "SF Pro Text", "Segoe UI", "Ubuntu", Tahoma, Geneva, Verdana, sans-serif;
--default-font: ui-sans-serif, "SF Pro Text", "Segoe UI", "Ubuntu", Geneva, Verdana, Tahoma, sans-serif;
}

::-webkit-scrollbar {
Expand Down
5 changes: 1 addition & 4 deletions infrastructure/oss/inline_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ type inlineValueMap map[string][]snyk.InlineValue

func (cliScanner *CLIScanner) GetInlineValues(path string, myRange snyk.Range) (result []snyk.InlineValue, err error) {
logger := cliScanner.config.Logger().With().Str("method", "CLIScanner.GetInlineValues").Logger()
logger.Debug().Str("path", path).Msg("called")
cliScanner.inlineValueMutex.RLock()
inlineValues := cliScanner.inlineValues[path]
cliScanner.inlineValueMutex.RUnlock()
result = filterInlineValuesForRange(inlineValues, myRange)
logger.Debug().Str("path", path).Msgf("%d inlineValues found", len(result))
logger.Trace().Str("path", path).Msgf("%d inlineValues found", len(result))
return result, nil
}

func (cliScanner *CLIScanner) ClearInlineValues(path string) {
logger := cliScanner.config.Logger().With().Str("method", "CLIScanner.ClearInlineValues").Logger()
cliScanner.inlineValueMutex.Lock()
cliScanner.inlineValues[path] = nil
cliScanner.inlineValueMutex.Unlock()
logger.Debug().Str("path", path).Msg("called")
}

func filterInlineValuesForRange(inlineValues []snyk.InlineValue, myRange snyk.Range) (result []snyk.InlineValue) {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/oss/template/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<style nonce="${nonce}">
:root {
--default-font: "SF Pro Text", "Segoe UI", "Ubuntu", Tahoma, Geneva, Verdana, sans-serif;
--default-font: ui-sans-serif, "SF Pro Text", "Segoe UI", "Ubuntu", Geneva, Verdana, Tahoma, sans-serif;
}

::-webkit-scrollbar {
Expand Down

0 comments on commit 037cc8a

Please sign in to comment.