responders

package
v0.0.0-...-8e8e47b Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(writer http.ResponseWriter, err error, opts ...Option)

Error responds to an HTTP requests with an ErrorResponse. It tries to match it to a known error type so it can return its corresponding status and message. It defaults to HTTP 500 internal server error. An error is returned if there was an error writing the response.

func JSON

func JSON[RequestParameters any, ResponseBody any](writer http.ResponseWriter, request *http.Request, callback func(*RequestParameters) (*ResponseBody, int, error), opts ...Option)

JSON responds to an HTTP request by encoding the response as JSON. An error is returned if there was an error writing the response.

func JSONStream

func JSONStream[RequestParameters any, ResponseBody any](writer http.ResponseWriter, request *http.Request, callback func(*RequestParameters) (<-chan *ResponseBody, int, error), opts ...Option)

JSONStream responds to an HTTP request by streaming responses as JSON objects. The producer is responsible for closing the response channel. An error is returned if there was an error writing the response.

func MustRegisterErrorResponse

func MustRegisterErrorResponse[T any, R any](status int, callback func(err *T) *R)

MustRegisterErrorResponse allows error types to be registered for the Error responder. The registered error type should always be instantiated as a pointer for this to work correctly.

func Status

func Status[RequestParameters any](writer http.ResponseWriter, request *http.Request, callback func(*RequestParameters) (int, error), opts ...Option)

Status responds to an HTTP request with a status but no response body. An error is returned if there was an error writing the response.

Types

type Option

type Option func(*config)

Option configures the responders.

func WithErrorCallback

func WithErrorCallback(callback func(error)) Option

WithErrorCallback configures the responder to invoke this callback when there's a write error.

type StandardErrorResponse

type StandardErrorResponse struct {
	Message string `json:"message"`
}

StandardErrorResponse is the standard JSON response an API endpoint makes when an unknown error occurs in the endpoint handler.

Jump to

Keyboard shortcuts

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