21 lines
752 B
Go
21 lines
752 B
Go
|
|
package httputil
|
||
|
|
|
||
|
|
import "net/http"
|
||
|
|
|
||
|
|
var _ http.Handler = HandlerFunc(nil)
|
||
|
|
|
||
|
|
// HandlerFunc is an http.Handler that returns an error.
|
||
|
|
// On non-nil error the error is mapped to the appropriate HTTP response via [Error].
|
||
|
|
// Use for manual handlers that need path parameters or custom status codes.
|
||
|
|
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
|
||
|
|
|
||
|
|
// ServeHTTP implements http.Handler.
|
||
|
|
// Errors are written as standardized JSON without logging — no logger is in
|
||
|
|
// scope for a bare function type. Use [Handle], [HandleNoBody], or
|
||
|
|
// [HandleEmpty] for centralized logging, or call [Error] explicitly.
|
||
|
|
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if err := h(w, r); err != nil {
|
||
|
|
writeError(w, err)
|
||
|
|
}
|
||
|
|
}
|