Documentation
¶
Overview ¶
Package response writes bounded JSON envelopes using native net/http.
Index ¶
- Variables
- func Accepted(w http.ResponseWriter, data any) error
- func BadGateway(w http.ResponseWriter, message string) error
- func BadRequest(w http.ResponseWriter, message string) error
- func Conflict(w http.ResponseWriter, message string) error
- func Created(w http.ResponseWriter, data any) error
- func Error(w http.ResponseWriter, r *http.Request, err error, options ...Options) error
- func Forbidden(w http.ResponseWriter, message string) error
- func GatewayTimeout(w http.ResponseWriter, message string) error
- func Gone(w http.ResponseWriter, message string) error
- func InternalServerError(w http.ResponseWriter, message string) error
- func JSON(w http.ResponseWriter, status int, v any, options ...Options) error
- func MethodNotAllowed(w http.ResponseWriter, message string) error
- func NoContent(w http.ResponseWriter) error
- func NotFound(w http.ResponseWriter, message string) error
- func OK(w http.ResponseWriter, data any) error
- func Paginated(w http.ResponseWriter, data, meta any) error
- func ServiceUnavailable(w http.ResponseWriter, message string) error
- func TooManyRequests(w http.ResponseWriter, message string) error
- func Unauthorized(w http.ResponseWriter, message string) error
- func UnprocessableEntity(w http.ResponseWriter, message string) error
- type DebugPolicy
- type Envelope
- type Options
- type Writer
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
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 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) WriteHeader ¶
WriteHeader records and writes the HTTP status once.