httpx

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package httpx provides gin-kit httpx implementation support.

Index

Constants

View Source
const LoggerKey = "logger"

LoggerKey is the gin context key under which the runtime stores the request-scoped logger.

View Source
const RequestIDKey = "request_id"

RequestIDKey is the Gin context key for the request identifier.

View Source
const ValidatorKey = "validator"

ValidatorKey is the gin context key under which the runtime stores the application validator, so binders resolve app-registered rules and messages without every call site passing the validator explicitly.

Variables

This section is empty.

Functions

func BindJSON

func BindJSON[T any](c *gin.Context, validators ...*validation.Validator) (T, bool)

BindJSON decodes one JSON value, rejects unknown fields and trailing JSON, validates it, and writes a stable error response when it fails.

func BindQuery

func BindQuery[T any](c *gin.Context, validators ...*validation.Validator) (T, bool)

BindQuery binds query parameters through `form` struct tags, validates the result, and writes a stable error response when it fails. Parameter values are never echoed back to the client.

func BindURI

func BindURI[T any](c *gin.Context, validators ...*validation.Validator) (T, bool)

BindURI binds path parameters through `uri` struct tags, validates the result, and writes a stable error response when it fails. Give URI fields matching `json` or `form` tags so validation errors use readable names.

func Created

func Created(c *gin.Context, data any)

Created writes a 201 response using the canonical success envelope.

func Fail

func Fail(c *gin.Context, err *Error)

Fail aborts the request with a validated public error envelope.

func Handle

func Handle(c *gin.Context, mapper Mapper, err error)

Handle maps an arbitrary error and aborts the request with the result.

func List

func List(c *gin.Context, data, meta any)

List writes a 200 response with data and optional pagination metadata.

func Logger

func Logger(c *gin.Context) *slog.Logger

Logger returns the request-scoped logger stored by the runtime, pre-populated with the request ID, method, and path. It falls back to slog.Default() when absent, for example in tests without the runtime middleware.

func NoContent

func NoContent(c *gin.Context)

NoContent writes an empty 204 response.

func OK

func OK(c *gin.Context, data any)

OK writes a 200 response using the canonical success envelope.

Types

type Envelope

type Envelope struct {
	// Data is the response payload.
	Data any `json:"data"`
	// Meta contains optional pagination or response metadata.
	Meta any `json:"meta,omitempty"`
}

Envelope is the canonical successful JSON response shape.

type Error

type Error struct {
	// Status is the HTTP status code written to the response.
	Status int
	// Code is the stable application error code.
	Code string
	// Message is safe to serialize to clients.
	Message string
	// Details carries optional safe structured data.
	Details any
	// Cause retains the original internal error for logging and errors.Is checks.
	Cause error
}

Error is a public HTTP error. Cause is retained for logging and never serialized.

func DefaultMapper

func DefaultMapper(err error, _ *gin.Context) *Error

DefaultMapper preserves public and validation errors; every other error becomes a generic internal_error without exposing its cause.

func NewError

func NewError(status int, code, message string) *Error

NewError constructs a public error without an internal cause.

func ValidationError

func ValidationError(failures *validation.Errors) *Error

ValidationError converts structured validation failures into the canonical 422 validation_failed response.

func WrapError

func WrapError(status int, code, message string, cause error) *Error

WrapError constructs a public error while preserving an internal cause.

func (*Error) Error

func (e *Error) Error() string

Error returns the internal cause message when available, otherwise Message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap exposes Cause to Go error inspection without serializing it.

type ErrorBody

type ErrorBody struct {
	// Code is the stable, machine-readable error code.
	Code string `json:"code"`
	// Message is the safe human-readable error message.
	Message string `json:"message"`
	// Details optionally carries safe structured error details.
	Details any `json:"details,omitempty"`
	// RequestID correlates the response with structured server logs.
	RequestID string `json:"request_id,omitempty"`
}

ErrorBody contains the serializable portion of one failed response.

type ErrorEnvelope

type ErrorEnvelope struct {
	// Error contains the safe public error details.
	Error ErrorBody `json:"error"`
}

ErrorEnvelope is the canonical failed JSON response shape.

type Mapper

type Mapper func(error, *gin.Context) *Error

Mapper converts an arbitrary handler error into a safe public Error.

Jump to

Keyboard shortcuts

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