common

package
v0.205.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2021 License: Apache-2.0 Imports: 27 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrHandlerTimeout = errors.New("http: Handler timeout")

ErrHandlerTimeout is returned on ResponseWriter Write calls in handlers which have timed out.

Functions

func AddTraceIDToContext

func AddTraceIDToContext(ctx context.Context, id uuid.UUID, wasProvided bool) context.Context

func CheckContextTimeout

func CheckContextTimeout(ctx context.Context, message string, cause error) error

func CoreRequestContextMiddleware

func CoreRequestContextMiddleware(next http.Handler) http.Handler

func CreateDownstreamError

func CreateDownstreamError(ctx context.Context, kind Kind, response *http.Response, body []byte, cause error) error

func CreateError

func CreateError(ctx context.Context, kind Kind, message string, cause error) error

func FindConfigFilename

func FindConfigFilename(cfgDir, prefix string) string

func GetLogEntryFromContext deprecated

func GetLogEntryFromContext(ctx context.Context) *logrus.Entry

Deprecated: Use log.GetLogger.

func GetLoggerFromContext deprecated

func GetLoggerFromContext(ctx context.Context) *logrus.Logger

Deprecated: Use log.GetLogger.

func GetTraceIDFromContext

func GetTraceIDFromContext(ctx context.Context) uuid.UUID

func HandleError

func HandleError(ctx context.Context, w http.ResponseWriter, kind Kind, message string, cause error, httpErrorMapper func(context.Context, error) *HTTPError)

func LoadAndValidateFromYAMLFileName

func LoadAndValidateFromYAMLFileName(filename string, out validator.Validator) error

func LoggerToContext deprecated

func LoggerToContext(ctx context.Context, logger *logrus.Logger, entry *logrus.Entry) context.Context

Deprecated: Use log.GetLogger.

func NewBool

func NewBool(b bool) *bool

func NewHTTPTestServer added in v0.175.0

func NewHTTPTestServer(handler http.Handler) *httptest.Server

NewHTTPTestServer returns a new httptest.Server with the given handler suitable for use within unit tests. The returned server comes equipped with the following: 1. log.Logger.

func NewLoggingRoundTripper

func NewLoggingRoundTripper(name string, base http.RoundTripper) http.RoundTripper

func NewString

func NewString(s string) *string

func NewUnstartedHTTPTestServer added in v0.175.0

func NewUnstartedHTTPTestServer(handler http.Handler) *httptest.Server

NewHTTPTestServer returns an unstarted httptest.Server with the given handler suitable for use within unit tests. The returned server comes equipped with the following: 1. log.Logger.

func ProvisionRestResult added in v0.69.0

func ProvisionRestResult(ctx context.Context) context.Context

ProvisionRestResult provisions within the context the ability to retrieve the result of a rest request.

func RequestHeaderFromContext

func RequestHeaderFromContext(ctx context.Context) http.Header

RequestHeaderFromContext retrieves the request header from the context.

func RequestHeaderToContext

func RequestHeaderToContext(ctx context.Context, header http.Header) context.Context

RequestHeaderToContext creates a new context containing the request header.

func RespHeaderAndStatusFromContext

func RespHeaderAndStatusFromContext(ctx context.Context) (header http.Header, status int)

RespHeaderAndStatusFromContext retrieves response header and status from the context.

func RespHeaderAndStatusToContext

func RespHeaderAndStatusToContext(ctx context.Context, header http.Header, status int) context.Context

RespHeaderAndStatusToContext creates a new context containing the response header and status.

func Timeout

func Timeout(timeout time.Duration, timeoutHandler http.Handler) func(next http.Handler) http.Handler

Timeout is a middleware that cancels ctx after a given timeout or call a special handler on timeout.

func TimeoutHandler

func TimeoutHandler(h http.Handler, dt time.Duration, timeout http.Handler) http.Handler

TimeoutHandler returns a Handler that runs h with the given time limit.

The new Handler calls h.ServeHTTP to handle each request, but if a call runs for longer than its time limit, the handler calls the error handler which can then return a HTTP result however is deems correct. After such a timeout, writes by h to its ResponseWriter will return ErrHandlerTimeout.

TimeoutHandler supports the Pusher interface but does not support the Hijacker or Flusher interfaces.

func TraceabilityMiddleware

func TraceabilityMiddleware(next http.Handler) http.Handler

Injects a traceId UUID into the request context.

func TryGetTraceIDFromContext

func TryGetTraceIDFromContext(ctx context.Context) (uuid.UUID, bool)

func UpdateResponseStatus

func UpdateResponseStatus(ctx context.Context, status int) error

func WrappedError added in v0.31.0

func WrappedError(err error, fields ...KV) error

Types

type Callback

type Callback struct {
	DownstreamTimeout time.Duration
	RouterBasePath    string
	UpstreamConfig    validator.Validator
	MapErrorFunc      func(ctx context.Context, err error) *HTTPError // MapErrorFunc may be left nil to use default behaviour.
	AddMiddlewareFunc func(ctx context.Context, r chi.Router)         // AddMiddlewareFunc may be left nil to use default behaviour.
}

func DefaultCallback

func DefaultCallback() Callback

func NewCallback added in v0.175.0

func NewCallback(
	config *config.GenCodeConfig,
	downstreamTimeOut time.Duration,
	mapError func(ctx context.Context, err error) *HTTPError,
) Callback

NewCallback is deprecated, prefer NewCallbackV2.

func NewCallbackV2 added in v0.175.0

func NewCallbackV2(
	config *config.GenCodeConfig,
	downstreamTimeOut time.Duration,
	mapError func(ctx context.Context, err error) *HTTPError,
	addMiddleware func(ctx context.Context, r chi.Router),
) Callback

func (Callback) AddMiddleware

func (g Callback) AddMiddleware(ctx context.Context, r chi.Router)

func (Callback) BasePath

func (g Callback) BasePath() string

func (Callback) Config

func (g Callback) Config() interface{}

func (Callback) DownstreamTimeoutContext

func (g Callback) DownstreamTimeoutContext(ctx context.Context) (context.Context, context.CancelFunc)

func (Callback) HandleError

func (g Callback) HandleError(ctx context.Context, w http.ResponseWriter, kind Kind, message string, cause error)

func (Callback) MapError added in v0.58.0

func (g Callback) MapError(ctx context.Context, err error) *HTTPError

MapError maps an error to an HTTPError in instances where custom error mapping is required. Return nil to perform default error mapping; defined as: 1. CustomError.HTTPError if the original error is a CustomError, otherwise 2. common.MapError.

type Config

type Config struct{}

func (Config) Validate

func (c Config) Validate() error

type CustomError added in v0.5.0

type CustomError map[string]string

func (CustomError) Error added in v0.5.0

func (e CustomError) Error() string

func (CustomError) HTTPError added in v0.5.0

func (e CustomError) HTTPError(ctx context.Context) *HTTPError

type DownstreamError

type DownstreamError struct {
	Kind     Kind
	Response *http.Response
	Body     []byte
	Cause    error
}

func (*DownstreamError) Error

func (e *DownstreamError) Error() string

func (*DownstreamError) ErrorKind

func (e *DownstreamError) ErrorKind() Kind

func (*DownstreamError) Unwrap

func (e *DownstreamError) Unwrap() error

type ErrorKinder

type ErrorKinder interface {
	ErrorKind() Kind
}

type HTTPError

type HTTPError struct {
	HTTPCode    int    `json:"-"`
	Code        string `json:"code,omitempty"`
	Description string `json:"description,omitempty"`
	// contains filtered or unexported fields
}

func MapError added in v0.5.0

func MapError(ctx context.Context, err error) HTTPError

func (*HTTPError) AddField added in v0.31.0

func (httpError *HTTPError) AddField(key string, val interface{})

func (*HTTPError) WriteError

func (httpError *HTTPError) WriteError(ctx context.Context, w http.ResponseWriter)

type KV added in v0.31.0

type KV struct {
	K string
	V interface{}
}

type Kind

type Kind int
const (
	UnknownError Kind = iota
	BadRequestError
	InternalError
	UnauthorizedError
	DownstreamUnavailableError
	DownstreamTimeoutError
	DownstreamUnauthorizedError       // 401 from downstream
	DownstreamUnexpectedResponseError // unexpected response from downstream
	DownstreamResponseError           // application-leve error response from downstream
)

func (Kind) String

func (k Kind) String() string

type MockRoundTripper

type MockRoundTripper struct {
	mock.Mock
}

func (*MockRoundTripper) RoundTrip

func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type RestResult added in v0.69.0

type RestResult struct {
	StatusCode int
	Headers    map[string][]string
	Body       []byte
}

func GetRestResult added in v0.69.0

func GetRestResult(ctx context.Context) *RestResult

GetRestResult gets the result of the most recent rest request. The context must be provisioned prior to the request taking place with a call to ProvisionRestResult.

type RestResultContextKey added in v0.69.0

type RestResultContextKey struct{}

type ServerError

type ServerError struct {
	Kind    Kind
	Message string
	Cause   error
}

func (*ServerError) Error

func (e *ServerError) Error() string

func (*ServerError) ErrorKind

func (e *ServerError) ErrorKind() Kind

func (*ServerError) Unwrap

func (e *ServerError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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