✨ Implement Handler and HandlerFunc, add comprehensive tests
This commit is contained in:
parent
aa7dfb532f
commit
a136616088
5 changed files with 138 additions and 5 deletions
16
helper.go
16
helper.go
|
|
@ -11,20 +11,26 @@ func NewHelper(errH ErrorHandler) *Helper {
|
|||
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
|
||||
|
||||
// Helper provides methods to make easier, more idiomatic [http.Handler]s.
|
||||
type Helper struct{
|
||||
type Helper struct {
|
||||
errHandler ErrorHandler
|
||||
}
|
||||
|
||||
// Handler returns an [http.Handler] for the provided [Handler].
|
||||
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
||||
func (help *Helper) Handler(hnd Handler) http.Handler {
|
||||
// TODO implement this
|
||||
panic("unimplemented")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := hnd.ServeHTTP(w, r); err != nil {
|
||||
help.errHandler(w, r, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Handler returns an [http.Handler] for the provided [HandlerFunc].
|
||||
// If hnd returns an error, an appropriate error response is written using the ErrorHandler.
|
||||
func (help *Helper) HandlerFunc(hnd HandlerFunc) http.Handler {
|
||||
// TODO implement this
|
||||
panic("unimplemented")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := hnd(w, r); err != nil {
|
||||
help.errHandler(w, r, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue