Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/ fatal Public

πŸš‘ A handler that recovers from panics

License

Notifications You must be signed in to change notification settings

gowww/fatal

Repository files navigation

Package fatal provides a handler that recovers from panics.

Installing

  1. Get package:

    go get -u github.com/gowww/fatal
  2. Import it in your code:

    import "github.com/gowww/fatal"

Usage

To wrap an http.Handler, use Handle:

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	panic("error")
})
http.ListenAndServe(":8080", fatal.Handle(mux, nil))

To wrap an http.HandlerFunc, use HandleFunc:

http.Handle("/", fatal.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
	panic("error")
}, nil))
http.ListenAndServe(":8080", nil)

Custom "error" handler

When your code panics, the response status is set to 500 and an empty body is sent by default.

But you can set your own error handler and retrive the error value with Error.
In this case, it's up to you to set the response status code (normally 500):

http.ListenAndServe(":8080", fatal.Handle(mux, &fatal.Options{
	RecoverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, "error: %v", fatal.Error(r))
	}),
}))

About

πŸš‘ A handler that recovers from panics

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /