PanicRecoveryError returns a new ErrPanicRecovery error.
(ctx *context.Context, err any)
| 32 | |
| 33 | // PanicRecoveryError returns a new ErrPanicRecovery error. |
| 34 | func PanicRecoveryError(ctx *context.Context, err any) *context.ErrPanicRecovery { |
| 35 | if recoveryErr, ok := ctx.IsRecovered(); ok { |
| 36 | // If registered before any other recovery middleware, get its error. |
| 37 | // Because of defer this will be executed last, after the recovery middleware in this case. |
| 38 | return recoveryErr |
| 39 | } |
| 40 | |
| 41 | if err == nil { |
| 42 | return nil |
| 43 | } else if ctx.IsStopped() { |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | var callers []string |
| 48 | for i := 2; ; /* 1 for New() 2 for NewPanicRecoveryError */ i++ { |
| 49 | _, file, line, got := runtime.Caller(i) |
| 50 | if !got { |
| 51 | break |
| 52 | } |
| 53 | |
| 54 | callers = append(callers, fmt.Sprintf("%s:%d", file, line)) |
| 55 | } |
| 56 | |
| 57 | // get the list of registered handlers and the |
| 58 | // handler which panic derived from. |
| 59 | handlers := ctx.Handlers() |
| 60 | handlersFileLines := make([]string, 0, len(handlers)) |
| 61 | currentHandlerIndex := ctx.HandlerIndex(-1) |
| 62 | currentHandlerFileLine := "???" |
| 63 | for i, h := range ctx.Handlers() { |
| 64 | file, line := context.HandlerFileLine(h) |
| 65 | fileline := fmt.Sprintf("%s:%d", file, line) |
| 66 | handlersFileLines = append(handlersFileLines, fileline) |
| 67 | if i == currentHandlerIndex { |
| 68 | currentHandlerFileLine = fileline |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // see accesslog.wasRecovered too. |
| 73 | recoveryErr := &context.ErrPanicRecovery{ |
| 74 | Cause: err, |
| 75 | Callers: callers, |
| 76 | Stack: debug.Stack(), |
| 77 | RegisteredHandlers: handlersFileLines, |
| 78 | CurrentHandlerFileLine: currentHandlerFileLine, |
| 79 | CurrentHandlerName: ctx.HandlerName(), |
| 80 | Request: getRequestLogs(ctx), |
| 81 | } |
| 82 | |
| 83 | return recoveryErr |
| 84 | } |
| 85 | |
| 86 | func getRequestLogs(ctx *context.Context) string { |
| 87 | rawReq, _ := httputil.DumpRequest(ctx.Request(), false) |
no test coverage detected
searching dependent graphs…