Documentation
¶
Overview ¶
Package httputil provides shared HTTP response helpers for writing JSON payloads and standardized, correlation-ID-tagged API error bodies.
Index ¶
- Variables
- func CorrelationID(r *http.Request) string
- func Error(w http.ResponseWriter, r *http.Request, status int, message string)
- func InternalServerError(w http.ResponseWriter, r *http.Request, logMessage string, err error, ...)
- func JSON(w http.ResponseWriter, status int, payload any)
- type ErrorResponse
Constants ¶
This section is empty.
Variables ¶
var CorrelationIDFunc func(context.Context) string
CorrelationIDFunc optionally extracts a correlation ID from a request context for inclusion in Error/InternalServerError responses. It is nil by default, in which case CorrelationID returns "". A consumer that wants correlation IDs wires in an extractor of its choosing, e.g.:
httputil.CorrelationIDFunc = trace.GetTraceID
Functions ¶
func CorrelationID ¶
CorrelationID returns the request's correlation ID via CorrelationIDFunc, or "" if CorrelationIDFunc is unset. This is the same value included in the correlationId field of Error/InternalServerError responses.
func InternalServerError ¶
func InternalServerError(w http.ResponseWriter, r *http.Request, logMessage string, err error, attrs ...any)
InternalServerError logs err server-side and responds with a generic, safe message to the client.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
CorrelationID string `json:"correlationId,omitempty"`
}
ErrorResponse is the standard JSON shape for API error bodies.