Documentation ¶
Index ¶
- func ErrHandler(ctx context.Context, statusCode int, err error)
- func NewJSONHandler(fn func(http.ResponseWriter, *http.Request) error, statusFn StatusMapper, ...) http.Handler
- func ParseBearerTokenHeader(req *http.Request) (string, error)
- func SecretStringEqual(a, b string) bool
- func StatusCodeMapper(err error) int
- func WriteJSONResponse(w http.ResponseWriter, obj interface{}, status int)
- type ErrorHandler
- 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 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 SecretStringEqual ¶ added in v2.31.0
SecretStringEqual will compare two strings and return true if they are equal. The time taken for the comparison does not depend on the string contents.
func StatusCodeMapper ¶
StatusCodeMapper maps a provided error to an HTTP status code. If the error's RootCause is a conjure error, the status mapping to the errorCode field is used. If the provided error is a contains the legacy httpStatusCode parameter, that value is used. Otherwise, returns http.StatusInternalServerError (500).
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.