response

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package response is the single shape every HTTP response in an inceptools service takes, and the single place an outgoing message is scrubbed.

One envelope, always:

{ "data": <payload|null>, "meta": { "success": true, "traceId": "...", ... } }

A client written against one endpoint can read every endpoint: success is a boolean in a fixed place, pagination is in a fixed place, the trace id to quote in a support ticket is in a fixed place. The cost is a wrapper object; the benefit is that no consumer ever has to ask what shape THIS route returns.

The other reason this package is narrow: Send is the funnel. Every response passes through it, so the leak check runs once, here, rather than being remembered at several hundred call sites.

Index

Constants

View Source
const (
	LocalTraceID  = "traceId"
	LocalStartKey = "startTime"
)

Keys under which the middleware stores per-request values on fiber.Ctx.

Variables

This section is empty.

Functions

func Accepted

func Accepted(c fiber.Ctx, message string, data any) error

Accepted writes 202 — the right code for work that was queued rather than done. Provisioning a VM is Accepted, not Created: the resource record exists but the thing itself does not yet.

func BadRequest

func BadRequest(c fiber.Ctx, message string, data any) error

BadRequest writes 400.

func Conflict

func Conflict(c fiber.Ctx, message string) error

Conflict writes 409.

func Created

func Created(c fiber.Ctx, message string, data any) error

Created writes 201.

func Fail

func Fail(c fiber.Ctx, err error) error

Fail writes the HTTP response for any error.

This is what a controller returns on the sad path — `return response.Fail(c, err)` — and what the exception filter calls for anything that escaped. An *errs.Error decides its own status, code and field details. Anything else is a 500 whose message is replaced, because an unclassified error is by definition one nobody checked for leaks.

func FailValidation

func FailValidation(c fiber.Ctx, details []errs.Detail) error

FailValidation writes 400 with field-scoped details, the shape a form expects.

func Forbidden

func Forbidden(c fiber.Ctx, message string) error

Forbidden writes 403.

func InternalError

func InternalError(c fiber.Ctx) error

InternalError writes 500 with a generic message. The message you pass is NOT sent — log it instead; the client gets the trace id.

func List

func List(c fiber.Ctx, message string, data any, page, pageSize int, total int64) error

List writes a paginated collection with the page metadata filled in.

func NoContent

func NoContent(c fiber.Ctx) error

NoContent writes 204 with no body. It is the one response that is NOT an envelope, because 204 means exactly that.

func NotFound

func NotFound(c fiber.Ctx, message string) error

NotFound writes 404.

func OK

func OK(c fiber.Ctx, message string, data any) error

OK writes 200.

func PaymentRequired

func PaymentRequired(c fiber.Ctx, message string) error

PaymentRequired writes 402.

func Route

func Route(c fiber.Ctx) string

Route renders the handler route for logs: "GET /api/v1/catalog/products".

func Send

func Send(c fiber.Ctx, status int, message string, data any) error

Send writes the envelope. Every other function in this package goes through it, and so should any responder you add.

func ServiceUnavailable

func ServiceUnavailable(c fiber.Ctx, message string) error

ServiceUnavailable writes 503.

func SetSanitizer

func SetSanitizer(s Sanitizer)

SetSanitizer installs the outgoing-message filter. Passing nil removes it. Call it once, at boot.

func StatusFor

func StatusFor(kind errs.Kind) int

StatusFor maps an error kind to an HTTP status. It is exported so a custom exception filter can reuse the mapping without re-deriving it.

func StatusForError added in v1.1.0

func StatusForError(err error) int

StatusForError is the status an error will END UP with once the exception filter has run.

Instrumentation needs this. A middleware observes `c.Next()` returning an error, but the status on the response is still the default at that point: Fiber invokes the error handler after the whole chain has unwound. A collector that reads the status code there records a failed request as a 200 — the route that fails every time looks perfectly healthy.

func TooManyRequests

func TooManyRequests(c fiber.Ctx, message string) error

TooManyRequests writes 429.

func TraceID

func TraceID(c fiber.Ctx) string

TraceID returns the request's correlation id, generating one if the middleware has not run.

func Unauthorized

func Unauthorized(c fiber.Ctx, message string) error

Unauthorized writes 401.

func UnprocessableEntity

func UnprocessableEntity(c fiber.Ctx, message string, data any) error

UnprocessableEntity writes 422 — syntactically valid, semantically refused.

Types

type Envelope

type Envelope struct {
	Data any  `json:"data"`
	Meta Meta `json:"meta"`
}

Envelope is the body of every response.

type Meta

type Meta struct {
	Success     bool   `json:"success"`
	TraceID     string `json:"traceId"`
	Message     string `json:"message"`
	RequestTime int64  `json:"requestTime"` // milliseconds spent serving
	StatusCode  int    `json:"statusCode"`
	Route       string `json:"route"`

	// Code is the machine-readable error code, set only on failures that
	// carry one. Clients branch on this, never on Message.
	Code string `json:"code,omitempty"`
	// Errors carries field-scoped detail, set only on failures that have it.
	Errors []errs.Detail `json:"errors,omitempty"`

	// Set only on list responses.
	Page     *int   `json:"page,omitempty"`
	PageSize *int   `json:"pageSize,omitempty"`
	Total    *int64 `json:"total,omitempty"`
	Pages    *int   `json:"pages,omitempty"`
}

Meta is the envelope's second half: everything about the response that is not the payload.

type Sanitizer

type Sanitizer func(c fiber.Ctx, scope, route, message string) string

Sanitizer inspects an outgoing message and returns what may be sent.

It is the hook for the one thing a response funnel is uniquely able to do: catch infrastructure detail on its way out. Handlers pass err.Error() into these responders by the hundred, and any one of those strings may have come from a database driver, an HTTP client or a message broker. Install a sanitizer and the check happens once, for all of them.

route is "METHOD /path"; scope is "http".

Jump to

Keyboard shortcuts

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