mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
345e7280a1
Former-commit-id: 765b43602655fad7f525ca7a5f7f297a6167d075
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// Package recover provides recovery for specific routes or for the whole app via middleware. See _examples/miscellaneous/recover
|
|
package recover
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/kataras/iris/context"
|
|
)
|
|
|
|
func getRequestLogs(ctx context.Context) string {
|
|
var status, ip, method, path string
|
|
status = strconv.Itoa(ctx.GetStatusCode())
|
|
path = ctx.Path()
|
|
method = ctx.Method()
|
|
ip = ctx.RemoteAddr()
|
|
// the date should be logged by iris' Logger, so we skip them
|
|
return fmt.Sprintf("%v %s %s %s", status, path, method, ip)
|
|
}
|
|
|
|
// New returns a new recover middleware,
|
|
// it recovers from panics and logs
|
|
// the panic message to the application's logger "Warn" level.
|
|
func New() context.Handler {
|
|
return func(ctx context.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
if ctx.IsStopped() {
|
|
return
|
|
}
|
|
|
|
var stacktrace string
|
|
for i := 1; ; i++ {
|
|
_, f, l, got := runtime.Caller(i)
|
|
if !got {
|
|
break
|
|
|
|
}
|
|
|
|
stacktrace += fmt.Sprintf("%s:%d\n", f, l)
|
|
}
|
|
|
|
// when stack finishes
|
|
logMessage := fmt.Sprintf("Recovered from a route's Handler('%s')\n", ctx.HandlerName())
|
|
logMessage += fmt.Sprintf("At Request: %s\n", getRequestLogs(ctx))
|
|
logMessage += fmt.Sprintf("Trace: %s\n", err)
|
|
logMessage += fmt.Sprintf("\n%s", stacktrace)
|
|
ctx.Application().Logger().Warn(logMessage)
|
|
|
|
ctx.StatusCode(500)
|
|
ctx.StopExecution()
|
|
}
|
|
}()
|
|
|
|
ctx.Next()
|
|
}
|
|
}
|