Documentation ¶
Index ¶
- func ErrHandler(ctx context.Context, statusCode int, err error)
- func NewError(err error, params ...ErrorParam) error
- func NewJSONHandler(fn func(http.ResponseWriter, *http.Request) error, statusFn StatusMapper, ...) http.Handler
- func ParseBearerTokenHeader(req *http.Request) (string, error)
- func StatusCodeMapper(err error) int
- func WriteJSONResponse(w http.ResponseWriter, obj interface{}, status int)
- type ErrorHandler
- type ErrorParam
- type StatusMapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrHandler ¶
ErrHandler is an ErrorHandler that creates a log in the provided context's svc1log logger when an error is received. The log output is printed at the ERROR level if the status code is >= 500; otherwise, it is printed at INFO level. This preserves request-scoped logging configuration added by wrouter.
func NewError ¶
func NewError(err error, params ...ErrorParam) error
func NewJSONHandler ¶
func NewJSONHandler(fn func(http.ResponseWriter, *http.Request) error, statusFn StatusMapper, errorFn ErrorHandler) http.Handler
NewJSONHandler returns a http.Handler which will convert a returned error into a corresponding status code, and handle the error according to the provided ErrorHandler. The provided 'fn' function is not expected to write a response in the http.ResponseWriter if it returns a non-nil error. If a non-nil error is returned, the mapped status code from the provided StatusMapper will be returned.
func ParseBearerTokenHeader ¶
ParseBearerTokenHeader parses a bearer token value out of the Authorization header. It expects a header with a key of 'Authorization' and a value of 'bearer {token}'. ParseBearerTokenHeader will return the token value, or an error if the Authorization header is missing, an empty string, or is not in the format expected.
func StatusCodeMapper ¶
StatusCodeMapper maps a provided error to an HTTP status code. If the provided error contains a status code added using the StatusCode ErrorParam, returns that status code; otherwise, returns http.StatusInternalServerError.
func WriteJSONResponse ¶
func WriteJSONResponse(w http.ResponseWriter, obj interface{}, status int)
WriteJSONResponse marshals the provided object to JSON using a JSON encoder with SetEscapeHTML(false) and writes the resulting JSON as a JSON response to the provided http.ResponseWriter with the provided status code. If marshaling the provided object as JSON results in an error, writes a 500 response with the text content of the error.
Types ¶
type ErrorParam ¶
type ErrorParam interface {
// contains filtered or unexported methods
}
func StatusCode ¶
func StatusCode(code int) ErrorParam