Skip to main content
Glama
goroutine_helper.go1.04 kB
package tui import ( "fmt" "runtime/debug" ) // SafeGoroutine executes a function in a goroutine with panic recovery and error context func SafeGoroutine(operation string, fn func() error, onError func(error)) { go func() { defer func() { if r := recover(); r != nil { stack := debug.Stack() err := fmt.Errorf("panic in goroutine %s: %v\nStack trace:\n%s", operation, r, string(stack)) if onError != nil { onError(err) } } }() if err := fn(); err != nil && onError != nil { onError(fmt.Errorf("error in %s: %w", operation, err)) } }() } // SafeGoroutineNoError executes a function in a goroutine with panic recovery (for functions that don't return errors) func SafeGoroutineNoError(operation string, fn func(), onPanic func(error)) { go func() { defer func() { if r := recover(); r != nil { stack := debug.Stack() err := fmt.Errorf("panic in goroutine %s: %v\nStack trace:\n%s", operation, r, string(stack)) if onPanic != nil { onPanic(err) } } }() fn() }() }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/standardbeagle/brummer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server