httputils

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParamIDRequired        = model.ValidationError{Err: errors.New("id parameter is required")}
	ErrRequestBodyRequired    = model.ValidationError{Err: errors.New("request body required")}
	ErrRequestBodyJSONInvalid = model.ValidationError{Err: errors.New("request body contains malformed JSON")}
)

Functions

func IsJSONError

func IsJSONError(err error) bool

Types

type DefaultImpl

type DefaultImpl struct {
	// contains filtered or unexported fields
}

func NewDefaultHelper

func NewDefaultHelper(logger logrus.FieldLogger) *DefaultImpl

func (*DefaultImpl) ErrorCode

func (d *DefaultImpl) ErrorCode(r *http.Request, w http.ResponseWriter, rLogger logrus.FieldLogger, err error, code int)

ErrorCode replies to the request with the specified error message as JSON-encoded body and writes corresponding message to the log.

If HTTP code is less than or equal zero, it will be deduced based on the error. If it fails, StatusInternalServerError will be returned without the response body. The error can be of 'multierror.Error' type.

The call writes messages with the debug log level except the case when the code is StatusInternalServerError which is logged as an error.

It does not end the HTTP request; the caller should ensure no further writes are done to w.

func (*DefaultImpl) HandleError

func (d *DefaultImpl) HandleError(r *http.Request, w http.ResponseWriter, err error)

HandleError replies to the request with an appropriate message as JSON-encoded body and writes a corresponding message to the log with debug log level.

Any error of a type not defined in this package or pkg/model, will be treated as an internal server error causing response code 500. Such errors are not sent but only logged with error log level.

func (*DefaultImpl) IDFromRequest

func (*DefaultImpl) IDFromRequest(r *http.Request) (uint, error)

func (*DefaultImpl) Logger

func (d *DefaultImpl) Logger(r *http.Request) logrus.FieldLogger

Logger creates a new logger scoped to the request and enriches it with the known fields.

func (*DefaultImpl) MustJSON

func (*DefaultImpl) MustJSON(_ *http.Request, w http.ResponseWriter, v interface{})

func (*DefaultImpl) WriteError

func (d *DefaultImpl) WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string)

func (*DefaultImpl) WriteInternalServerError

func (d *DefaultImpl) WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string)

func (*DefaultImpl) WriteInvalidMethodError

func (d *DefaultImpl) WriteInvalidMethodError(r *http.Request, w http.ResponseWriter)

func (*DefaultImpl) WriteInvalidParameterError

func (d *DefaultImpl) WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error)

func (*DefaultImpl) WriteJSONEncodeError

func (d *DefaultImpl) WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error)

func (*DefaultImpl) WriteResponseFile

func (*DefaultImpl) WriteResponseFile(_ *http.Request, w http.ResponseWriter, filename string, content []byte)

func (*DefaultImpl) WriteResponseJSON

func (d *DefaultImpl) WriteResponseJSON(r *http.Request, w http.ResponseWriter, res interface{})

type Errors

type Errors struct {
	Errors []string `json:"errors"`
}

type JSONError

type JSONError struct {
	Err error
}

func (JSONError) Error

func (e JSONError) Error() string

func (JSONError) Unwrap

func (e JSONError) Unwrap() error

type Utils

type Utils interface {
	// these methods were originally extracted from api package
	MustJSON(r *http.Request, w http.ResponseWriter, v interface{})
	HandleError(r *http.Request, w http.ResponseWriter, err error)
	IDFromRequest(r *http.Request) (uint, error)
	Logger(r *http.Request) logrus.FieldLogger

	// these methods were originally extracted from server package
	WriteResponseJSON(r *http.Request, w http.ResponseWriter, res interface{})
	WriteResponseFile(r *http.Request, w http.ResponseWriter, filename string, content []byte)
	WriteInvalidMethodError(r *http.Request, w http.ResponseWriter)
	WriteInvalidParameterError(r *http.Request, w http.ResponseWriter, err error)
	WriteInternalServerError(r *http.Request, w http.ResponseWriter, err error, msg string)
	WriteJSONEncodeError(r *http.Request, w http.ResponseWriter, err error)
	WriteError(r *http.Request, w http.ResponseWriter, code int, err error, msg string)
}

TODO(petethepig): this interface is pretty large, we can probably simplify it, some methods do pretty similar things

Jump to

Keyboard shortcuts

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