panicrecovery

package
v0.0.0-...-8f1e8fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2024 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PanicRecoveryMiddleware

func PanicRecoveryMiddleware() httpmiddlewareutils.Middleware

Middleware that recovers from a panic by responding to the request with:

  • http.StatusInternalServerError
  • Response body of "500 Internal Server Error"

Will also record an error within the OTEL span with the stack and recovered value from the panic.

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	panic("a fatal error occurred")
})
middleware := PanicRecoveryMiddleware()
server := http.Server{
	Handler: middleware(mux),
}
// error handling omitted for brevity
defer server.Close()
_ = server.ListenAndServe()
Output:

func PanicRecoveryMiddlewareFunc

func PanicRecoveryMiddlewareFunc(recoverAction func(w http.ResponseWriter, r *http.Request, recoverValue any, stack []byte)) httpmiddlewareutils.Middleware

Middleware that allows the developer to specify a recovery action to take when recovering from a panic

Example
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	panic("a fatal error occurred")
})
middleware := PanicRecoveryMiddlewareFunc(func(w http.ResponseWriter, r *http.Request, recoverValue any, stack []byte) {
	log.Printf("something has broken: %s", recoverValue)
})
server := http.Server{
	Handler: middleware(mux),
}
// error handling omitted for brevity
defer server.Close()
_ = server.ListenAndServe()
Output:

something has broken: a fatal error occurred

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL