Documentation
¶
Overview ¶
The jsonhttp package provides general functions for returning JSON responses to HTTP requests. It is agnostic about the specific form of any returned errors.
Index ¶
- func HandleErrors(errToResp ErrorToResponse) func(handle ErrorHandler) http.Handler
- func HandleJSON(errToResp ErrorToResponse) func(handle JSONHandler) http.Handler
- func WriteError(errToResp ErrorToResponse) func(w http.ResponseWriter, err error)
- func WriteJSON(w http.ResponseWriter, code int, val interface{}) error
- type ErrorHandler
- type ErrorToResponse
- type JSONHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HandleErrors ¶
func HandleErrors(errToResp ErrorToResponse) func(handle ErrorHandler) http.Handler
HandleErrors returns a function that can be used to convert an ErrorHandler into an http.Handler. The given errToResp parameter is used to convert any non-nil error returned by handle to the response in the HTTP body.
func HandleJSON ¶
func HandleJSON(errToResp ErrorToResponse) func(handle JSONHandler) http.Handler
HandleJSON returns a function that can be used to convert an JSONHandler into an http.Handler. The given errToResp parameter is used to convert any non-nil error returned by handle to the response in the HTTP body If it returns a nil value, the original error is returned as a JSON string.
func WriteError ¶
func WriteError(errToResp ErrorToResponse) func(w http.ResponseWriter, err error)
WriteError returns a function that can be used to write an error to a ResponseWriter and set the HTTP status code. The errToResp parameter is used to determine the actual error value and status to write.
Types ¶
type ErrorHandler ¶
type ErrorHandler func(http.ResponseWriter, *http.Request) error
ErrorHandler is like http.Handler except it returns an error which may be returned as the error body of the response. An ErrorHandler function should not itself write to the ResponseWriter if it returns an error.
type ErrorToResponse ¶
ErrorToResponse represents a function that can convert a Go error into a form that can be returned as a JSON body from an HTTP request. The httpStatus value reports the desired HTTP status.