httpwrapper

package module
v0.0.4 Latest Latest
Warning

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

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

README

httpwrapper Documentation audit Go Report Card Coverage Status Maintainability

wrap your API to save you from boilerplate code

Examples

See doc or example_test.go

Documentation

Overview

Example (Get)

Wrapping a GET http handler.

zl := zerolog.New(io.Discard).With()

getter := func(ctx context.Context, key string) (string, *ErrorResponse) {
	if key == "id" {
		return "1", nil
	}

	return "", MissingParamError{Name: key}.ToErrorResponse()
}

getHandler := func(nupg NamedURLParamsGetter) TypedHandler {
	return func(r *http.Request) (*Response, *ErrorResponse) {
		idParam, errR := nupg(r.Context(), "id")
		if errR != nil {
			return nil, errR
		}

		id, err := strconv.ParseInt(idParam, 10, 64)
		if err != nil {
			return nil, ParsingParamError{
				Name:  "id",
				Value: idParam,
			}.ToErrorResponse()
		}

		return &Response{
			Body:           id,
			HTTPStatusCode: http.StatusOK,
		}, nil
	}
}

Wrapper(zl.Logger(), getHandler(getter)).ServeHTTP(nil, nil)
Example (Post)

Wrapping a POST http handler.

zl := zerolog.New(io.Discard).With()

type postRequest struct {
	Name string `json:"name"`
}

createHandler := func() TypedHandler {
	return func(r *http.Request) (*Response, *ErrorResponse) {
		var pr postRequest

		if err := BindBody(r, &pr); err != nil {
			return nil, err
		}

		log.Println(pr)

		return &Response{
			Body:           pr,
			HTTPStatusCode: http.StatusCreated,
		}, nil
	}
}

Wrapper(zl.Logger(), createHandler()).ServeHTTP(nil, nil)

Index

Examples

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