Documentation ¶
Index ¶
- func Error(writer http.ResponseWriter, err error, opts ...Option)
- func JSON[RequestParameters any, ResponseBody any](writer http.ResponseWriter, request *http.Request, ...)
- func JSONStream[RequestParameters any, ResponseBody any](writer http.ResponseWriter, request *http.Request, ...)
- func MustRegisterErrorResponse[T any, R any](status int, callback func(err *T) *R)
- func Status[RequestParameters any](writer http.ResponseWriter, request *http.Request, ...)
- type Option
- type StandardErrorResponse
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 ¶
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.
Types ¶
type Option ¶
type Option func(*config)
Option configures the responders.
func WithErrorCallback ¶
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.