panicRecover returns a default panic-recover handler.
()
| 251 | |
| 252 | // panicRecover returns a default panic-recover handler. |
| 253 | func panicRecover() *hook.Handler[*core.RequestEvent] { |
| 254 | return &hook.Handler[*core.RequestEvent]{ |
| 255 | Id: DefaultPanicRecoverMiddlewareId, |
| 256 | Priority: DefaultPanicRecoverMiddlewarePriority, |
| 257 | Func: func(e *core.RequestEvent) (err error) { |
| 258 | // panic-recover |
| 259 | defer func() { |
| 260 | recoverResult := recover() |
| 261 | if recoverResult == nil { |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | recoverErr, ok := recoverResult.(error) |
| 266 | if !ok { |
| 267 | recoverErr = fmt.Errorf("%v", recoverResult) |
| 268 | } else if errors.Is(recoverErr, http.ErrAbortHandler) { |
| 269 | // don't recover ErrAbortHandler so the response to the client can be aborted |
| 270 | panic(recoverResult) |
| 271 | } |
| 272 | |
| 273 | stack := make([]byte, 2<<10) // 2 KB |
| 274 | length := runtime.Stack(stack, true) |
| 275 | err = e.InternalServerError("", fmt.Errorf("[PANIC RECOVER] %w %s", recoverErr, stack[:length])) |
| 276 | }() |
| 277 | |
| 278 | err = e.Next() |
| 279 | |
| 280 | return err |
| 281 | }, |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // securityHeaders middleware adds common security headers to the response. |
| 286 | // |
no test coverage detected
searching dependent graphs…