httpwrapper

package module
v0.0.3 Latest Latest
Warning

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

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

README

httpwrapper Documentation Go Report Card Coverage Status Maintainability

wrap your API to save you from boilerplate code

Documentation

Index

Constants

View Source
const (
	ErrCodeParsingBody = "error_parsing_body"
)

Variables

This section is empty.

Functions

func Wrapper

func Wrapper(
	log zerolog.Logger,
	f TypedHandler,
) http.HandlerFunc

Wrapper will actually do the boring work of logging an error and render the response.

Types

type ErrorResponse

type ErrorResponse struct {
	Error          error  `json:"-"`
	HTTPStatusCode int    `json:"-"`
	ErrorCode      string `json:"error_code"`
	ErrorMsg       string `json:"error_msg"`
}

ErrorResponse is a wrapper for the error response body to have a clean way of displaying errors.

func BindBody

func BindBody(r *http.Request, target interface{}) *ErrorResponse

BindBody will bind the body of the request to the given interface.

func NewErrorResponse

func NewErrorResponse(
	e error,
	hsc int,
	ec string,
	msg string,
) *ErrorResponse

NewErrorResponse creates a new ErrorResponse.

func (*ErrorResponse) IsEqual

func (her *ErrorResponse) IsEqual(e1 *ErrorResponse) bool

IsEqual checks if an error response is equal to another.

type InternalServerError

type InternalServerError struct {
	Err error
}

InternalServerError is an error that is returned when an internal server error occurs.

func (InternalServerError) Error

func (e InternalServerError) Error() string

func (InternalServerError) ToErrorResponse

func (e InternalServerError) ToErrorResponse() *ErrorResponse

type MissingParamError

type MissingParamError struct {
	Name string
}

MissingParamError is the error that is returned when a named URL param is missing.

func (MissingParamError) Error

func (e MissingParamError) Error() string

func (MissingParamError) ToErrorResponse

func (e MissingParamError) ToErrorResponse() *ErrorResponse

type NamedURLParamsGetter

type NamedURLParamsGetter func(ctx context.Context, key string) (string, *ErrorResponse)

NamedURLParamsGetter is the interface that is used to parse the URL parameters.

type NotFoundError

type NotFoundError struct {
	Designation string
}

NotFoundError is an error that is returned when a resource is not found.

func (NotFoundError) Error

func (e NotFoundError) Error() string

func (NotFoundError) ToErrorResponse

func (e NotFoundError) ToErrorResponse() *ErrorResponse

type ParsingParamError

type ParsingParamError struct {
	Name  string
	Value string
}

ParsingParamError is the error that is returned when a named URL param is invalid.

func (ParsingParamError) Error

func (e ParsingParamError) Error() string

func (ParsingParamError) ToErrorResponse

func (e ParsingParamError) ToErrorResponse() *ErrorResponse

type Response

type Response struct {
	Body           any
	HTTPStatusCode int
}

Response is a wrapper for the response body.

type TypedHandler

type TypedHandler func(r *http.Request) (*Response, *ErrorResponse)

TypedHandler is the handler that you are actually handling the response.

Jump to

Keyboard shortcuts

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