util

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: Apache-2.0, BSD-3-Clause, MIT Imports: 15 Imported by: 22

Documentation

Overview

Package util contains web utils for APIs, clients and error handling

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFoundInStorage error returned from storage when entity is not found
	ErrNotFoundInStorage = errors.New("not found")

	// ErrAlreadyExistsInStorage error returned from storage when entity has conflicting fields
	ErrAlreadyExistsInStorage = errors.New("unique constraint violation")

	// ErrConcurrentResourceModification error returned when concurrent resource updates are happening
	ErrConcurrentResourceModification = errors.New("another resource update happened concurrently. Please reattempt the update")
)

Functions

func BodyToBytes

func BodyToBytes(closer io.ReadCloser) ([]byte, error)

BodyToBytes of the request inside given struct

func BodyToObject

func BodyToObject(closer io.ReadCloser, object interface{}) error

BodyToObject of the request inside given struct

func BytesToObject

func BytesToObject(bytes []byte, object interface{}) error

BytesToObject converts the provided bytes to object and validates it

func HandleInterrupts

func HandleInterrupts(ctx context.Context, cancel context.CancelFunc)

HandleInterrupts handles process signal interrupts

func HandleResponseError

func HandleResponseError(response *http.Response) error

HandleResponseError builds at HttpErrorResponse from the given response.

func HandleStorageError

func HandleStorageError(err error, entityName string) error

HandleStorageError handles storage errors by converting them to relevant HTTPErrors

func HasRFC3986ReservedSymbols

func HasRFC3986ReservedSymbols(input string) bool

HasRFC3986ReservedSymbols returns true if input contains any reserver characters as defined in RFC3986 section oidc_authn.oidc_authn

func NewJSONResponse

func NewJSONResponse(code int, value interface{}) (*web.Response, error)

NewJSONResponse turns plain object into a byte array representing JSON value and wraps it in web.Response

func RequestBodyToBytes

func RequestBodyToBytes(request *http.Request) ([]byte, error)

RequestBodyToBytes reads the request body and returns []byte with its content or an error if the media type is incorrect or if the body is not a valid JSON

func SendRequest

func SendRequest(ctx context.Context, doRequest DoRequestFunc, method, url string, params map[string]string, body interface{}) (*http.Response, error)

SendRequest sends a request to the specified client and the provided URL with the specified parameters and body.

func SendRequestWithHeaders added in v0.1.11

func SendRequestWithHeaders(ctx context.Context, doRequest DoRequestFunc, method, url string, params map[string]string, body interface{}, headers map[string]string) (*http.Response, error)

SendRequestWithHeaders sends a request to the specified client and the provided URL with the specified parameters, body and headers.

func StartInWaitGroup added in v0.3.2

func StartInWaitGroup(f func(), group *sync.WaitGroup)

func StartInWaitGroupWithContext added in v0.3.2

func StartInWaitGroupWithContext(ctx context.Context, f func(ctx context.Context), group *sync.WaitGroup)

func ToRFCFormat

func ToRFCFormat(timestamp time.Time) string

ToRFCFormat converts a time.Time timestamp to RFC3339 format

func WriteError

func WriteError(err error, writer http.ResponseWriter)

WriteError sends a JSON containing the error to the response writer

func WriteJSON

func WriteJSON(writer http.ResponseWriter, code int, value interface{}) error

WriteJSON writes a JSON value and sets the specified HTTP Status code

Types

type DoRequestFunc

type DoRequestFunc func(request *http.Request) (*http.Response, error)

DoRequestFunc is an alias for any function that takes an http request and returns a response and error

type EmptyResponseBody added in v0.1.2

type EmptyResponseBody struct{}

EmptyResponseBody represents an empty response body value

type ErrBadRequestStorage added in v0.1.6

type ErrBadRequestStorage struct {
	Cause error
}

ErrBadRequestStorage represents a storage error that should be translated to http.StatusBadRequest

func (*ErrBadRequestStorage) Error added in v0.3.0

func (e *ErrBadRequestStorage) Error() string

type HTTPError

type HTTPError struct {
	ErrorType   string `json:"error,omitempty"`
	Description string `json:"description,omitempty"`
	StatusCode  int    `json:"-"`
}

HTTPError is an error type that provides error details that Service Manager error handlers would propagate to the client

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error HTTPError should implement error

type InputValidator

type InputValidator interface {
	Validate() error
}

InputValidator should be implemented by types that need input validation check. For a reference refer to pkg/types

type UnsupportedQueryError added in v0.1.9

type UnsupportedQueryError struct {
	Message string
}

UnsupportedQueryError is an error to show that the provided query cannot be executed

func (*UnsupportedQueryError) Error added in v0.1.9

func (uq *UnsupportedQueryError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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