Documentation ¶
Overview ¶
Package http contains several tools for dealing with http such as writing different kinds out content types and error handling.
Index ¶
- func BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)
- func ConflictResponse(w http.ResponseWriter, r *http.Request, err errortools.ConflictError)
- func ErrorResponse(w http.ResponseWriter, r *http.Request, status int, message any)
- func FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string)
- func ForbiddenResponse(w http.ResponseWriter, r *http.Request)
- func HandleError(w http.ResponseWriter, r *http.Request, err error, ...)
- func NotFoundResponse(w http.ResponseWriter, r *http.Request, err errortools.NotFoundError)
- func RateLimitExceededResponse(w http.ResponseWriter, r *http.Request)
- func ReadCSV(body io.Reader) ([][]string, error)
- func ReadJSON(body io.Reader, dst any) error
- func Serve(logger *slog.Logger, srv *http.Server, environment string) error
- func ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)
- func UnauthorizedResponse(w http.ResponseWriter, r *http.Request, err errortools.UnauthorizedError)
- func WriteCSV(w http.ResponseWriter, filename string, headers []string, data [][]string) error
- func WriteJSON(w http.ResponseWriter, status int, data any, headers http.Header) error
- type ResponseWriter
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 ¶
ErrorResponse is used to handle any kind of error.
func FailedValidationResponse ¶
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 ¶
ReadCSV reads the returned CSV file from a http.Response.Body.
func ReadJSON ¶
ReadJSON reads the returned data from a http.Response.Body and assigns the decoded value to dst.
func Serve ¶
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 ¶
WriteCSV writes the provided data as a CSV file with the provided filename to a http.ResponseWriter.
func WriteJSON ¶
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.