Documentation
¶
Overview ¶
Package safety provides internal crash-isolation helpers for turning recovered panics into typed errors with bounded, server-side metadata. It does not import HTTP, metrics, or transport packages; call sites in adapters add client-safe response shaping and logging.
Index ¶
- func AppendPanicStackAttr(attrs []slog.Attr, pe *PanicError) []slog.Attr
- func Call(boundary Boundary, operation string, fn func() error) (err error)
- func CallValue[T any](boundary Boundary, operation string, fn func() (T, error)) (v T, err error)
- func PanicSlogFieldAttrs(pe *PanicError) []slog.Attr
- type Boundary
- type PanicError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendPanicStackAttr ¶
func AppendPanicStackAttr(attrs []slog.Attr, pe *PanicError) []slog.Attr
AppendPanicStackAttr appends a "panic_stack" attribute when pe has stack bytes. For server logs only; do not add to client responses. When pe is nil, attrs is returned unchanged.
func Call ¶
Call runs fn and turns a panic into a PanicError; otherwise returns the error from fn.
func PanicSlogFieldAttrs ¶
func PanicSlogFieldAttrs(pe *PanicError) []slog.Attr
PanicSlogFieldAttrs returns boundary, operation, and panic_value_type attributes for a non-nil PanicError. It returns an empty slice when pe is nil.
Types ¶
type Boundary ¶
type Boundary string
Boundary classifies where an isolated panic was observed (operator diagnostics).
const ( BoundaryHTTP Boundary = "http_request" BoundaryExtension Boundary = "extension_execution" BoundaryBackend Boundary = "backend_attempt" BoundaryStream Boundary = "stream_processing" BoundaryWorker Boundary = "owned_worker" )
Boundary values match design: internal stable strings, safe for metrics/log keys when bounded.
type PanicError ¶
type PanicError struct {
// contains filtered or unexported fields
}
PanicError is an [error] for a recovered application panic. [Error] returns only bounded, non-sensitive text suitable for wrapping; use [Stack] and logs for server-side details.
func Capture ¶
func Capture(boundary Boundary, operation string, value any) *PanicError
Capture builds a PanicError from a recovered panic value. Call from the `recover()` branch in a deferred function, passing the `recover()` result.
func (*PanicError) Boundary ¶
func (e *PanicError) Boundary() Boundary
Boundary returns the failure class for diagnostics.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns a stable, client-safe message without the panic value or stack.
func (*PanicError) Operation ¶
func (e *PanicError) Operation() string
Operation returns the logical operation name (static at the call site, not user data).
func (*PanicError) Stack ¶
func (e *PanicError) Stack() []byte
Stack returns captured stack bytes for server-side logging only. Do not send to clients.
func (*PanicError) ValueType ¶
func (e *PanicError) ValueType() string
ValueType returns a short type name of the panic value (e.g. "string", "int") for server logs.