-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathhandler.go
More file actions
25 lines (22 loc) · 826 Bytes
/
handler.go
File metadata and controls
25 lines (22 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package httputil
import (
"net/http"
)
// WrapHandlerFunc wraps a function returning an error into an HTTP handler func that returns a 200 OK with empty
// contents upon success, and sends an error formatted according to `WriteError` to the client otherwise.
func WrapHandlerFunc(handlerFn func(req *http.Request) error) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
err := handlerFn(req)
if err != nil {
WriteError(w, err)
return
}
})
}
// NotImplementedHandler returns an HTTP Handler func that returns 501 Not Implemented with a custom error message.
func NotImplementedHandler(errMsg string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Error(errMsg)
http.Error(w, errMsg, http.StatusNotImplemented)
})
}