response

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package response writes bounded JSON envelopes using native net/http.

Index

Constants

This section is empty.

Variables

View Source
var ErrWritten = errors.New("response: response already committed")

Functions

func Accepted

func Accepted(w http.ResponseWriter, data any) error

Accepted writes an HTTP 202 success envelope.

func BadGateway

func BadGateway(w http.ResponseWriter, message string) error

BadGateway writes the corresponding HTTP error envelope with an approved public message.

func BadRequest

func BadRequest(w http.ResponseWriter, message string) error

BadRequest writes the corresponding HTTP error envelope with an approved public message.

func Conflict

func Conflict(w http.ResponseWriter, message string) error

Conflict writes the corresponding HTTP error envelope with an approved public message.

func Created

func Created(w http.ResponseWriter, data any) error

Created writes an HTTP 201 success envelope.

func Error

func Error(w http.ResponseWriter, r *http.Request, err error, options ...Options) error

Error maps an error to a safe public HTTP envelope under explicit diagnostic policy.

func Forbidden

func Forbidden(w http.ResponseWriter, message string) error

Forbidden writes the corresponding HTTP error envelope with an approved public message.

func GatewayTimeout

func GatewayTimeout(w http.ResponseWriter, message string) error

GatewayTimeout writes the corresponding HTTP error envelope with an approved public message.

func Gone

func Gone(w http.ResponseWriter, message string) error

Gone writes the corresponding HTTP error envelope with an approved public message.

func InternalServerError

func InternalServerError(w http.ResponseWriter, message string) error

InternalServerError writes the corresponding HTTP error envelope with an approved public message.

func JSON

func JSON(w http.ResponseWriter, status int, v any, options ...Options) error

JSON writes or processes a bounded JSON representation; see package documentation for ownership.

func MethodNotAllowed

func MethodNotAllowed(w http.ResponseWriter, message string) error

MethodNotAllowed writes the corresponding HTTP error envelope with an approved public message.

func NoContent

func NoContent(w http.ResponseWriter) error

NoContent writes HTTP 204 without a response body.

func NotFound

func NotFound(w http.ResponseWriter, message string) error

NotFound writes the corresponding HTTP error envelope with an approved public message.

func OK

func OK(w http.ResponseWriter, data any) error

OK writes an HTTP 200 success envelope.

func Paginated

func Paginated(w http.ResponseWriter, data, meta any) error

Paginated writes a success envelope with pagination metadata.

func ServiceUnavailable

func ServiceUnavailable(w http.ResponseWriter, message string) error

ServiceUnavailable writes the corresponding HTTP error envelope with an approved public message.

func TooManyRequests

func TooManyRequests(w http.ResponseWriter, message string) error

TooManyRequests writes the corresponding HTTP error envelope with an approved public message.

func Unauthorized

func Unauthorized(w http.ResponseWriter, message string) error

Unauthorized writes the corresponding HTTP error envelope with an approved public message.

func UnprocessableEntity

func UnprocessableEntity(w http.ResponseWriter, message string) error

UnprocessableEntity writes the corresponding HTTP error envelope with an approved public message.

Types

type DebugPolicy

type DebugPolicy int

DebugPolicy controls explicit server-authorized diagnostic exposure.

const (
	Never DebugPolicy = iota
	DevelopmentOnly
	AuthorizedInternal
)

type Envelope

type Envelope struct {
	Success       bool      `json:"success"`
	Code          string    `json:"code"`
	Message       string    `json:"message"`
	Data          any       `json:"data,omitempty"`
	Error         any       `json:"error,omitempty"`
	Meta          any       `json:"meta,omitempty"`
	TraceID       string    `json:"trace_id,omitempty"`
	CorrelationID string    `json:"correlation_id,omitempty"`
	Timestamp     time.Time `json:"timestamp"`
}

Envelope contains public JSON response data and correlation metadata.

type Options

type Options struct {
	MaxBytes       int
	Debug          DebugPolicy
	Development    bool
	AuthorizeDebug func(*http.Request) bool
	SanitizeDebug  func(string) string
	EscapeHTML     *bool
}

Options controls explicit loading or response policy.

type Writer

type Writer struct {
	http.ResponseWriter
	Written bool
	Status  int
}

Writer tracks commitment when shared by every component writing a response.

func (*Writer) Unwrap

func (w *Writer) Unwrap() http.ResponseWriter

Unwrap returns the underlying wrapped resource or error cause.

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Write implements the wrapped HTTP writer with commitment tracking.

func (*Writer) WriteHeader

func (w *Writer) WriteHeader(status int)

WriteHeader records and writes the HTTP status once.

Jump to

Keyboard shortcuts

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