mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-28 08:58:38 +02:00
27010d9fe4
- 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.
40 lines
872 B
Go
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)
|
|
}
|