web

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIDParam

func GetIDParam(r *http.Request) (int32, error)

GetIDParam returns url id param

func NewError

func NewError(err error, statusCode int) error

NewError creates error with status code

func NewErrorFromMessage

func NewErrorFromMessage(message string, statusCode int) error

NewErrorFromMessage creates error with status code from message

func NewErrorFromStatusCode

func NewErrorFromStatusCode(statusCode int) error

NewErrorFromStatusCode creates error from status code

func NewErrorWithFields

func NewErrorWithFields(err error, statusCode int, fields []FieldError) error

NewErrorWithFields creates error with status code and error fields

func Respond

func Respond(w http.ResponseWriter, val any, statusCode int)

Respond marshals a value to JSON and writes it to response

func Respond500

func Respond500(w http.ResponseWriter)

Respond500 returns InternalServerError to client

func RespondError

func RespondError(w http.ResponseWriter, respErr error)

RespondError handles outgoing errors by marshaling an error response. if error is not of type Error, then err is ignored. if no err is provided, InternalServerError returns to client

Types

type Decoder

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

Decoder decodes JSON request

func NewDecoder

func NewDecoder(log *zap.Logger, cfg *appconf.Cfg) *Decoder

NewDecoder creates new decoder

func (*Decoder) Decode

func (d *Decoder) Decode(r *http.Request, val any) error

Decode unmarshalls JSON request body

type Error

type Error struct {
	Err        error
	StatusCode int
	Fields     []FieldError
}

Error contains information about web error

func (*Error) Error

func (e *Error) Error() string

Error - returns error as string

type ErrorResponse

type ErrorResponse struct {
	Error  string       `json:"error"`
	Fields []FieldError `json:"fields,omitempty"`
}

ErrorResponse represents response in case of a web error

type FieldError

type FieldError struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

FieldError represents error in a struct field

type Sanitizable

type Sanitizable interface {
	Sanitize()
}

Sanitizable interface that request structs must implement

type Validatable

type Validatable interface {
	ValidateWith(v *validation.Validator) (bool, []FieldError)
}

Validatable interface that request structs must implement

Jump to

Keyboard shortcuts

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