http

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package http provides transport-independent HTTP responses, JSON decoding and application error mapping. Import it as pfwhttp alongside net/http.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON[T any](r *stdhttp.Request, options DecodeOptions) (T, error)

DecodeJSON reads one JSON value, enforcing content type and body size. Request errors are *Error values with a safe public message and internal cause.

Types

type DecodeOptions

type DecodeOptions struct {
	// MaxBodyBytes defaults to 1 MiB. The body is read with a bounded allocation.
	MaxBodyBytes int64
	// Unknown fields are rejected by default; opt in to compatibility if needed.
	AllowUnknownFields bool
}

type Error

type Error struct {
	Status  int               `json:"-"`
	Code    string            `json:"code"`
	Message string            `json:"message"`
	Fields  map[string]string `json:"fields,omitempty"`
	// contains filtered or unexported fields
}

Error describes a public HTTP error. Its status and internal cause are not serialized, and domain errors need not import this package.

func NewError

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

func (*Error) WithFields

func (e *Error) WithFields(fields map[string]string) *Error

func (*Error) Write

func (e *Error) Write(w stdhttp.ResponseWriter) error

type ErrorMapper

type ErrorMapper interface {
	Map(error) *Error
	Write(stdhttp.ResponseWriter, error) error
}

ErrorMapper is the contract used by HTTP consumers. Implementations map application errors and write their public response without exposing causes.

type Mapper

type Mapper struct {
	// contains filtered or unexported fields
}

func NewMapper

func NewMapper(rules ...Rule) *Mapper

NewMapper creates a mapper with ordered rules and a safe generic 500 fallback.

func (*Mapper) Map

func (m *Mapper) Map(err error) *Error

Map preserves already typed HTTP errors, then evaluates application rules. Unknown errors receive a generic public message while retaining the cause.

func (*Mapper) Write

func (m *Mapper) Write(w stdhttp.ResponseWriter, err error) error

type Response

type Response[T any] struct {
	Status  int
	Headers stdhttp.Header
	Body    T
}

func Created

func Created[T any](location string, body T) Response[T]

func JSON

func JSON[T any](status int, body T) Response[T]

func NoContent

func NoContent() Response[struct{}]

func OK

func OK[T any](body T) Response[T]

func (Response[T]) Write

func (r Response[T]) Write(w stdhttp.ResponseWriter) error

Write serializes before committing headers, so encoding failures do not leave a partial successful response. Network write errors are returned to the caller.

type Rule

type Rule func(error) *Error

Rule returns nil when it does not match. Rules must be safe for concurrent use.

func As

func As[T error](mapError func(T) *Error) Rule

func Is

func Is(target error, response *Error) Rule

Jump to

Keyboard shortcuts

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