Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app.go
diff options
context:
space:
mode:
authorJaime Martinez <jmartinez@gitlab.com>2021-10-07 07:28:15 +0300
committerVladimir Shushlin <v.shushlin@gmail.com>2021-10-14 14:32:38 +0300
commit98e38acc90217a7a5bdb23b048c10d47d6dee1f7 (patch)
treea73354f15d6598470df83e95fbf87f83eb5f06c6 /app.go
parent7e8f58590804c78f2c27f51212781d50bb1321ed (diff)
feat: add panic handler middleware
Changelog: added
Diffstat (limited to 'app.go')
-rw-r--r--app.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/app.go b/app.go
index b97ec0bc..eba3c690 100644
--- a/app.go
+++ b/app.go
@@ -289,6 +289,7 @@ func (a *theApp) buildHandlerPipeline() (http.Handler, error) {
if a.config.General.PropagateCorrelationID {
correlationOpts = append(correlationOpts, correlation.WithPropagation())
}
+ handler = handlePanicMiddleware(handler)
handler = correlation.InjectCorrelationID(handler, correlationOpts...)
// This MUST be the last handler!
@@ -499,3 +500,21 @@ func (a *theApp) TLSConfig() (*cryptotls.Config, error) {
return tls.Create(a.config.General.RootCertificate, a.config.General.RootKey, a.ServeTLS,
a.config.General.InsecureCiphers, a.config.TLS.MinVersion, a.config.TLS.MaxVersion)
}
+
+// handlePanicMiddleware logs and captures the recover() information from any panic
+func handlePanicMiddleware(handler http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ defer func() {
+ i := recover()
+ if i != nil {
+ err := fmt.Errorf("panic trace: %v", i)
+ metrics.PanicRecoveredCount.Inc()
+ logging.LogRequest(r).WithError(err).Error("recovered from panic")
+ errortracking.Capture(err, errortracking.WithRequest(r), errortracking.WithContext(r.Context()))
+ httperrors.Serve500(w)
+ }
+ }()
+
+ handler.ServeHTTP(w, r)
+ })
+}