-
Notifications
You must be signed in to change notification settings - Fork 1
/
no_content_wrapper.go
31 lines (27 loc) · 1.04 KB
/
no_content_wrapper.go
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
26
27
28
29
30
31
package resthelper
import (
"net/http"
)
type NoResponseHandler func(*http.Request) *HttpError
// NoContentWrapper allows us to ensure at compile time that a route handler will always return either a 204 No Content response or an error code
func NoContentWrapper(toWrap NoResponseHandler) func(http.ResponseWriter, *http.Request) {
return NoContentWrapperWithHooks([]PreRequestHook{}, toWrap, []PostResponseHook{})
}
func NoContentWrapperWithHooks(preRequestHooks []PreRequestHook, toWrap NoResponseHandler, postResponseHooks []PostResponseHook) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer recoverToErrorResponse(w, postResponseHooks)
writeCommonHeaders(w)
err := callPreRequestHooks(preRequestHooks, r)
if err != nil {
respondWithError(w, err, postResponseHooks)
return
}
err = toWrap(r)
if err != nil {
respondWithError(w, err, postResponseHooks)
} else {
w.WriteHeader(http.StatusNoContent)
callPostResponseHooks(postResponseHooks, nil, http.StatusNoContent)
}
}
}