WithRecover wraps a HandlerFunc to recover from panics and return an error.
(h GenericHandlerFunc)
| 249 | |
| 250 | // WithRecover wraps a HandlerFunc to recover from panics and return an error. |
| 251 | func WithRecover(h GenericHandlerFunc) GenericHandlerFunc { |
| 252 | return func(ctx context.Context, deps Deps, args json.RawMessage) (ret json.RawMessage, err error) { |
| 253 | defer func() { |
| 254 | if r := recover(); r != nil { |
| 255 | if buildinfo.IsDev() { |
| 256 | // Capture stack trace in dev builds |
| 257 | stack := debug.Stack() |
| 258 | err = xerrors.Errorf("tool handler panic: %v\nstack trace:\n%s", r, stack) |
| 259 | } else { |
| 260 | // Simple error message in production builds |
| 261 | err = xerrors.Errorf("tool handler panic: %v", r) |
| 262 | } |
| 263 | } |
| 264 | }() |
| 265 | return h(ctx, deps, args) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // WithCleanContext wraps a HandlerFunc to provide it with a new context. |
| 270 | // This ensures that no data is passed using context.Value. |