response

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Forbidden added in v0.8.0

func Forbidden(message string, cause error) error

Forbidden creates an expected HTTP 403 error for generated boundaries.

func HandlerErrorMessage added in v0.5.0

func HandlerErrorMessage(err error, status int) string

HandlerErrorMessage returns the client-facing message for a generated handler error. Ordinary 5xx failures use generic status text so internal details stay out of HTTP responses. HandlerError.Message remains an explicit application-owned override.

func HandlerStatus

func HandlerStatus(err error, fallback int) int

HandlerStatus returns a handler error status, or fallback for ordinary errors.

func HandlerUserMessage added in v0.13.0

func HandlerUserMessage(err error, status int) i18n.UserMessage

HandlerUserMessage returns the stable code/default/vars payload for err.

func IsRequestBodyTooLarge added in v0.13.0

func IsRequestBodyTooLarge(err error) bool

IsRequestBodyTooLarge reports whether err contains the typed error returned by http.MaxBytesReader after a request exceeds its configured limit.

func LocalizeValidation added in v0.13.0

func LocalizeValidation(result validation.Result, bundle i18n.ErrorBundle, locale string) validation.Result

LocalizeValidation resolves validation messages through the typed catalog.

func LocalizedHandlerErrorMessage added in v0.13.0

func LocalizedHandlerErrorMessage(err error, status int, bundle i18n.ErrorBundle, locale string) string

LocalizedHandlerErrorMessage resolves an expected handler error through bundle.

func NewCodedHandlerError added in v0.13.0

func NewCodedHandlerError(status int, code, message string, vars map[string]string, cause error) error

NewCodedHandlerError creates an expected handler failure with a stable code.

func NewExpectedCode added in v0.13.0

func NewExpectedCode(kind ErrorKind, code, message string, vars map[string]string, cause error) error

NewExpectedCode creates an expected typed error with an app-owned stable code.

func NewExpectedError added in v0.8.0

func NewExpectedError(kind ErrorKind, message string, cause error) error

NewExpectedError creates a typed application-owned error for generated boundaries. The message is client-facing; do not include secrets or internal service details.

func NewHandlerError

func NewHandlerError(status int, message string, cause error) error

NewHandlerError creates an error suitable for generated handlers.

func NotFound added in v0.8.0

func NotFound(message string, cause error) error

NotFound creates an expected HTTP 404 error for generated boundaries.

func ServerError added in v0.8.0

func ServerError(message string, cause error) error

ServerError creates an expected HTTP 500 error for generated boundaries.

func ValidateLocalRedirect added in v0.5.0

func ValidateLocalRedirect(url string) error

ValidateLocalRedirect reports whether url is a safe same-origin redirect target. Only local absolute paths are allowed; protocol-relative URLs, backslash tricks browsers normalize to "//", and CRLF header-injection attempts are rejected. It is the single redirect-safety contract shared by the response, guard, and SSR lanes so an attacker-influenced "next"/"return_to" value cannot become an open redirect.

func ValidationFailed added in v0.8.0

func ValidationFailed(message string, cause error) error

ValidationFailed creates an expected HTTP 422 error for generated boundaries.

func ValidationHTML added in v0.1.5

func ValidationHTML(result validation.Result) string

ValidationHTML renders a small escaped validation message block.

func WriteHTML added in v0.1.5

func WriteHTML(writer http.ResponseWriter, request *http.Request, body string, cacheControl string) error

WriteHTML writes an HTML response with an explicit Cache-Control policy and suppresses the body for HEAD requests. An empty cache policy falls back to no-store for request-time safety.

func WriteHTTP

func WriteHTTP(writer http.ResponseWriter, result Response) error

WriteHTTP writes a runtime response envelope to net/http.

func WriteNoStoreError

func WriteNoStoreError(writer http.ResponseWriter, status int, message string)

WriteNoStoreError writes an HTTP error that must not be cached.

func WriteNoStoreHTML

func WriteNoStoreHTML(writer http.ResponseWriter, request *http.Request, body string) error

WriteNoStoreHTML writes a no-store HTML response and suppresses the body for HEAD requests.

func WriteNoStoreHTTP

func WriteNoStoreHTTP(writer http.ResponseWriter, result Response) error

WriteNoStoreHTTP writes a response envelope that must not be cached.

func WriteNoStoreHandlerError added in v0.5.0

func WriteNoStoreHandlerError(writer http.ResponseWriter, err error, fallbackStatus int)

WriteNoStoreHandlerError writes a generated handler error using the client-safe HandlerErrorMessage policy.

func WriteNoStoreHandlerJSONError added in v0.5.0

func WriteNoStoreHandlerJSONError(writer http.ResponseWriter, err error, fallbackStatus int)

WriteNoStoreHandlerJSONError writes a generated handler error as the stable JSON shape used by contract web adapters.

func WriteNoStoreJSONError added in v0.5.0

func WriteNoStoreJSONError(writer http.ResponseWriter, status int, message string)

WriteNoStoreJSONError writes the stable generated JSON error shape.

func WriteNoStoreJSONUserError added in v0.13.0

func WriteNoStoreJSONUserError(writer http.ResponseWriter, status int, code, message string)

WriteNoStoreJSONUserError writes the stable code/message JSON error shape.

func WriteNoStoreLocalizedHandlerError added in v0.13.0

func WriteNoStoreLocalizedHandlerError(writer http.ResponseWriter, err error, fallbackStatus int, bundle i18n.ErrorBundle, locale string)

WriteNoStoreLocalizedHandlerError localizes an expected handler error.

func WriteNoStoreLocalizedHandlerJSONError added in v0.13.0

func WriteNoStoreLocalizedHandlerJSONError(writer http.ResponseWriter, err error, fallbackStatus int, bundle i18n.ErrorBundle, locale string)

WriteNoStoreLocalizedHandlerJSONError is the structured JSON counterpart.

func WriteNoStoreLocalizedJSONUserError added in v0.13.0

func WriteNoStoreLocalizedJSONUserError(writer http.ResponseWriter, status int, code, message string, vars map[string]string, bundle i18n.ErrorBundle, locale string)

WriteNoStoreLocalizedJSONUserError resolves a coded JSON error through bundle.

func WriteNoStoreLocalizedUserError added in v0.13.0

func WriteNoStoreLocalizedUserError(writer http.ResponseWriter, status int, code, message string, vars map[string]string, bundle i18n.ErrorBundle, locale string)

WriteNoStoreLocalizedUserError resolves a coded default through bundle.

func WriteNoStoreUserError added in v0.13.0

func WriteNoStoreUserError(writer http.ResponseWriter, status int, code, message string)

WriteNoStoreUserError writes safe text and exposes its stable code in a response header for non-JSON clients.

Types

type ErrorKind added in v0.8.0

type ErrorKind string

ErrorKind identifies expected application-owned error categories that generated request-time lanes can map to HTTP status codes.

const (
	ErrorNotFound   ErrorKind = "not_found"
	ErrorForbidden  ErrorKind = "forbidden"
	ErrorValidation ErrorKind = "validation"
	ErrorServer     ErrorKind = "server"
)

type HandlerError

type HandlerError struct {
	Status  int
	Message string
	Cause   error
}

HandlerError wraps failures raised by generated handlers.

func (HandlerError) Error

func (err HandlerError) Error() string

func (HandlerError) Unwrap

func (err HandlerError) Unwrap() error

type Kind

type Kind string

Kind identifies the response shape produced by actions, fragments, APIs, or full-page rendering.

const (
	HTML     Kind = "html"
	Redirect Kind = "redirect"
	Fragment Kind = "fragment"
	JSON     Kind = "json"
	Reload   Kind = "reload"
)

type Response

type Response struct {
	Kind    Kind
	Status  int
	Body    string
	Target  string
	Swap    SwapMode
	URL     string
	Cookies []http.Cookie
}

Response is the generated runtime response envelope.

func FragmentFor

func FragmentFor(target, body string) Response

FragmentFor creates a partial fragment response for a DOM target.

func FragmentSwap

func FragmentSwap(target string, swap SwapMode, body string) (Response, error)

FragmentSwap creates a partial fragment response with an explicit swap mode.

func HTMLBody

func HTMLBody(status int, body string) Response

HTMLBody creates a full HTML response.

func JSONBody

func JSONBody(status int, body string) Response

JSONBody creates a JSON response from an already-encoded body.

func JSONValue

func JSONValue(status int, value any) (Response, error)

JSONValue marshals a value into a JSON response body.

func LocalizedValidationFragment added in v0.13.0

func LocalizedValidationFragment(target string, result validation.Result, bundle i18n.ErrorBundle, locale string) Response

LocalizedValidationFragment resolves coded field errors before rendering.

func RedirectTo

func RedirectTo(url string) Response

RedirectTo creates a redirect response.

func ReloadPage added in v0.1.5

func ReloadPage() Response

ReloadPage asks enhanced clients to reload the current page. Non-enhanced forms should still use normal POST/redirect/get behavior.

func ValidationFragment added in v0.1.5

func ValidationFragment(target string, result validation.Result) Response

ValidationFragment creates a fragment response with escaped validation messages. It uses HTTP 200 so progressively enhanced partial forms can swap the returned fragment with the current client runtime.

func ValidationJSON added in v0.1.5

func ValidationJSON(result validation.Result) (Response, error)

ValidationJSON creates a structured validation error response.

func WithCookie added in v0.1.5

func WithCookie(result Response, cookie http.Cookie) Response

WithCookie returns a copy of result that sets cookie when written to HTTP.

type SwapMode

type SwapMode string

SwapMode identifies how a fragment response should update its target.

const (
	SwapInnerHTML SwapMode = "innerHTML"
	SwapOuterHTML SwapMode = "outerHTML"
)

Jump to

Keyboard shortcuts

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