responders

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package responders provides HTTP response helpers for JSON APIs.

Use this package to handle the common patterns of decoding request parameters, invoking a callback, and writing the response. The responders automatically handle parameter validation, JSON encoding, and error responses.

The JSON responder encodes a response body as JSON. The JSONStream responder streams multiple JSON objects using chunked transfer encoding. The Status responder returns only an HTTP status code without a body. The Error responder converts errors to appropriate HTTP status codes and JSON error messages.

Custom error types can be registered with MustRegisterErrorResponse to map specific error types to HTTP status codes and formatted error messages. Unregistered errors default to HTTP 500 Internal Server Error.

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.

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.

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.

func MustRegisterErrorResponse

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

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.

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 a responder processing error occurs. Do not retry any HTTP responder when this is invoked.

func WithJSONMarshal

func WithJSONMarshal(marshal func(any) ([]byte, error)) Option

WithJSONMarshal configures a JSON marshal function for testing.

type StandardErrorResponse

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

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

Jump to

Keyboard shortcuts

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