var ReallyCrash … var PanicHandlers … // HandleCrash simply catches a crash and logs an error. Meant to be called via // defer. Additional context-specific handlers can be provided, and will be // called in case of panic. HandleCrash actually crashes, after calling the // handlers and logging the panic message. // // E.g., you can provide one or more additional handlers for something like shutting down go routines gracefully. // // TODO(pohly): logcheck:context // HandleCrashWithContext should be used instead of HandleCrash in code which supports contextual logging. func HandleCrash(additionalHandlers ...func(interface{ … } // HandleCrashWithContext simply catches a crash and logs an error. Meant to be called via // defer. Additional context-specific handlers can be provided, and will be // called in case of panic. HandleCrash actually crashes, after calling the // handlers and logging the panic message. // // E.g., you can provide one or more additional handlers for something like shutting down go routines gracefully. // // The context is used to determine how to log. func HandleCrashWithContext(ctx context.Context, additionalHandlers ...func(context.Context, interface{ … } // handleCrash is the common implementation of HandleCrash and HandleCrash. // Having those call a common implementation ensures that the stack depth // is the same regardless through which path the handlers get invoked. func handleCrash(ctx context.Context, r any, additionalHandlers ...func(context.Context, interface{ … } // logPanic logs the caller tree when a panic occurs (except in the special case of http.ErrAbortHandler). func logPanic(ctx context.Context, r interface{ … } var ErrorHandlers … type ErrorHandler … // HandlerError is a method to invoke when a non-user facing piece of code cannot // return an error and needs to indicate it has been ignored. Invoking this method // is preferable to logging the error - the default behavior is to log but the // errors may be sent to a remote server for analysis. // // TODO(pohly): logcheck:context // HandleErrorWithContext should be used instead of HandleError in code which supports contextual logging. func HandleError(err error) { … } // HandlerErrorWithContext is a method to invoke when a non-user facing piece of code cannot // return an error and needs to indicate it has been ignored. Invoking this method // is preferable to logging the error - the default behavior is to log but the // errors may be sent to a remote server for analysis. The context is used to // determine how to log the error. // // If contextual logging is enabled, the default log output is equivalent to // // logr.FromContext(ctx).WithName("UnhandledError").Error(err, msg, keysAndValues...) // // Without contextual logging, it is equivalent to: // // klog.ErrorS(err, msg, keysAndValues...) // // In contrast to HandleError, passing nil for the error is still going to // trigger a log entry. Don't construct a new error or wrap an error // with fmt.Errorf. Instead, add additional information via the mssage // and key/value pairs. // // This variant should be used instead of HandleError because it supports // structured, contextual logging. func HandleErrorWithContext(ctx context.Context, err error, msg string, keysAndValues ...interface{ … } // handleError is the common implementation of HandleError and HandleErrorWithContext. // Using this common implementation ensures that the stack depth // is the same regardless through which path the handlers get invoked. func handleError(ctx context.Context, err error, msg string, keysAndValues ...interface{ … } // logError prints an error with the call stack of the location it was reported. // It expects to be called as <caller> -> HandleError[WithContext] -> handleError -> logError. func logError(ctx context.Context, err error, msg string, keysAndValues ...interface{ … } type rudimentaryErrorBackoff … // OnError will block if it is called more often than the embedded period time. // This will prevent overly tight hot error loops. func (r *rudimentaryErrorBackoff) OnError() { … } // GetCaller returns the caller of the function that calls it. func GetCaller() string { … } // RecoverFromPanic replaces the specified error with an error containing the // original error, and the call tree when a panic occurs. This enables error // handlers to handle errors and panics the same way. func RecoverFromPanic(err *error) { … } // Must panics on non-nil errors. Useful to handling programmer level errors. func Must(err error) { … }