http

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package http contains several tools for dealing with http such as writing different kinds out content types and error handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequestResponse

func BadRequestResponse(w http.ResponseWriter,
	r *http.Request, err error)

BadRequestResponse is used to handle an error when a request is incorrect.

func ConflictResponse

func ConflictResponse(
	w http.ResponseWriter,
	r *http.Request,
	err errortools.ConflictError,
)

ConflictResponse is used to handle an error when a resource already exists.

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, r *http.Request,
	status int, message any)

ErrorResponse is used to handle any kind of error.

func FailedValidationResponse

func FailedValidationResponse(
	w http.ResponseWriter,
	r *http.Request,
	errors map[string]string,
)

FailedValidationResponse is used to handle an error of a [validate.Validator].

func ForbiddenResponse

func ForbiddenResponse(w http.ResponseWriter, r *http.Request)

ForbiddenResponse is used to handle an error when a user isn't authorized to access a certain resource.

func HandleError

func HandleError(
	w http.ResponseWriter,
	r *http.Request,
	err error,
	validationErrors map[string]string,
)

HandleError is used to translate errors to the right HTTP response.

func NotFoundResponse

func NotFoundResponse(
	w http.ResponseWriter,
	r *http.Request,
	err errortools.NotFoundError,
)

NotFoundResponse is used to handle an error when a resource wasn't found.

func RateLimitExceededResponse

func RateLimitExceededResponse(w http.ResponseWriter,
	r *http.Request)

RateLimitExceededResponse is used to handle an error when the rate limit is exceeded.

func ReadCSV

func ReadCSV(body io.Reader) ([][]string, error)

ReadCSV reads the returned CSV file from a http.Response.Body.

func ReadJSON

func ReadJSON(body io.Reader, dst any) error

ReadJSON reads the returned data from a http.Response.Body and assigns the decoded value to dst.

func Serve

func Serve(logger *slog.Logger, srv *http.Server, environment string) error

Serve calls http.Server.ListenAndServe with some more fluff around it to handle unexpected shutdowns nicely.

func ServerErrorResponse

func ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)

ServerErrorResponse is used to handle internal server errors.

func UnauthorizedResponse

func UnauthorizedResponse(w http.ResponseWriter,
	r *http.Request, err errortools.UnauthorizedError)

UnauthorizedResponse is used to handle an error when a user isn't authorized.

func WriteCSV

func WriteCSV(
	w http.ResponseWriter,
	filename string,
	headers []string,
	data [][]string,
) error

WriteCSV writes the provided data as a CSV file with the provided filename to a http.ResponseWriter.

func WriteJSON

func WriteJSON(
	w http.ResponseWriter,
	status int,
	data any,
	headers http.Header,
) error

WriteJSON writes the provided status, data and headers to a http.ResponseWriter.

Types

type ResponseWriter added in v0.1.3

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker // need this for sentry
	http.Flusher  // need this for sentry
	io.ReaderFrom // need this for sentry
	Status() int
}

A ResponseWriter is used to capture set status codes.

func NewResponseWriter added in v0.1.3

func NewResponseWriter(w http.ResponseWriter) ResponseWriter

NewResponseWriter returns a new ResponseWriter.

Jump to

Keyboard shortcuts

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