response

package
v1.24.5 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ServerError = InternalServerError

Functions

func BadRequest

func BadRequest(w http.ResponseWriter, message string)

func Error

func Error(w http.ResponseWriter, err error)

func Forbidden added in v1.23.9

func Forbidden(w http.ResponseWriter, message string)

func InternalServerError added in v1.23.9

func InternalServerError(w http.ResponseWriter, err error)

func NotFound

func NotFound(w http.ResponseWriter, message string)

func OK

func OK(w http.ResponseWriter, data any)

func Raw added in v1.23.8

func Raw(w http.ResponseWriter, status int, data any, headers map[string]string)

func Unauthorized added in v1.23.9

func Unauthorized(w http.ResponseWriter, message string)

Types

type Response

type Response struct {
	Message string `json:"message,omitempty"` // user friendly message, it contains error message or success message
	Data    any    `json:"data,omitempty"`    // data
	Error   any    `json:"error,omitempty"`   // raw error for debug purpose only
}

type StatusError

type StatusError struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
	RawErr  error  `json:"error,omitempty"`
}

func NewStatusError added in v1.23.9

func NewStatusError(status int, err error) *StatusError

func NewStatusErrorMessage added in v1.23.9

func NewStatusErrorMessage(status int, message string) *StatusError

func NewStatusErrorf added in v1.23.9

func NewStatusErrorf(status int, format string, args ...any) *StatusError

NewStatusErrorf acts like fmt.Errorf but returns a StatusError. Usage:

if err:=someprocess(username); err!=nil {
  return NewStatusErrorf(http.StatusNotFound, "user %s not found: %w", username, err)
}

func (StatusError) Error

func (e StatusError) Error() string

Jump to

Keyboard shortcuts

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