Handlers
httpx.HandlerFunc (func(r *http.Request) (Response, error))
is a custom handler type that allows you to define handlers that return a structured response and an error.
It returns an httpx.Response
and an error. The httpx.Wrap
helper converts it to a standard http.HandlerFunc
that writes the response and handles errors for you (see Errors handling).
handler.go
func (h *NoteHandler) CreateNote() http.HandlerFunc {
return httpx.Wrap(func(r *http.Request) (httpx.Response, error) {
// business logic
return httpx.JSON.Created(nil), nil
})
}
You can always use stdlib http.HandlerFunc
if you prefer, and use httpx helper for response handling, but you loose error handling.
Last updated on