httpx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package httpx provides small, framework-neutral helpers for JSON HTTP handlers: a standard error envelope, safe error responses that never echo internal error strings to clients, bounded JSON decoding, and clamped pagination parsing. Everything operates on net/http types; framework veneers (e.g. Gin) wrap these.

Index

Examples

Constants

View Source
const (
	DefaultPageLimit = 20
	MaxPageLimit     = 100
)

Pagination defaults; override per call with PaginationWith.

View Source
const MaxJSONBody = 1 << 20 // 1 MiB

MaxJSONBody is the default request-body cap for DecodeJSON.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(w http.ResponseWriter, r *http.Request, dst any) error

DecodeJSON decodes the request body into dst, capping the body at MaxJSONBody and rejecting trailing garbage. The returned error is safe to show to clients.

func WriteError

func WriteError(w http.ResponseWriter, status int, message string)

WriteError writes the standard error envelope. The message is sent to the client verbatim, so it must be a public-safe description — never pass err.Error() from an internal error here; use WriteInternalError instead.

Example

WriteError sends a client-facing message. Internal error text never reaches the response body: use WriteInternalError for that, which logs the cause and tells the client nothing beyond the status.

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/dobrevit/svckit/httpx"
)

func main() {
	w := httptest.NewRecorder()

	httpx.WriteError(w, http.StatusNotFound, "order not found")

	fmt.Println(w.Code)
	fmt.Println(w.Body.String())
}
Output:
404
{"error":"order not found"}

func WriteErrorCode

func WriteErrorCode(w http.ResponseWriter, status int, message, code string)

WriteErrorCode writes the standard error envelope with a machine-readable code.

func WriteInternalError

func WriteInternalError(w http.ResponseWriter)

WriteInternalError writes the canonical 500 response without exposing any internal detail. Log the underlying error at the call site.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON writes v as JSON with the given status code.

Types

type ErrorBody

type ErrorBody struct {
	Error   string `json:"error"`
	Code    string `json:"code,omitempty"`
	Details any    `json:"details,omitempty"`
}

ErrorBody is the standard JSON error envelope.

type Page

type Page struct {
	Limit  int
	Offset int
}

Page is a parsed, clamped limit/offset pair.

func Pagination

func Pagination(r *http.Request) Page

Pagination parses the "limit" and "offset" query parameters with the package defaults: limit defaults to DefaultPageLimit and is clamped to [1, MaxPageLimit]; offset defaults to 0 and is clamped to >= 0.

Example

Pagination clamps the caller's request into a range the handler can serve, so an unbounded ?limit= cannot ask for the whole table.

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/dobrevit/svckit/httpx"
)

func main() {
	r := httptest.NewRequest(http.MethodGet, "/orders?limit=100000&offset=40", nil)

	page := httpx.Pagination(r)

	fmt.Printf("limit=%d offset=%d\n", page.Limit, page.Offset)
}
Output:
limit=100 offset=40

func PaginationWith

func PaginationWith(r *http.Request, defaultLimit, maxLimit int) Page

PaginationWith parses pagination with a custom default and maximum limit.

Jump to

Keyboard shortcuts

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