1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00
authboss/defaults/error_handler.go
Aaron L 27010d9fe4 Abstract logger and error handling
- Replace the old logging mechanisms with a leveled one. This is
  important as authboss needs to start saying a lot more about what's
  happening in the Info log, which will end up like Debug but that's
  okay.
- Replace the error handling mechanisms with something different. This
  allows people to define their own error handlers.
2018-02-02 12:11:47 -08:00

40 lines
872 B
Go

package defaults
import (
"fmt"
"io"
"net/http"
)
// ErrorHandler wraps http handlers with errors with itself
// to provide error handling.
//
// The pieces provided to this struct must be thread-safe
// since they will be handed to many pointers to themselves.
type ErrorHandler struct {
LogWriter io.Writer
}
// Wrap an http handler with an error
func (e ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request) error) http.Handler {
return errorHandler{
Handler: handler,
LogWriter: e.LogWriter,
}
}
type errorHandler struct {
Handler func(w http.ResponseWriter, r *http.Request) error
LogWriter io.Writer
}
// ServeHTTP handles errors
func (e errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := e.Handler(w, r)
if err == nil {
return
}
fmt.Fprintf(e.LogWriter, "error at %s: %+v", r.URL.String(), err)
}