MCPcopy Index your code
hub / github.com/coder/coder / WithRecover

Function WithRecover

codersdk/toolsdk/toolsdk.go:251–267  ·  view source on GitHub ↗

WithRecover wraps a HandlerFunc to recover from panics and return an error.

(h GenericHandlerFunc)

Source from the content-addressed store, hash-verified

249
250// WithRecover wraps a HandlerFunc to recover from panics and return an error.
251func 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.

Callers 1

TestWithRecoveryFunction · 0.92

Calls 2

IsDevFunction · 0.92
ErrorfMethod · 0.45

Tested by 1

TestWithRecoveryFunction · 0.74