rest

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrHandler

func ErrHandler(ctx context.Context, statusCode int, err error)

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

func ParseBearerTokenHeader(req *http.Request) (string, error)

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

func StatusCodeMapper(err error) int

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 ErrorHandler

type ErrorHandler func(ctx context.Context, statusCode int, err error)

type ErrorParam

type ErrorParam interface {
	// contains filtered or unexported methods
}

func StatusCode

func StatusCode(code int) ErrorParam

type StatusMapper

type StatusMapper func(err error) int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL