MCPcopy
hub / github.com/keploy/keploy / Recover

Function Recover

pkg/agent/proxy/util/util.go:934–967  ·  view source on GitHub ↗

Recover recovers from a panic in any parser and logs the stack trace to Sentry. It also closes the client and destination connection.

(logger *zap.Logger, client, dest net.Conn)

Source from the content-addressed store, hash-verified

932// Recover recovers from a panic in any parser and logs the stack trace to Sentry.
933// It also closes the client and destination connection.
934func Recover(logger *zap.Logger, client, dest net.Conn) {
935 if logger == nil {
936 fmt.Println(Emoji + "Failed to recover from panic. Logger is nil.")
937 return
938 }
939
940 sentry.Flush(2 * time.Second)
941 if r := recover(); r != nil {
942 logger.Error("Recovered from panic in parser, closing active connections")
943 if client != nil {
944 err := client.Close()
945 if err != nil {
946 // Use string matching as a last resort to check for the specific error
947 if !strings.Contains(err.Error(), "use of closed network connection") {
948 // Log other errors
949 utils.LogError(logger, err, "failed to close the client connection")
950 }
951 }
952 }
953
954 if dest != nil {
955 err := dest.Close()
956 if err != nil {
957 // Use string matching as a last resort to check for the specific error
958 if !strings.Contains(err.Error(), "use of closed network connection") {
959 // Log other errors
960 utils.LogError(logger, err, "failed to close the destination connection")
961 }
962 }
963 }
964 utils.HandleRecovery(logger, r, "Recovered from panic")
965 sentry.Flush(time.Second * 2)
966 }
967}

Callers 5

startMethod · 0.92
ReadBytesFunction · 0.70
ReadRequiredBytesFunction · 0.70
ReadFromPeerFunction · 0.70
PassThroughFunction · 0.70

Calls 5

LogErrorFunction · 0.92
HandleRecoveryFunction · 0.92
FlushMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected