Documentation
¶
Overview ¶
Package httputil provides shared HTTP response helpers. All JSON responses, standard error shapes, and named error helpers live here so every Lambda function and HTTP server writes consistent, structured responses without duplicating boilerplate.
Index ¶
- func WriteBadRequest(w http.ResponseWriter, message, requestID string)
- func WriteConflict(w http.ResponseWriter, message, requestID string)
- func WriteError(w http.ResponseWriter, status int, code, message, requestID string)
- func WriteForbidden(w http.ResponseWriter, requestID string)
- func WriteInternalError(w http.ResponseWriter, requestID string)
- func WriteJSON(w http.ResponseWriter, status int, data any)
- func WriteMethodNotAllowed(w http.ResponseWriter, requestID string)
- func WriteNotFound(w http.ResponseWriter, requestID string)
- func WriteTooManyRequests(w http.ResponseWriter, requestID string)
- func WriteUnauthorized(w http.ResponseWriter, requestID string)
- type ErrorResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteBadRequest ¶
func WriteBadRequest(w http.ResponseWriter, message, requestID string)
WriteBadRequest writes a 400 Bad Request response. The caller supplies the message so callers can describe which field or value was invalid.
func WriteConflict ¶
func WriteConflict(w http.ResponseWriter, message, requestID string)
WriteConflict writes a 409 Conflict response. The caller supplies the message so callers can describe the specific conflict.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, code, message, requestID string)
WriteError writes a JSON error response with the provided HTTP status, error code, human-readable message, and optional request ID.
func WriteForbidden ¶
func WriteForbidden(w http.ResponseWriter, requestID string)
WriteForbidden writes a 403 Forbidden response.
func WriteInternalError ¶
func WriteInternalError(w http.ResponseWriter, requestID string)
WriteInternalError writes a 500 Internal Server Error response.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, data any)
WriteJSON serializes data as JSON and writes it to w with the given HTTP status. Content-Type is always set to application/json. If marshaling fails, a 500 with a static error body is written instead — the original status is discarded.
func WriteMethodNotAllowed ¶
func WriteMethodNotAllowed(w http.ResponseWriter, requestID string)
WriteMethodNotAllowed writes a 405 Method Not Allowed response.
func WriteNotFound ¶
func WriteNotFound(w http.ResponseWriter, requestID string)
WriteNotFound writes a 404 Not Found response using the standard error code.
func WriteTooManyRequests ¶
func WriteTooManyRequests(w http.ResponseWriter, requestID string)
WriteTooManyRequests writes a 429 Too Many Requests response.
func WriteUnauthorized ¶
func WriteUnauthorized(w http.ResponseWriter, requestID string)
WriteUnauthorized writes a 401 Unauthorized response.
Types ¶
type ErrorResponse ¶
type ErrorResponse struct {
Error string `json:"error"`
Code string `json:"code"`
RequestID string `json:"request_id,omitempty"`
}
ErrorResponse is the standard error shape returned by API endpoints. Consumers should check the Code field for machine-readable error classification.