1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

recover, accesslog middlewares: give more information to the stored error, accesslog: make use of that information and introduce panic log levels

relative to: https://github.com/kataras/iris/issues/1601#issuecomment-690541789
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-10 21:03:34 +03:00
parent facc94b725
commit 2bb04823b0
4 changed files with 131 additions and 48 deletions

View File

@@ -4790,35 +4790,6 @@ func (ctx *Context) IsRecording() (*ResponseRecorder, bool) {
return rr, ok
}
// ErrPanicRecovery may be returned from `Context` actions of a `Handler`
// which recovers from a manual panic.
type ErrPanicRecovery struct {
ErrPrivate
Cause interface{}
Stacktrace string
}
// Error implements the Go standard error type.
func (e ErrPanicRecovery) Error() string {
return fmt.Sprintf("%v\n%s", e.Cause, e.Stacktrace)
}
// ErrPrivate if provided then the error saved in context
// should NOT be visible to the client no matter what.
type ErrPrivate interface {
IrisPrivateError()
}
// IsErrPrivate reports whether the given "err" is a private one.
func IsErrPrivate(err error) bool {
if err == nil {
return false
}
_, ok := err.(ErrPrivate)
return ok
}
// ErrTransactionInterrupt can be used to manually force-complete a Context's transaction
// and log(warn) the wrapped error's message.
// Usage: `... return fmt.Errorf("my custom error message: %w", context.ErrTransactionInterrupt)`.
@@ -5093,19 +5064,68 @@ func (ctx *Context) GetErr() error {
return nil
}
// IsRecovered reports whether this handler has been recovered
// by the Iris recover middleware.
func (ctx *Context) IsRecovered() (ErrPanicRecovery, bool) {
if ctx.GetStatusCode() == 500 {
// Panic error from recovery middleware is private.
if err := ctx.GetErr(); err != nil {
if panicErr, ok := err.(ErrPanicRecovery); ok {
return panicErr, true
}
// ErrPanicRecovery may be returned from `Context` actions of a `Handler`
// which recovers from a manual panic.
type ErrPanicRecovery struct {
ErrPrivate
Cause interface{}
Callers []string // file:line callers.
Stack []byte // the full debug stack.
RegisteredHandlers []string // file:line of all registered handlers.
CurrentHandler string // the handler panic came from.
}
// Error implements the Go standard error type.
func (e *ErrPanicRecovery) Error() string {
if e.Cause != nil {
if err, ok := e.Cause.(error); ok {
return err.Error()
}
}
return ErrPanicRecovery{}, false
return fmt.Sprintf("%v\n%s", e.Cause, strings.Join(e.Callers, "\n"))
}
// Is completes the internal errors.Is interface.
func (e *ErrPanicRecovery) Is(err error) bool {
_, ok := IsErrPanicRecovery(err)
return ok
}
// IsErrPanicRecovery reports whether the given "err" is a type of ErrPanicRecovery.
func IsErrPanicRecovery(err error) (*ErrPanicRecovery, bool) {
if err == nil {
return nil, false
}
v, ok := err.(*ErrPanicRecovery)
return v, ok
}
// ErrPrivate if provided then the error saved in context
// should NOT be visible to the client no matter what.
type ErrPrivate interface {
IrisPrivateError()
}
// IsErrPrivate reports whether the given "err" is a private one.
func IsErrPrivate(err error) bool {
if err == nil {
return false
}
_, ok := err.(ErrPrivate)
return ok
}
// IsRecovered reports whether this handler has been recovered
// by the Iris recover middleware.
func (ctx *Context) IsRecovered() (*ErrPanicRecovery, bool) {
if ctx.GetStatusCode() == 500 {
// Panic error from recovery middleware is private.
return IsErrPanicRecovery(ctx.GetErr())
}
return nil, false
}
const idContextKey = "iris.context.id"