mirror of
https://github.com/kataras/iris.git
synced 2026-01-20 18:35:57 +00:00
20 days of unstoppable work. Waiting fo go 1.8, I didn't finish yet, some touches remains.
Former-commit-id: ed84f99c89f43fe5e980a8e6d0ee22c186f0e1b9
This commit is contained in:
31
middleware/recover/_example/main.go
Normal file
31
middleware/recover/_example/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
"gopkg.in/kataras/iris.v6/middleware/recover"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Adapt(httprouter.New())
|
||||
|
||||
app.Adapt(iris.DevLogger()) // fast way to enable non-fatal messages to be printed to the user
|
||||
// (yes in iris even recover's errors are not fatal because it's restarting,
|
||||
// ProdMode messages are only for things that Iris cannot continue at all,
|
||||
// these are logged by-default but you can change that behavior too by passing a different LoggerPolicy to the .Adapt)
|
||||
app.Use(recover.New()) // it's io.Writer is the same as app.Config.LoggerOut
|
||||
|
||||
i := 0
|
||||
// let's simmilate a panic every next request
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
i++
|
||||
if i%2 == 0 {
|
||||
panic("a panic here")
|
||||
}
|
||||
ctx.Writef("Hello, refresh one time more to get panic!")
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
app.Listen(":8080")
|
||||
}
|
||||
58
middleware/recover/recover.go
Normal file
58
middleware/recover/recover.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package recover
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
)
|
||||
|
||||
func getRequestLogs(ctx *iris.Context) string {
|
||||
var status, ip, method, path string
|
||||
status = strconv.Itoa(ctx.ResponseWriter.StatusCode())
|
||||
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 logs to the LoggerOut iris' configuration field if its IsDeveloper configuration field is enabled.
|
||||
// otherwise it just continues to serve
|
||||
func New() iris.HandlerFunc {
|
||||
return func(ctx *iris.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.GetHandlerName())
|
||||
logMessage += fmt.Sprintf("At Request: %s\n", getRequestLogs(ctx))
|
||||
logMessage += fmt.Sprintf("Trace: %s\n", err)
|
||||
logMessage += fmt.Sprintf("\n%s\n", stacktrace)
|
||||
ctx.Log(iris.DevMode, logMessage)
|
||||
|
||||
ctx.StopExecution()
|
||||
ctx.EmitError(iris.StatusInternalServerError)
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user