diff --git a/recovery.go b/recovery.go index bc946c03..3d4e588d 100644 --- a/recovery.go +++ b/recovery.go @@ -33,6 +33,18 @@ func Recovery() HandlerFunc { // RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one. func RecoveryWithWriter(out io.Writer) HandlerFunc { + return RecoveryWithWriterAndCallback(out, func(c *Context, err interface{}, brokenPipe bool) { + // If the connection is dead, we can't write a status to it. + if brokenPipe { + c.Error(err.(error)) // nolint: errcheck + c.Abort() + } else { + c.AbortWithStatus(http.StatusInternalServerError) + } + }) +} + +func RecoveryWithWriterAndCallback(out io.Writer, fn func(c *Context, err interface{}, brokenPipe bool)) HandlerFunc { var logger *log.Logger if out != nil { logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags) @@ -70,14 +82,7 @@ func RecoveryWithWriter(out io.Writer) HandlerFunc { timeFormat(time.Now()), err, stack, reset) } } - - // If the connection is dead, we can't write a status to it. - if brokenPipe { - c.Error(err.(error)) // nolint: errcheck - c.Abort() - } else { - c.AbortWithStatus(http.StatusInternalServerError) - } + fn(c, err, brokenPipe) } }() c.Next()