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)
| 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. |
| 934 | func 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 | } |
no test coverage detected