Documentation
¶
Index ¶
- func BuildInsertArgs(body map[string]any) (cols, placeholders string, args []any)
- func CachedGet(next http.HandlerFunc) http.HandlerFunc
- func FilterFields(record map[string]any, allowed []string) map[string]any
- func IsServerError(err error) bool
- func RowsToMaps(rows pgx.Rows) ([]map[string]any, error)
- func WriteDBError(w http.ResponseWriter, err error)
- func WriteJSON(w http.ResponseWriter, v any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildInsertArgs ¶
BuildInsertArgs builds the column list, placeholder list ($1,$2…), and args slice from a JSON-decoded body map. Keys are sorted for deterministic output and each column identifier is quoted with pgx.Identifier.Sanitize so a key can never break out of the identifier position (defence-in-depth alongside ValidateWritableColumns).
func CachedGet ¶
func CachedGet(next http.HandlerFunc) http.HandlerFunc
CachedGet wraps a GET handler to add ETag and Cache-Control headers. If the request includes an If-None-Match header that matches the computed ETag, it returns 304 Not Modified with no body. Only 200 responses are cached.
func FilterFields ¶
FilterFields returns a copy of record containing only the keys in allowed. If allowed is empty, record is returned unchanged.
func IsServerError ¶
IsServerError reports whether err would be answered as HTTP 500 by WriteDBError (i.e. it is not a missing-tenant / bad-input / unavailable error). Used to decide whether to capture a stack trace — client/availability errors are not bugs.
func RowsToMaps ¶
RowsToMaps converts pgx.Rows to a slice of string-keyed maps. UUID columns ([16]byte) are converted to hyphenated UUID strings.
func WriteDBError ¶
func WriteDBError(w http.ResponseWriter, err error)
WriteDBError maps a database error to an HTTP response without leaking internal details (raw SQL, schema names) to the client:
- missing tenant schema/relation (42P01/3F000) → 400 "invalid tenant"
- caller-supplied bad input (22P02, e.g. a non-UUID id) → 400 "invalid request"
- unknown column on a write (42703) → 422 validation_failed/unknown_field
- unreachable database (timeout / open breaker) → 503 "service unavailable"
- anything else → 500 "internal error"
Types ¶
This section is empty.