rest

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 8 Imported by: 17

Documentation

Overview

Package rest provides utilities for REST/JSON servers

Deprecated: prefer github.com/palantir/conjure-go-runtime/v2/conjure-go-server/httpserver

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrHandler deprecated

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.

Deprecated: Prefer server utilities in github.com/palantir/conjure-go-runtime/v2/conjure-go-server/httpserver.

func NewError deprecated

func NewError(err error, params ...ErrorParam) error

NewError creates an error which may include an HTTP status code using the StatusCode parameter.

Deprecated: Prefer conjure errors provided by github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/errors.

func NewJSONHandler deprecated

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.

Deprecated: Prefer server utilities in github.com/palantir/conjure-go-runtime/conjure-go-server/httpserver.

func ParseBearerTokenHeader deprecated

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.

Deprecated: Prefer server utilities in github.com/palantir/conjure-go-runtime/v2/conjure-go-server/httpserver.

func StatusCodeMapper deprecated

func StatusCodeMapper(err error) int

StatusCodeMapper maps a provided error to an HTTP status code. If the provided error contains a non-zero status code added using the StatusCode ErrorParam, returns that status code; otherwise, returns http.StatusInternalServerError.

Deprecated: Prefer server utilities in github.com/palantir/conjure-go-runtime/v2/conjure-go-server/httpserver.

func WriteJSONResponse deprecated

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.

Deprecated: Prefer server utilities in github.com/palantir/conjure-go-runtime/v2/conjure-go-server/httpserver.

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