requestContext

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIDNonValid = NewError("Id was not valid", CodeErrIDNonValid)
	ErrIDTooLong  = NewError("Id was too long", CodeErrIDTooLong)
	ErrIDEmpty    = NewError("Id was empty", CodeErrIDEmpty)
)

Functions

func UnmarshalRequestBody

func UnmarshalRequestBody(r *http.Request, j interface{}) error

func UnmarshalRequestBytes

func UnmarshalRequestBytes(r *http.Request, b []byte, j interface{}) error

func UnmarshalWithKind

func UnmarshalWithKind(kind OutputKind, b []byte, j interface{}) error

func WriteAuto

func WriteAuto(output interface{}, err error, errCode ErrorCodes, r *http.Request, rw http.ResponseWriter) error

func WriteErr

func WriteErr(err error, code ErrorCodes, r *http.Request, rw http.ResponseWriter) error

func WriteError

func WriteError(msg string, code ErrorCodes, r *http.Request, rw http.ResponseWriter) error

func WriteOutput

func WriteOutput(isError bool, statusCode int, output interface{}, r *http.Request, rw http.ResponseWriter) error

Types

type ApiError

type ApiError struct {
	Error string `json:"error"`
	Code  string `json:"code,omitempty"`
}

type Context

type Context struct {
	L               logger.AppLogger
	DB              types.Storage
	StructValidater *validator.Validate
}

type Error

type Error struct {
	Code    ErrorCodes
	Message string
}

func NewError

func NewError(msg string, code ErrorCodes) Error

func (Error) Error

func (e Error) Error() string

type ErrorCodes

type ErrorCodes string
const (
	CodeErrEndpoint         ErrorCodes = "Error: Regarding type Endpoint"
	CodeErrRequest          ErrorCodes = "Error: Regarding type Request"
	CodeErrMethodNotAllowed ErrorCodes = "Error: HTTP-Method is not allowed"
	CodeErrNoRoute          ErrorCodes = "Error: No route matched for this http-path"
	CodeErrReadBody         ErrorCodes = "Error: Failed to read body"
	CodeErrMarhal           ErrorCodes = "Error: Failed to marshal"
	CodeErrUnmarshal        ErrorCodes = "Error: Failed to unmarshal"
	CodeErrJmesPath         ErrorCodes = "Error: JmesPath"
	CodeErrJmesPathMarshal  ErrorCodes = "Error: JmesPathMarshal"

	CodeErrInputValidation ErrorCodes = "Error: General input validation"
	CodeErrIDNonValid      ErrorCodes = "Error: ID not valid"
	CodeErrIDTooLong       ErrorCodes = "Error: ID is too long"
	CodeErrIDEmpty         ErrorCodes = "Error: ID was Empty"

	CodeErrDBUpdateSchedule ErrorCodes = "Error: Database Update Schedule"
	CodeErrDBUpdateEndpoint ErrorCodes = "Error: Database Update Endpoint"
	CodeErrDBUpdateRequest  ErrorCodes = "Error: Database Update Request"
	CodeErrDBDeleteEndpoint ErrorCodes = "Error: Database Delete Endpoint"
	CodeErrDBDeleteRequest  ErrorCodes = "Error: Database Delete Request"
	CodeErrDBDeleteSchedule ErrorCodes = "Error: Database Delete Schedule"
	CodeErrDBCreateEndpoint ErrorCodes = "Error: Database Create Endpoint"
	CodeErrSchedule         ErrorCodes = "Error: Database Create Schedule"
	CodeErrDBCreateRequest  ErrorCodes = "Error: Database Create Request"
	CodeErrDBCreateSchedule ErrorCodes = "Error: Database Create Schedule"
)

type OutputKind

type OutputKind = int
const (
	OutputJson OutputKind = iota + 1
	OutputYaml
	OutputToml
)

func WantedOutputFormat

func WantedOutputFormat(r *http.Request) OutputKind

attempts to guess at what kind of output the user most likely wants

type ReqContext

type ReqContext struct {
	Context     *Context
	Req         *http.Request
	L           logger.AppLogger
	Rw          http.ResponseWriter
	ContentKind OutputKind
	Accept      OutputKind
}

func NewReqContext

func NewReqContext(context *Context, req *http.Request, rw http.ResponseWriter) ReqContext

func (ReqContext) Unmarshal

func (rc ReqContext) Unmarshal(body []byte, j interface{}) error

func (ReqContext) ValidateBytes

func (rc ReqContext) ValidateBytes(body []byte, j interface{}) error

Will perform validation and write errors to responsewriter if validation failed. If err is non-nill, the caller should simply return

func (ReqContext) ValidateStruct

func (rc ReqContext) ValidateStruct(input interface{}) error

func (ReqContext) WriteAuto

func (rc ReqContext) WriteAuto(output interface{}, error error, errCode ErrorCodes)

func (ReqContext) WriteErr

func (rc ReqContext) WriteErr(err error, errCode ErrorCodes)

func (ReqContext) WriteError

func (rc ReqContext) WriteError(msg string, errCode ErrorCodes)

func (ReqContext) WriteOutput

func (rc ReqContext) WriteOutput(output interface{}, statusCode int)

Jump to

Keyboard shortcuts

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