httpio

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT, MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

httpio

The httpio package provides tools for decoding HTTP requests, decoding url parameters, and encoding HTTP responses in Go, complete with validation rules.

Getting Started

First, get the package by running:

go get github.com/impulseframework/impulse/httpio

StructDecoder

StructDecoder decodes a JSON request body into a plain struct and validates it. The body must be a JSON object, and every key it names must reach a field of the struct: a field with a json tag answers to that tag only; a field without one answers to its Go name, exactly or fully lower-cased. A key no field answers to is refused with a 400 invalid field in json - <key>, and a JSON null lands only in a pointer field. Every refusal is a ClientMessage, so a handler writes it with Encoder.ClientMessage.

Validation is optional. WithValidator takes a ValidatorFunc, an interface with Struct(s any) error and StructPartial(s any, fields ...string) error, which *validator.Validate from github.com/go-playground/validator satisfies. A PATCH validates only the fields the body carried; every other method validates the whole struct. A rejection is a 400 failed validating the request.

A request body bound to a resource decodes with the resource package's decoders instead, which add what its generated struct tags declare.

Example usage
type MyRequest struct {
    Field1 string `json:"field1" validate:"required"`
    Field2 int    `json:"field2" validate:"required,gt=0"`
}

// NewStructDecoder fails only for a type that is not a struct or whose wire names
// collide, a programming error, so construct the decoder once, at startup.
func newDecoder[T any]() *httpio.StructDecoder[T] {
    decoder, err := httpio.NewStructDecoder[T]()
    if err != nil {
        panic(err)
    }

    return decoder.WithValidator(validator.New())
}

func MyHandler() http.HandlerFunc {
    decoder := newDecoder[MyRequest]()

    return func(w http.ResponseWriter, r *http.Request) {
        req, err := decoder.Decode(r)
        if err != nil {
            _ = httpio.NewEncoder(w).ClientMessage(r.Context(), err)

            return
        }
        // continue processing req...
    }
}

Encoder

The Encoder struct is used to encode HTTP responses. It has an implementation of the json.NewEncoder() function to encode a provided struct into the HTTP response body. The Encoder also allows for setting HTTP status codes and headers.

For usage of Encoder, please refer to the httpio package's source code.

Example usage

Here's an example of how to use Encoder:

type MyResponse struct {
    Message string `json:"message"`
    Code    int    `json:"code"`
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    // create response body
    responseBody := &MyResponse{
        Message: "Hello, world!",
        Code:    http.StatusOK,
    }

    // encode and send the response
    if err := httpio.NewEncoder(w).Ok(responseBody); err != nil {
        // handle error
        return
    }
}

The Encoder struct also provides methods to handle errors and encode HTTP error responses. Here's an example:

func MyHandler(w http.ResponseWriter, r *http.Request) {
    // some operation that may cause an error
    err := someOperation()
    if err != nil {
        // if the operation fails, return an Internal Server Error
        httpio.NewEncoder(w).InternalServerErrorWithMessage("This is what is returned in the response message", err)
        return
    }

    // if the operation is successful, proceed as normal...
}

Params

The Params() generic function serves as an enhancement to the chi router's parameters feature by decoding HTTP URL parameters into native Go types.

Currently the supported types are string, int, int64, float64, bool, and any type that implements the encoding.TextUnmarshaler interface.

Example usage
// given url: http://myapi.com/api/fileid/26
// and chi route of:          /api/fileid/{fileId}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    param := Param[int64](r, "fileId")
    // param is parsed as type int64
    //
    // WithParams() middleware should be used to catch parsing errors. It responds
    // with a 400 and a generic message naming the parameter; the underlying
    // parse error is logged server-side and never returned to the client.
}

Log

Log returns a http.HandlerFunc that logs any error coming from handlers. This provides a more ergonomic feel by allowing errors to be returned from handlers

Example
func MyHandler() http.HandlerFunc {
	return httpio.Log(func(w http.ResponseWriter, r *http.Request) error {
		// do something
		return errors.New("error")
	})
}

License

This project is licensed under the MIT License.

Documentation

Overview

Package httpio handles encoding and decoding for http i/o. This package is used to standardize request and response handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CauseIsError

func CauseIsError(err error) bool

CauseIsError returns true if the Cause of this error is an error vs a ClientMessage with nil error

func HasBadGateway

func HasBadGateway(err error) bool

HasBadGateway checks if the error contains a BadGateway (502) message

func HasBadRequest

func HasBadRequest(err error) bool

HasBadRequest checks if the error contains a BadRequest (400) message

func HasClientClosedRequest

func HasClientClosedRequest(err error) bool

HasClientClosedRequest checks if the error contains a ClientClosedRequest (499) message

func HasClientMessage

func HasClientMessage(err error) bool

HasClientMessage checks if the error contains a client message

func HasConflict

func HasConflict(err error) bool

HasConflict checks if the error contains a Conflict (409) message

func HasForbidden

func HasForbidden(err error) bool

HasForbidden checks if the error contains a Forbidden (403) message

func HasGatewayTimeout

func HasGatewayTimeout(err error) bool

HasGatewayTimeout checks if the error contains a GatewayTimeout (504) message

func HasInternalServerError

func HasInternalServerError(err error) bool

HasInternalServerError checks if the error contains a InternalServerError (500) message

func HasMethodNotAllowed

func HasMethodNotAllowed(err error) bool

HasMethodNotAllowed checks if the error contains a MethodNotAllowed (405) message

func HasNotAcceptable

func HasNotAcceptable(err error) bool

HasNotAcceptable checks if the error contains a NotAcceptable (406) message

func HasNotFound

func HasNotFound(err error) bool

HasNotFound checks if the error contains a NotFound (404) message

func HasNotImplemented

func HasNotImplemented(err error) bool

HasNotImplemented checks if the error contains a NotImplemented (501) message

func HasRequestEntityTooLarge

func HasRequestEntityTooLarge(err error) bool

HasRequestEntityTooLarge checks if the error contains a RequestEntityTooLarge (413) message

func HasRequestTimeout

func HasRequestTimeout(err error) bool

HasRequestTimeout checks if the error contains a RequestTimeout (408) message

func HasServiceUnavailable

func HasServiceUnavailable(err error) bool

HasServiceUnavailable checks if the error contains a ServiceUnavailable (503) message

func HasTooManyRequests

func HasTooManyRequests(err error) bool

HasTooManyRequests checks if the error contains a TooManyRequests (429) message

func HasUnauthorized

func HasUnauthorized(err error) bool

HasUnauthorized checks if the error contains a Unauthorized (401) message

func HasUnprocessableEntity

func HasUnprocessableEntity(err error) bool

HasUnprocessableEntity checks if the error contains a UnprocessableEntity (422) message

func HasUnsupportedMediaType

func HasUnsupportedMediaType(err error) bool

HasUnsupportedMediaType checks if the error contains a UnsupportedMediaType (415) message

func Log

func Log(handler func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc

Log returns a http.HandlerFunc that logs any error coming from handlers. This provides a more ergonomic feel by allowing errors to be returned from handlers

Example usage:

func Handler() http.HandlerFunc {
	return httpio.Log(func(w http.ResponseWriter, r *http.Request) error {
		// do something
		return errors.New("error")
	})
}

func Message

func Message(err error) string

Message returns the message from the wrapped ClientMessage or an empty string

func Messages

func Messages(err error) []string

Messages returns a slice of messages from the ClientMessage's contained within the chain of errors

func NewBadGateway

func NewBadGateway() errors.Chain

NewBadGateway creates a new empty client message with a BadGateway (502) return code

func NewBadGatewayMessage

func NewBadGatewayMessage(message string) errors.Chain

NewBadGatewayMessage creates a new client message with a BadGateway (502) return code

func NewBadGatewayMessageWithError

func NewBadGatewayMessageWithError(err error, message string) errors.Chain

NewBadGatewayMessageWithError wraps an existing error while creating a new client message with a BadGateway (502) return code

func NewBadGatewayMessageWithErrorf

func NewBadGatewayMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewBadGatewayMessageWithErrorf wraps an existing error while creating a new client message with a BadGateway (502) return code

func NewBadGatewayMessagef

func NewBadGatewayMessagef(format string, a ...any) errors.Chain

NewBadGatewayMessagef creates a new client message with a BadGateway (502) return code

func NewBadGatewayWithError

func NewBadGatewayWithError(err error) errors.Chain

NewBadGatewayWithError wraps an existing error while creating a new empty client message and a BadGateway (502) return code

func NewBadRequest

func NewBadRequest() errors.Chain

NewBadRequest creates a new empty client message with a BadRequest (400) return code

func NewBadRequestMessage

func NewBadRequestMessage(message string) errors.Chain

NewBadRequestMessage creates a new client message with a BadRequest (400) return code

func NewBadRequestMessageWithError

func NewBadRequestMessageWithError(err error, message string) errors.Chain

NewBadRequestMessageWithError wraps an existing error while creating a new client message with a BadRequest (400) return code

func NewBadRequestMessageWithErrorf

func NewBadRequestMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewBadRequestMessageWithErrorf wraps an existing error while creating a new client message with a BadRequest (400) return code

func NewBadRequestMessagef

func NewBadRequestMessagef(format string, a ...any) errors.Chain

NewBadRequestMessagef creates a new client message with a BadRequest (400) return code

func NewBadRequestWithError

func NewBadRequestWithError(err error) errors.Chain

NewBadRequestWithError wraps an existing error while creating a new empty client message and a BadRequest (400) return code

func NewClientClosedRequest

func NewClientClosedRequest() errors.Chain

NewClientClosedRequest creates a new empty client message with a ClientClosedRequest (499) return code

func NewClientClosedRequestMessage

func NewClientClosedRequestMessage(message string) errors.Chain

NewClientClosedRequestMessage creates a new client message with a ClientClosedRequest (499) return code

func NewClientClosedRequestMessageWithError

func NewClientClosedRequestMessageWithError(err error, message string) errors.Chain

NewClientClosedRequestMessageWithError wraps an existing error while creating a new client message with a ClientClosedRequest (499) return code

func NewClientClosedRequestMessageWithErrorf

func NewClientClosedRequestMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewClientClosedRequestMessageWithErrorf wraps an existing error while creating a new client message with a ClientClosedRequest (499) return code

func NewClientClosedRequestMessagef

func NewClientClosedRequestMessagef(format string, a ...any) errors.Chain

NewClientClosedRequestMessagef creates a new client message with a ClientClosedRequest (499) return code

func NewClientClosedRequestWithError

func NewClientClosedRequestWithError(err error) errors.Chain

NewClientClosedRequestWithError wraps an existing error while creating a new empty client message and a ClientClosedRequest (499) return code

func NewConflict

func NewConflict() errors.Chain

NewConflict creates a new empty client message with a Conflict (409) return code

func NewConflictMessage

func NewConflictMessage(message string) errors.Chain

NewConflictMessage creates a new client message with a Conflict (409) return code

func NewConflictMessageWithError

func NewConflictMessageWithError(err error, message string) errors.Chain

NewConflictMessageWithError wraps an existing error while creating a new client message with a Conflict (409) return code

func NewConflictMessageWithErrorf

func NewConflictMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewConflictMessageWithErrorf wraps an existing error while creating a new client message with a Conflict (409) return code

func NewConflictMessagef

func NewConflictMessagef(format string, a ...any) errors.Chain

NewConflictMessagef creates a new client message with a Conflict (409) return code

func NewConflictWithError

func NewConflictWithError(err error) errors.Chain

NewConflictWithError wraps an existing error while creating a new empty client message and a Conflict (409) return code

func NewForbidden

func NewForbidden() errors.Chain

NewForbidden creates a new empty client message with a Forbidden (403) return code

func NewForbiddenMessage

func NewForbiddenMessage(message string) errors.Chain

NewForbiddenMessage creates a new client message with a Forbidden (403) return code

func NewForbiddenMessageWithError

func NewForbiddenMessageWithError(err error, message string) errors.Chain

NewForbiddenMessageWithError wraps an existing error while creating a new client message with a Forbidden (403) return code

func NewForbiddenMessageWithErrorf

func NewForbiddenMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewForbiddenMessageWithErrorf wraps an existing error while creating a new client message with a Forbidden (403) return code

func NewForbiddenMessagef

func NewForbiddenMessagef(format string, a ...any) errors.Chain

NewForbiddenMessagef creates a new client message with a Forbidden (403) return code

func NewForbiddenWithError

func NewForbiddenWithError(err error) errors.Chain

NewForbiddenWithError wraps an existing error while creating a new empty client message and a Forbidden (403) return code

func NewGatewayTimeout

func NewGatewayTimeout() errors.Chain

NewGatewayTimeout creates a new empty client message with a GatewayTimeout (504) return code

func NewGatewayTimeoutMessage

func NewGatewayTimeoutMessage(message string) errors.Chain

NewGatewayTimeoutMessage creates a new client message with a GatewayTimeout (504) return code

func NewGatewayTimeoutMessageWithError

func NewGatewayTimeoutMessageWithError(err error, message string) errors.Chain

NewGatewayTimeoutMessageWithError wraps an existing error while creating a new client message with a GatewayTimeout (504) return code

func NewGatewayTimeoutMessageWithErrorf

func NewGatewayTimeoutMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewGatewayTimeoutMessageWithErrorf wraps an existing error while creating a new client message with a GatewayTimeout (504) return code

func NewGatewayTimeoutMessagef

func NewGatewayTimeoutMessagef(format string, a ...any) errors.Chain

NewGatewayTimeoutMessagef creates a new client message with a GatewayTimeout (504) return code

func NewGatewayTimeoutWithError

func NewGatewayTimeoutWithError(err error) errors.Chain

NewGatewayTimeoutWithError wraps an existing error while creating a new empty client message and a GatewayTimeout (504) return code

func NewInternalServerError

func NewInternalServerError() errors.Chain

NewInternalServerError creates a new empty client message with a InternalServerError (500) return code

func NewInternalServerErrorMessage

func NewInternalServerErrorMessage(message string) errors.Chain

NewInternalServerErrorMessage creates a new client message with a InternalServerError (500) return code

func NewInternalServerErrorMessageWithError

func NewInternalServerErrorMessageWithError(err error, message string) errors.Chain

NewInternalServerErrorMessageWithError wraps an existing error while creating a new client message with a InternalServerError (500) return code

func NewInternalServerErrorMessageWithErrorf

func NewInternalServerErrorMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewInternalServerErrorMessageWithErrorf wraps an existing error while creating a new client message with a InternalServerError (500) return code

func NewInternalServerErrorMessagef

func NewInternalServerErrorMessagef(format string, a ...any) errors.Chain

NewInternalServerErrorMessagef creates a new client message with a InternalServerError (500) return code

func NewInternalServerErrorWithError

func NewInternalServerErrorWithError(err error) errors.Chain

NewInternalServerErrorWithError wraps an existing error while creating a new empty client message and a InternalServerError (500) return code

func NewMethodNotAllowed

func NewMethodNotAllowed() errors.Chain

NewMethodNotAllowed creates a new empty client message with a MethodNotAllowed (405) return code

func NewMethodNotAllowedMessage

func NewMethodNotAllowedMessage(message string) errors.Chain

NewMethodNotAllowedMessage creates a new client message with a MethodNotAllowed (405) return code

func NewMethodNotAllowedMessageWithError

func NewMethodNotAllowedMessageWithError(err error, message string) errors.Chain

NewMethodNotAllowedMessageWithError wraps an existing error while creating a new client message with a MethodNotAllowed (405) return code

func NewMethodNotAllowedMessageWithErrorf

func NewMethodNotAllowedMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewMethodNotAllowedMessageWithErrorf wraps an existing error while creating a new client message with a MethodNotAllowed (405) return code

func NewMethodNotAllowedMessagef

func NewMethodNotAllowedMessagef(format string, a ...any) errors.Chain

NewMethodNotAllowedMessagef creates a new client message with a MethodNotAllowed (405) return code

func NewMethodNotAllowedWithError

func NewMethodNotAllowedWithError(err error) errors.Chain

NewMethodNotAllowedWithError wraps an existing error while creating a new empty client message and a MethodNotAllowed (405) return code

func NewNotAcceptable

func NewNotAcceptable() errors.Chain

NewNotAcceptable creates a new empty client message with a NotAcceptable (406) return code

func NewNotAcceptableMessage

func NewNotAcceptableMessage(message string) errors.Chain

NewNotAcceptableMessage creates a new client message with a NotAcceptable (406) return code

func NewNotAcceptableMessageWithError

func NewNotAcceptableMessageWithError(err error, message string) errors.Chain

NewNotAcceptableMessageWithError wraps an existing error while creating a new client message with a NotAcceptable (406) return code

func NewNotAcceptableMessageWithErrorf

func NewNotAcceptableMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewNotAcceptableMessageWithErrorf wraps an existing error while creating a new client message with a NotAcceptable (406) return code

func NewNotAcceptableMessagef

func NewNotAcceptableMessagef(format string, a ...any) errors.Chain

NewNotAcceptableMessagef creates a new client message with a NotAcceptable (406) return code

func NewNotAcceptableWithError

func NewNotAcceptableWithError(err error) errors.Chain

NewNotAcceptableWithError wraps an existing error while creating a new empty client message and a NotAcceptable (406) return code

func NewNotFound

func NewNotFound() errors.Chain

NewNotFound creates a new empty client message with a NotFound (404) return code

func NewNotFoundMessage

func NewNotFoundMessage(message string) errors.Chain

NewNotFoundMessage creates a new client message with a NotFound (404) return code

func NewNotFoundMessageWithError

func NewNotFoundMessageWithError(err error, message string) errors.Chain

NewNotFoundMessageWithError wraps an existing error while creating a new client message with a NotFound (404) return code

func NewNotFoundMessageWithErrorf

func NewNotFoundMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewNotFoundMessageWithErrorf wraps an existing error while creating a new client message with a NotFound (404) return code

func NewNotFoundMessagef

func NewNotFoundMessagef(format string, a ...any) errors.Chain

NewNotFoundMessagef creates a new client message with a NotFound (404) return code

func NewNotFoundWithError

func NewNotFoundWithError(err error) errors.Chain

NewNotFoundWithError wraps an existing error while creating a new empty client message and a NotFound (404) return code

func NewNotImplemented

func NewNotImplemented() errors.Chain

NewNotImplemented creates a new empty client message with a NotImplemented (501) return code

func NewNotImplementedMessage

func NewNotImplementedMessage(message string) errors.Chain

NewNotImplementedMessage creates a new client message with a NotImplemented (501) return code

func NewNotImplementedMessageWithError

func NewNotImplementedMessageWithError(err error, message string) errors.Chain

NewNotImplementedMessageWithError wraps an existing error while creating a new client message with a NotImplemented (501) return code

func NewNotImplementedMessageWithErrorf

func NewNotImplementedMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewNotImplementedMessageWithErrorf wraps an existing error while creating a new client message with a NotImplemented (501) return code

func NewNotImplementedMessagef

func NewNotImplementedMessagef(format string, a ...any) errors.Chain

NewNotImplementedMessagef creates a new client message with a NotImplemented (501) return code

func NewNotImplementedWithError

func NewNotImplementedWithError(err error) errors.Chain

NewNotImplementedWithError wraps an existing error while creating a new empty client message and a NotImplemented (501) return code

func NewRequestEntityTooLarge

func NewRequestEntityTooLarge() errors.Chain

NewRequestEntityTooLarge creates a new empty client message with a RequestEntityTooLarge (413) return code

func NewRequestEntityTooLargeMessage

func NewRequestEntityTooLargeMessage(message string) errors.Chain

NewRequestEntityTooLargeMessage creates a new client message with a RequestEntityTooLarge (413) return code

func NewRequestEntityTooLargeMessageWithError

func NewRequestEntityTooLargeMessageWithError(err error, message string) errors.Chain

NewRequestEntityTooLargeMessageWithError wraps an existing error while creating a new client message with a RequestEntityTooLarge (413) return code

func NewRequestEntityTooLargeMessageWithErrorf

func NewRequestEntityTooLargeMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewRequestEntityTooLargeMessageWithErrorf wraps an existing error while creating a new client message with a RequestEntityTooLarge (413) return code

func NewRequestEntityTooLargeMessagef

func NewRequestEntityTooLargeMessagef(format string, a ...any) errors.Chain

NewRequestEntityTooLargeMessagef creates a new client message with a RequestEntityTooLarge (413) return code

func NewRequestEntityTooLargeWithError

func NewRequestEntityTooLargeWithError(err error) errors.Chain

NewRequestEntityTooLargeWithError wraps an existing error while creating a new empty client message and a RequestEntityTooLarge (413) return code

func NewRequestTimeout

func NewRequestTimeout() errors.Chain

NewRequestTimeout creates a new empty client message with a RequestTimeout (408) return code

func NewRequestTimeoutMessage

func NewRequestTimeoutMessage(message string) errors.Chain

NewRequestTimeoutMessage creates a new client message with a RequestTimeout (408) return code

func NewRequestTimeoutMessageWithError

func NewRequestTimeoutMessageWithError(err error, message string) errors.Chain

NewRequestTimeoutMessageWithError wraps an existing error while creating a new client message with a RequestTimeout (408) return code

func NewRequestTimeoutMessageWithErrorf

func NewRequestTimeoutMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewRequestTimeoutMessageWithErrorf wraps an existing error while creating a new client message with a RequestTimeout (408) return code

func NewRequestTimeoutMessagef

func NewRequestTimeoutMessagef(format string, a ...any) errors.Chain

NewRequestTimeoutMessagef creates a new client message with a RequestTimeout (408) return code

func NewRequestTimeoutWithError

func NewRequestTimeoutWithError(err error) errors.Chain

NewRequestTimeoutWithError wraps an existing error while creating a new empty client message and a RequestTimeout (408) return code

func NewServiceUnavailable

func NewServiceUnavailable() errors.Chain

NewServiceUnavailable creates a new empty client message with a ServiceUnavailable (503) return code

func NewServiceUnavailableMessage

func NewServiceUnavailableMessage(message string) errors.Chain

NewServiceUnavailableMessage creates a new client message with a ServiceUnavailable (503) return code

func NewServiceUnavailableMessageWithError

func NewServiceUnavailableMessageWithError(err error, message string) errors.Chain

NewServiceUnavailableMessageWithError wraps an existing error while creating a new client message with a ServiceUnavailable (503) return code

func NewServiceUnavailableMessageWithErrorf

func NewServiceUnavailableMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewServiceUnavailableMessageWithErrorf wraps an existing error while creating a new client message with a ServiceUnavailable (503) return code

func NewServiceUnavailableMessagef

func NewServiceUnavailableMessagef(format string, a ...any) errors.Chain

NewServiceUnavailableMessagef creates a new client message with a ServiceUnavailable (503) return code

func NewServiceUnavailableWithError

func NewServiceUnavailableWithError(err error) errors.Chain

NewServiceUnavailableWithError wraps an existing error while creating a new empty client message and a ServiceUnavailable (503) return code

func NewTooManyRequests

func NewTooManyRequests() errors.Chain

NewTooManyRequests creates a new empty client message with a TooManyRequests (429) return code

func NewTooManyRequestsMessage

func NewTooManyRequestsMessage(message string) errors.Chain

NewTooManyRequestsMessage creates a new client message with a TooManyRequests (429) return code

func NewTooManyRequestsMessageWithError

func NewTooManyRequestsMessageWithError(err error, message string) errors.Chain

NewTooManyRequestsMessageWithError wraps an existing error while creating a new client message with a TooManyRequests (429) return code

func NewTooManyRequestsMessageWithErrorf

func NewTooManyRequestsMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewTooManyRequestsMessageWithErrorf wraps an existing error while creating a new client message with a TooManyRequests (429) return code

func NewTooManyRequestsMessagef

func NewTooManyRequestsMessagef(format string, a ...any) errors.Chain

NewTooManyRequestsMessagef creates a new client message with a TooManyRequests (429) return code

func NewTooManyRequestsWithError

func NewTooManyRequestsWithError(err error) errors.Chain

NewTooManyRequestsWithError wraps an existing error while creating a new empty client message and a TooManyRequests (429) return code

func NewUnauthorized

func NewUnauthorized() errors.Chain

NewUnauthorized creates a new empty client message with a Unauthorized (401) return code

func NewUnauthorizedMessage

func NewUnauthorizedMessage(message string) errors.Chain

NewUnauthorizedMessage creates a new client message with a Unauthorized (401) return code

func NewUnauthorizedMessageWithError

func NewUnauthorizedMessageWithError(err error, message string) errors.Chain

NewUnauthorizedMessageWithError wraps an existing error while creating a new client message with a Unauthorized (401) return code

func NewUnauthorizedMessageWithErrorf

func NewUnauthorizedMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewUnauthorizedMessageWithErrorf wraps an existing error while creating a new client message with a Unauthorized (401) return code

func NewUnauthorizedMessagef

func NewUnauthorizedMessagef(format string, a ...any) errors.Chain

NewUnauthorizedMessagef creates a new client message with a Unauthorized (401) return code

func NewUnauthorizedWithError

func NewUnauthorizedWithError(err error) errors.Chain

NewUnauthorizedWithError wraps an existing error while creating a new empty client message and a Unauthorized (401) return code

func NewUnprocessableEntity

func NewUnprocessableEntity() errors.Chain

NewUnprocessableEntity creates a new empty client message with a UnprocessableEntity (422) return code

func NewUnprocessableEntityMessage

func NewUnprocessableEntityMessage(message string) errors.Chain

NewUnprocessableEntityMessage creates a new client message with a UnprocessableEntity (422) return code

func NewUnprocessableEntityMessageWithError

func NewUnprocessableEntityMessageWithError(err error, message string) errors.Chain

NewUnprocessableEntityMessageWithError wraps an existing error while creating a new client message with a UnprocessableEntity (422) return code

func NewUnprocessableEntityMessageWithErrorf

func NewUnprocessableEntityMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewUnprocessableEntityMessageWithErrorf wraps an existing error while creating a new client message with a UnprocessableEntity (422) return code

func NewUnprocessableEntityMessagef

func NewUnprocessableEntityMessagef(format string, a ...any) errors.Chain

NewUnprocessableEntityMessagef creates a new client message with a UnprocessableEntity (422) return code

func NewUnprocessableEntityWithError

func NewUnprocessableEntityWithError(err error) errors.Chain

NewUnprocessableEntityWithError wraps an existing error while creating a new empty client message and a UnprocessableEntity (422) return code

func NewUnsupportedMediaType

func NewUnsupportedMediaType() errors.Chain

NewUnsupportedMediaType creates a new empty client message with a UnsupportedMediaType (415) return code

func NewUnsupportedMediaTypeMessage

func NewUnsupportedMediaTypeMessage(message string) errors.Chain

NewUnsupportedMediaTypeMessage creates a new client message with a UnsupportedMediaType (415) return code

func NewUnsupportedMediaTypeMessageWithError

func NewUnsupportedMediaTypeMessageWithError(err error, message string) errors.Chain

NewUnsupportedMediaTypeMessageWithError wraps an existing error while creating a new client message with a UnsupportedMediaType (415) return code

func NewUnsupportedMediaTypeMessageWithErrorf

func NewUnsupportedMediaTypeMessageWithErrorf(err error, format string, a ...any) errors.Chain

NewUnsupportedMediaTypeMessageWithErrorf wraps an existing error while creating a new client message with a UnsupportedMediaType (415) return code

func NewUnsupportedMediaTypeMessagef

func NewUnsupportedMediaTypeMessagef(format string, a ...any) errors.Chain

NewUnsupportedMediaTypeMessagef creates a new client message with a UnsupportedMediaType (415) return code

func NewUnsupportedMediaTypeWithError

func NewUnsupportedMediaTypeWithError(err error) errors.Chain

NewUnsupportedMediaTypeWithError wraps an existing error while creating a new empty client message and a UnsupportedMediaType (415) return code

func Param

func Param[T any](r *http.Request, param ParamType) (val T)

Param extracts the Param from the Request Context

func WithParams

func WithParams(next http.Handler) http.Handler

WithParams middleware is used to capture Param Parsing errors. They are returned as a http.StatusBadRequest status code with a generic message naming the offending parameter. The underlying parse error is logged server-side and never sent to the client.

Types

type ClientMessage

type ClientMessage struct {
	// contains filtered or unexported fields
}

ClientMessage is a custom message type that can be used to return client messages

func (*ClientMessage) Error

func (c *ClientMessage) Error() string

Error returns the error message

func (*ClientMessage) Message

func (c *ClientMessage) Message() string

Message returns the client message

func (*ClientMessage) Unwrap

func (c *ClientMessage) Unwrap() error

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder is a struct that is used for encoding http responses

func NewEncoder

func NewEncoder(w http.ResponseWriter) *Encoder

NewEncoder returns a new Encoder to write to the ResponseWriter This encoder will write to the ResponseWriter using a json encoder.

func (*Encoder) BadGateway

func (e *Encoder) BadGateway(ctx context.Context) error

BadGateway creates a new empty client message with a BadGateway (502) return code

func (*Encoder) BadGatewayMessage

func (e *Encoder) BadGatewayMessage(ctx context.Context, message string) error

BadGatewayMessage creates a new client message with a BadGateway (502) return code

func (*Encoder) BadGatewayMessageWithError

func (e *Encoder) BadGatewayMessageWithError(ctx context.Context, err error, message string) error

BadGatewayMessageWithError wraps an existing error while creating a new client message with a BadGateway (502) return code

func (*Encoder) BadGatewayMessageWithErrorf

func (e *Encoder) BadGatewayMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

BadGatewayMessageWithErrorf wraps an existing error while creating a new client message with a BadGateway (502) return code

func (*Encoder) BadGatewayMessagef

func (e *Encoder) BadGatewayMessagef(ctx context.Context, format string, a ...any) error

BadGatewayMessagef creates a new client message with a BadGateway (502) return code

func (*Encoder) BadGatewayWithError

func (e *Encoder) BadGatewayWithError(ctx context.Context, err error) error

BadGatewayWithError wraps an existing error while creating a new empty client message and a BadGateway (502) return code

func (*Encoder) BadRequest

func (e *Encoder) BadRequest(ctx context.Context) error

BadRequest creates a new empty client message with a BadRequest (400) return code

func (*Encoder) BadRequestMessage

func (e *Encoder) BadRequestMessage(ctx context.Context, message string) error

BadRequestMessage creates a new client message with a BadRequest (400) return code

func (*Encoder) BadRequestMessageWithError

func (e *Encoder) BadRequestMessageWithError(ctx context.Context, err error, message string) error

BadRequestMessageWithError wraps an existing error while creating a new client message with a BadRequest (400) return code

func (*Encoder) BadRequestMessageWithErrorf

func (e *Encoder) BadRequestMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

BadRequestMessageWithErrorf wraps an existing error while creating a new client message with a BadRequest (400) return code

func (*Encoder) BadRequestMessagef

func (e *Encoder) BadRequestMessagef(ctx context.Context, format string, a ...any) error

BadRequestMessagef creates a new client message with a BadRequest (400) return code

func (*Encoder) BadRequestWithError

func (e *Encoder) BadRequestWithError(ctx context.Context, err error) error

BadRequestWithError wraps an existing error while creating a new empty client message and a BadRequest (400) return code

func (*Encoder) ClientClosedRequest

func (e *Encoder) ClientClosedRequest(ctx context.Context) error

ClientClosedRequest creates a new empty client message with a ClientClosedRequest (499) return code

func (*Encoder) ClientClosedRequestMessage

func (e *Encoder) ClientClosedRequestMessage(ctx context.Context, message string) error

ClientClosedRequestMessage creates a new client message with a ClientClosedRequest (499) return code

func (*Encoder) ClientClosedRequestMessageWithError

func (e *Encoder) ClientClosedRequestMessageWithError(ctx context.Context, err error, message string) error

ClientClosedRequestMessageWithError wraps an existing error while creating a new client message with a ClientClosedRequest (499) return code

func (*Encoder) ClientClosedRequestMessageWithErrorf

func (e *Encoder) ClientClosedRequestMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

ClientClosedRequestMessageWithErrorf wraps an existing error while creating a new client message with a ClientClosedRequest (499) return code

func (*Encoder) ClientClosedRequestMessagef

func (e *Encoder) ClientClosedRequestMessagef(ctx context.Context, format string, a ...any) error

ClientClosedRequestMessagef creates a new client message with a ClientClosedRequest (499) return code

func (*Encoder) ClientClosedRequestWithError

func (e *Encoder) ClientClosedRequestWithError(ctx context.Context, err error) error

ClientClosedRequestWithError wraps an existing error while creating a new empty client message and a ClientClosedRequest (499) return code

func (*Encoder) ClientMessage

func (e *Encoder) ClientMessage(ctx context.Context, err error) error

ClientMessage sets an http code and formats a client message based upon the message type found in the error chain. If no message type is found it defaults to InternalServerError (500) with no message

func (*Encoder) Conflict

func (e *Encoder) Conflict(ctx context.Context) error

Conflict creates a new empty client message with a Conflict (409) return code

func (*Encoder) ConflictMessage

func (e *Encoder) ConflictMessage(ctx context.Context, message string) error

ConflictMessage creates a new client message with a Conflict (409) return code

func (*Encoder) ConflictMessageWithError

func (e *Encoder) ConflictMessageWithError(ctx context.Context, err error, message string) error

ConflictMessageWithError wraps an existing error while creating a new client message with a Conflict (409) return code

func (*Encoder) ConflictMessageWithErrorf

func (e *Encoder) ConflictMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

ConflictMessageWithErrorf wraps an existing error while creating a new client message with a Conflict (409) return code

func (*Encoder) ConflictMessagef

func (e *Encoder) ConflictMessagef(ctx context.Context, format string, a ...any) error

ConflictMessagef creates a new client message with a Conflict (409) return code

func (*Encoder) ConflictWithError

func (e *Encoder) ConflictWithError(ctx context.Context, err error) error

ConflictWithError wraps an existing error while creating a new empty client message and a Conflict (409) return code

func (*Encoder) Forbidden

func (e *Encoder) Forbidden(ctx context.Context) error

Forbidden creates a new empty client message with a Forbidden (403) return code

func (*Encoder) ForbiddenMessage

func (e *Encoder) ForbiddenMessage(ctx context.Context, message string) error

ForbiddenMessage creates a new client message with a Forbidden (403) return code

func (*Encoder) ForbiddenMessageWithError

func (e *Encoder) ForbiddenMessageWithError(ctx context.Context, err error, message string) error

ForbiddenMessageWithError wraps an existing error while creating a new client message with a Forbidden (403) return code

func (*Encoder) ForbiddenMessageWithErrorf

func (e *Encoder) ForbiddenMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

ForbiddenMessageWithErrorf wraps an existing error while creating a new client message with a Forbidden (403) return code

func (*Encoder) ForbiddenMessagef

func (e *Encoder) ForbiddenMessagef(ctx context.Context, format string, a ...any) error

ForbiddenMessagef creates a new client message with a Forbidden (403) return code

func (*Encoder) ForbiddenWithError

func (e *Encoder) ForbiddenWithError(ctx context.Context, err error) error

ForbiddenWithError wraps an existing error while creating a new empty client message and a Forbidden (403) return code

func (*Encoder) GatewayTimeout

func (e *Encoder) GatewayTimeout(ctx context.Context) error

GatewayTimeout creates a new empty client message with a GatewayTimeout (504) return code

func (*Encoder) GatewayTimeoutMessage

func (e *Encoder) GatewayTimeoutMessage(ctx context.Context, message string) error

GatewayTimeoutMessage creates a new client message with a GatewayTimeout (504) return code

func (*Encoder) GatewayTimeoutMessageWithError

func (e *Encoder) GatewayTimeoutMessageWithError(ctx context.Context, err error, message string) error

GatewayTimeoutMessageWithError wraps an existing error while creating a new client message with a GatewayTimeout (504) return code

func (*Encoder) GatewayTimeoutMessageWithErrorf

func (e *Encoder) GatewayTimeoutMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

GatewayTimeoutMessageWithErrorf wraps an existing error while creating a new client message with a GatewayTimeout (504) return code

func (*Encoder) GatewayTimeoutMessagef

func (e *Encoder) GatewayTimeoutMessagef(ctx context.Context, format string, a ...any) error

GatewayTimeoutMessagef creates a new client message with a GatewayTimeout (504) return code

func (*Encoder) GatewayTimeoutWithError

func (e *Encoder) GatewayTimeoutWithError(ctx context.Context, err error) error

GatewayTimeoutWithError wraps an existing error while creating a new empty client message and a GatewayTimeout (504) return code

func (*Encoder) InternalServerError

func (e *Encoder) InternalServerError(ctx context.Context) error

InternalServerError creates a new empty client message with a InternalServerError (500) return code

func (*Encoder) InternalServerErrorMessage

func (e *Encoder) InternalServerErrorMessage(ctx context.Context, message string) error

InternalServerErrorMessage creates a new client message with a InternalServerError (500) return code

func (*Encoder) InternalServerErrorMessageWithError

func (e *Encoder) InternalServerErrorMessageWithError(ctx context.Context, err error, message string) error

InternalServerErrorMessageWithError wraps an existing error while creating a new client message with a InternalServerError (500) return code

func (*Encoder) InternalServerErrorMessageWithErrorf

func (e *Encoder) InternalServerErrorMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

InternalServerErrorMessageWithErrorf wraps an existing error while creating a new client message with a InternalServerError (500) return code

func (*Encoder) InternalServerErrorMessagef

func (e *Encoder) InternalServerErrorMessagef(ctx context.Context, format string, a ...any) error

InternalServerErrorMessagef creates a new client message with a InternalServerError (500) return code

func (*Encoder) InternalServerErrorWithError

func (e *Encoder) InternalServerErrorWithError(ctx context.Context, err error) error

InternalServerErrorWithError wraps an existing error while creating a new empty client message and a InternalServerError (500) return code

func (*Encoder) MethodNotAllowed

func (e *Encoder) MethodNotAllowed(ctx context.Context) error

MethodNotAllowed creates a new empty client message with a MethodNotAllowed (405) return code

func (*Encoder) MethodNotAllowedMessage

func (e *Encoder) MethodNotAllowedMessage(ctx context.Context, message string) error

MethodNotAllowedMessage creates a new client message with a MethodNotAllowed (405) return code

func (*Encoder) MethodNotAllowedMessageWithError

func (e *Encoder) MethodNotAllowedMessageWithError(ctx context.Context, err error, message string) error

MethodNotAllowedMessageWithError wraps an existing error while creating a new client message with a MethodNotAllowed (405) return code

func (*Encoder) MethodNotAllowedMessageWithErrorf

func (e *Encoder) MethodNotAllowedMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

MethodNotAllowedMessageWithErrorf wraps an existing error while creating a new client message with a MethodNotAllowed (405) return code

func (*Encoder) MethodNotAllowedMessagef

func (e *Encoder) MethodNotAllowedMessagef(ctx context.Context, format string, a ...any) error

MethodNotAllowedMessagef creates a new client message with a MethodNotAllowed (405) return code

func (*Encoder) MethodNotAllowedWithError

func (e *Encoder) MethodNotAllowedWithError(ctx context.Context, err error) error

MethodNotAllowedWithError wraps an existing error while creating a new empty client message and a MethodNotAllowed (405) return code

func (*Encoder) NotAcceptable

func (e *Encoder) NotAcceptable(ctx context.Context) error

NotAcceptable creates a new empty client message with a NotAcceptable (406) return code

func (*Encoder) NotAcceptableMessage

func (e *Encoder) NotAcceptableMessage(ctx context.Context, message string) error

NotAcceptableMessage creates a new client message with a NotAcceptable (406) return code

func (*Encoder) NotAcceptableMessageWithError

func (e *Encoder) NotAcceptableMessageWithError(ctx context.Context, err error, message string) error

NotAcceptableMessageWithError wraps an existing error while creating a new client message with a NotAcceptable (406) return code

func (*Encoder) NotAcceptableMessageWithErrorf

func (e *Encoder) NotAcceptableMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

NotAcceptableMessageWithErrorf wraps an existing error while creating a new client message with a NotAcceptable (406) return code

func (*Encoder) NotAcceptableMessagef

func (e *Encoder) NotAcceptableMessagef(ctx context.Context, format string, a ...any) error

NotAcceptableMessagef creates a new client message with a NotAcceptable (406) return code

func (*Encoder) NotAcceptableWithError

func (e *Encoder) NotAcceptableWithError(ctx context.Context, err error) error

NotAcceptableWithError wraps an existing error while creating a new empty client message and a NotAcceptable (406) return code

func (*Encoder) NotFound

func (e *Encoder) NotFound(ctx context.Context) error

NotFound creates a new empty client message with a NotFound (404) return code

func (*Encoder) NotFoundMessage

func (e *Encoder) NotFoundMessage(ctx context.Context, message string) error

NotFoundMessage creates a new client message with a NotFound (404) return code

func (*Encoder) NotFoundMessageWithError

func (e *Encoder) NotFoundMessageWithError(ctx context.Context, err error, message string) error

NotFoundMessageWithError wraps an existing error while creating a new client message with a NotFound (404) return code

func (*Encoder) NotFoundMessageWithErrorf

func (e *Encoder) NotFoundMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

NotFoundMessageWithErrorf wraps an existing error while creating a new client message with a NotFound (404) return code

func (*Encoder) NotFoundMessagef

func (e *Encoder) NotFoundMessagef(ctx context.Context, format string, a ...any) error

NotFoundMessagef creates a new client message with a NotFound (404) return code

func (*Encoder) NotFoundWithError

func (e *Encoder) NotFoundWithError(ctx context.Context, err error) error

NotFoundWithError wraps an existing error while creating a new empty client message and a NotFound (404) return code

func (*Encoder) NotImplemented

func (e *Encoder) NotImplemented(ctx context.Context) error

NotImplemented creates a new empty client message with a NotImplemented (501) return code

func (*Encoder) NotImplementedMessage

func (e *Encoder) NotImplementedMessage(ctx context.Context, message string) error

NotImplementedMessage creates a new client message with a NotImplemented (501) return code

func (*Encoder) NotImplementedMessageWithError

func (e *Encoder) NotImplementedMessageWithError(ctx context.Context, err error, message string) error

NotImplementedMessageWithError wraps an existing error while creating a new client message with a NotImplemented (501) return code

func (*Encoder) NotImplementedMessageWithErrorf

func (e *Encoder) NotImplementedMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

NotImplementedMessageWithErrorf wraps an existing error while creating a new client message with a NotImplemented (501) return code

func (*Encoder) NotImplementedMessagef

func (e *Encoder) NotImplementedMessagef(ctx context.Context, format string, a ...any) error

NotImplementedMessagef creates a new client message with a NotImplemented (501) return code

func (*Encoder) NotImplementedWithError

func (e *Encoder) NotImplementedWithError(ctx context.Context, err error) error

NotImplementedWithError wraps an existing error while creating a new empty client message and a NotImplemented (501) return code

func (*Encoder) Ok

func (e *Encoder) Ok(body interface{}) error

Ok returns a default http 200 status response with a body

func (*Encoder) RequestEntityTooLarge

func (e *Encoder) RequestEntityTooLarge(ctx context.Context) error

RequestEntityTooLarge creates a new empty client message with a RequestEntityTooLarge (413) return code

func (*Encoder) RequestEntityTooLargeMessage

func (e *Encoder) RequestEntityTooLargeMessage(ctx context.Context, message string) error

RequestEntityTooLargeMessage creates a new client message with a RequestEntityTooLarge (413) return code

func (*Encoder) RequestEntityTooLargeMessageWithError

func (e *Encoder) RequestEntityTooLargeMessageWithError(ctx context.Context, err error, message string) error

RequestEntityTooLargeMessageWithError wraps an existing error while creating a new client message with a RequestEntityTooLarge (413) return code

func (*Encoder) RequestEntityTooLargeMessageWithErrorf

func (e *Encoder) RequestEntityTooLargeMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

RequestEntityTooLargeMessageWithErrorf wraps an existing error while creating a new client message with a RequestEntityTooLarge (413) return code

func (*Encoder) RequestEntityTooLargeMessagef

func (e *Encoder) RequestEntityTooLargeMessagef(ctx context.Context, format string, a ...any) error

RequestEntityTooLargeMessagef creates a new client message with a RequestEntityTooLarge (413) return code

func (*Encoder) RequestEntityTooLargeWithError

func (e *Encoder) RequestEntityTooLargeWithError(ctx context.Context, err error) error

RequestEntityTooLargeWithError wraps an existing error while creating a new empty client message and a RequestEntityTooLarge (413) return code

func (*Encoder) RequestTimeout

func (e *Encoder) RequestTimeout(ctx context.Context) error

RequestTimeout creates a new empty client message with a RequestTimeout (408) return code

func (*Encoder) RequestTimeoutMessage

func (e *Encoder) RequestTimeoutMessage(ctx context.Context, message string) error

RequestTimeoutMessage creates a new client message with a RequestTimeout (408) return code

func (*Encoder) RequestTimeoutMessageWithError

func (e *Encoder) RequestTimeoutMessageWithError(ctx context.Context, err error, message string) error

RequestTimeoutMessageWithError wraps an existing error while creating a new client message with a RequestTimeout (408) return code

func (*Encoder) RequestTimeoutMessageWithErrorf

func (e *Encoder) RequestTimeoutMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

RequestTimeoutMessageWithErrorf wraps an existing error while creating a new client message with a RequestTimeout (408) return code

func (*Encoder) RequestTimeoutMessagef

func (e *Encoder) RequestTimeoutMessagef(ctx context.Context, format string, a ...any) error

RequestTimeoutMessagef creates a new client message with a RequestTimeout (408) return code

func (*Encoder) RequestTimeoutWithError

func (e *Encoder) RequestTimeoutWithError(ctx context.Context, err error) error

RequestTimeoutWithError wraps an existing error while creating a new empty client message and a RequestTimeout (408) return code

func (*Encoder) ServiceUnavailable

func (e *Encoder) ServiceUnavailable(ctx context.Context) error

ServiceUnavailable creates a new empty client message with a ServiceUnavailable (503) return code

func (*Encoder) ServiceUnavailableMessage

func (e *Encoder) ServiceUnavailableMessage(ctx context.Context, message string) error

ServiceUnavailableMessage creates a new client message with a ServiceUnavailable (503) return code

func (*Encoder) ServiceUnavailableMessageWithError

func (e *Encoder) ServiceUnavailableMessageWithError(ctx context.Context, err error, message string) error

ServiceUnavailableMessageWithError wraps an existing error while creating a new client message with a ServiceUnavailable (503) return code

func (*Encoder) ServiceUnavailableMessageWithErrorf

func (e *Encoder) ServiceUnavailableMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

ServiceUnavailableMessageWithErrorf wraps an existing error while creating a new client message with a ServiceUnavailable (503) return code

func (*Encoder) ServiceUnavailableMessagef

func (e *Encoder) ServiceUnavailableMessagef(ctx context.Context, format string, a ...any) error

ServiceUnavailableMessagef creates a new client message with a ServiceUnavailable (503) return code

func (*Encoder) ServiceUnavailableWithError

func (e *Encoder) ServiceUnavailableWithError(ctx context.Context, err error) error

ServiceUnavailableWithError wraps an existing error while creating a new empty client message and a ServiceUnavailable (503) return code

func (*Encoder) StatusCodeWithBody

func (e *Encoder) StatusCodeWithBody(statusCode int, body interface{}) error

StatusCodeWithBody writes a statusCode and body

func (*Encoder) TooManyRequests

func (e *Encoder) TooManyRequests(ctx context.Context) error

TooManyRequests creates a new empty client message with a TooManyRequests (429) return code

func (*Encoder) TooManyRequestsMessage

func (e *Encoder) TooManyRequestsMessage(ctx context.Context, message string) error

TooManyRequestsMessage creates a new client message with a TooManyRequests (429) return code

func (*Encoder) TooManyRequestsMessageWithError

func (e *Encoder) TooManyRequestsMessageWithError(ctx context.Context, err error, message string) error

TooManyRequestsMessageWithError wraps an existing error while creating a new client message with a TooManyRequests (429) return code

func (*Encoder) TooManyRequestsMessageWithErrorf

func (e *Encoder) TooManyRequestsMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

TooManyRequestsMessageWithErrorf wraps an existing error while creating a new client message with a TooManyRequests (429) return code

func (*Encoder) TooManyRequestsMessagef

func (e *Encoder) TooManyRequestsMessagef(ctx context.Context, format string, a ...any) error

TooManyRequestsMessagef creates a new client message with a TooManyRequests (429) return code

func (*Encoder) TooManyRequestsWithError

func (e *Encoder) TooManyRequestsWithError(ctx context.Context, err error) error

TooManyRequestsWithError wraps an existing error while creating a new empty client message and a TooManyRequests (429) return code

func (*Encoder) Unauthorized

func (e *Encoder) Unauthorized(ctx context.Context) error

Unauthorized creates a new empty client message with a Unauthorized (401) return code

func (*Encoder) UnauthorizedMessage

func (e *Encoder) UnauthorizedMessage(ctx context.Context, message string) error

UnauthorizedMessage creates a new client message with a Unauthorized (401) return code

func (*Encoder) UnauthorizedMessageWithError

func (e *Encoder) UnauthorizedMessageWithError(ctx context.Context, err error, message string) error

UnauthorizedMessageWithError wraps an existing error while creating a new client message with a Unauthorized (401) return code

func (*Encoder) UnauthorizedMessageWithErrorf

func (e *Encoder) UnauthorizedMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

UnauthorizedMessageWithErrorf wraps an existing error while creating a new client message with a Unauthorized (401) return code

func (*Encoder) UnauthorizedMessagef

func (e *Encoder) UnauthorizedMessagef(ctx context.Context, format string, a ...any) error

UnauthorizedMessagef creates a new client message with a Unauthorized (401) return code

func (*Encoder) UnauthorizedWithError

func (e *Encoder) UnauthorizedWithError(ctx context.Context, err error) error

UnauthorizedWithError wraps an existing error while creating a new empty client message and a Unauthorized (401) return code

func (*Encoder) UnprocessableEntity

func (e *Encoder) UnprocessableEntity(ctx context.Context) error

UnprocessableEntity creates a new empty client message with a UnprocessableEntity (422) return code

func (*Encoder) UnprocessableEntityMessage

func (e *Encoder) UnprocessableEntityMessage(ctx context.Context, message string) error

UnprocessableEntityMessage creates a new client message with a UnprocessableEntity (422) return code

func (*Encoder) UnprocessableEntityMessageWithError

func (e *Encoder) UnprocessableEntityMessageWithError(ctx context.Context, err error, message string) error

UnprocessableEntityMessageWithError wraps an existing error while creating a new client message with a UnprocessableEntity (422) return code

func (*Encoder) UnprocessableEntityMessageWithErrorf

func (e *Encoder) UnprocessableEntityMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

UnprocessableEntityMessageWithErrorf wraps an existing error while creating a new client message with a UnprocessableEntity (422) return code

func (*Encoder) UnprocessableEntityMessagef

func (e *Encoder) UnprocessableEntityMessagef(ctx context.Context, format string, a ...any) error

UnprocessableEntityMessagef creates a new client message with a UnprocessableEntity (422) return code

func (*Encoder) UnprocessableEntityWithError

func (e *Encoder) UnprocessableEntityWithError(ctx context.Context, err error) error

UnprocessableEntityWithError wraps an existing error while creating a new empty client message and a UnprocessableEntity (422) return code

func (*Encoder) UnsupportedMediaType

func (e *Encoder) UnsupportedMediaType(ctx context.Context) error

UnsupportedMediaType creates a new empty client message with a UnsupportedMediaType (415) return code

func (*Encoder) UnsupportedMediaTypeMessage

func (e *Encoder) UnsupportedMediaTypeMessage(ctx context.Context, message string) error

UnsupportedMediaTypeMessage creates a new client message with a UnsupportedMediaType (415) return code

func (*Encoder) UnsupportedMediaTypeMessageWithError

func (e *Encoder) UnsupportedMediaTypeMessageWithError(ctx context.Context, err error, message string) error

UnsupportedMediaTypeMessageWithError wraps an existing error while creating a new client message with a UnsupportedMediaType (415) return code

func (*Encoder) UnsupportedMediaTypeMessageWithErrorf

func (e *Encoder) UnsupportedMediaTypeMessageWithErrorf(ctx context.Context, err error, format string, a ...any) error

UnsupportedMediaTypeMessageWithErrorf wraps an existing error while creating a new client message with a UnsupportedMediaType (415) return code

func (*Encoder) UnsupportedMediaTypeMessagef

func (e *Encoder) UnsupportedMediaTypeMessagef(ctx context.Context, format string, a ...any) error

UnsupportedMediaTypeMessagef creates a new client message with a UnsupportedMediaType (415) return code

func (*Encoder) UnsupportedMediaTypeWithError

func (e *Encoder) UnsupportedMediaTypeWithError(ctx context.Context, err error) error

UnsupportedMediaTypeWithError wraps an existing error while creating a new empty client message and a UnsupportedMediaType (415) return code

type HTTPEncoder

type HTTPEncoder interface {
	// Encode is the call that is made to encode data into a response body and returns an error if it fails
	Encode(v interface{}) error
}

HTTPEncoder is an interface that is accepted when encoding http responses

type MessageResponse

type MessageResponse struct {
	Message string `json:"message,omitempty"`
	TraceID string `json:"traceId,omitempty"`
}

MessageResponse holds a standard structure for http responses that carry a single message This also includes a trace ID for debugging purposes

type ParamType

type ParamType string

ParamType defines the type used to describe url Params

type StructDecoder

type StructDecoder[Request any] struct {
	// contains filtered or unexported fields
}

StructDecoder decodes a JSON request body into a plain Request struct and validates it. The body is read once and must be a JSON object. Every key it names must reach a field of Request: a field with a json tag answers to that tag only; a field without one answers to its Go name, exactly or fully lower-cased. A key no field answers to is refused with 400 "invalid field in json - <key>", two keys reaching one field with 400 "json field name <field> collides with another field name of different case", and a JSON null with 400 "<key> cannot be null" unless the field is a pointer, which stays nil. A body that is not a JSON object answers 400 "failed to decode request body"; a value the field's type cannot hold, 400 "failed to unmarshal request body". With a validator (WithValidator), a PATCH validates only the fields the body carried (StructPartial, in struct order) and every other method validates the whole struct (Struct); a rejection answers 400 "failed validating the request". Every refusal is a ClientMessage, written by Encoder.ClientMessage.

A request body bound to a resource decodes with the resource package's decoders, which add what the generated struct tags declare (permissions, sizing, nullability, former names); a plain request body decodes here.

func NewStructDecoder

func NewStructDecoder[Request any]() (*StructDecoder[Request], error)

NewStructDecoder creates a StructDecoder for Request, which must be a struct whose wire names are distinct.

func (*StructDecoder[Request]) Decode

func (d *StructDecoder[Request]) Decode(request *http.Request) (*Request, error)

Decode decodes the request body into a new Request and validates it.

func (*StructDecoder[Request]) WithValidator

func (d *StructDecoder[Request]) WithValidator(v ValidatorFunc) *StructDecoder[Request]

WithValidator returns a copy of the decoder that validates every decoded body with v.

type ValidatorFunc

type ValidatorFunc interface {
	Struct(s any) error
	StructPartial(s any, fields ...string) error
}

ValidatorFunc validates a struct, in whole (Struct) or in the named fields only (StructPartial). Each method returns an error when validation fails. A *validator.Validate from github.com/go-playground/validator satisfies it.

Jump to

Keyboard shortcuts

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