Documentation
¶
Overview ¶
Package fault provides HTTP handlers and middleware for reporting errors in web applications, optionally rendering them with a custom HTML template.
Index ¶
- Constants
- func AssignError(req *http.Request, err error, status int)
- func FaultHandler() http.Handler
- func FaultHandlerWithOptions(opts *FaultHandlerOptions) http.Handler
- func ImplementsFaultHandlerVars(vars any) bool
- func RetrieveError(req *http.Request) (int, error)
- func TemplatedFaultHandler(t *template.Template) http.Handler
- func TemplatedFaultHandlerWrapper(t *template.Template, h http.Handler) http.Handler
- type FaultError
- type FaultHandlerOptions
- type FaultHandlerVars
- type FaultHandlerVarsFunc
- type FaultHandlerVarsSetter
- type FaultWrapper
- type StatusWriter
Constants ¶
const ErrorKey string = "github.com/aaronland/go-http/v5/fault#error"
ErrorKey is the name of the legacy string key for assigning `error` values to a `context.Context` instance.
Deprecated: use `AssignError` and `RetrieveError`. The value is still assigned for backwards compatibility.
const StatusKey string = "github.com/aaronland/go-http/v5/fault#status"
StatusKey is the name of the legacy string key for assigning status code values to a `context.Context` instance.
Deprecated: use `AssignError` and `RetrieveError`. The value is still assigned for backwards compatibility.
Variables ¶
This section is empty.
Functions ¶
func AssignError ¶
AssignError assigns 'err' and 'status' to the context of 'req' and updates 'req' in place (that is, '*req' is replaced by a shallow copy carrying the new context). The values are also assigned to the legacy `ErrorKey` and `StatusKey` string keys.
func FaultHandler ¶
FaultHandler returns a `http.Handler` instance for handling errors in a web application.
func FaultHandlerWithOptions ¶
func FaultHandlerWithOptions(opts *FaultHandlerOptions) http.Handler
FaultHandlerWithOptions returns a `http.Handler` for handling errors in a web application. It will retrieve any "public" and "private" errors that have been recorded and log the private error using `go-http/v4/slog`. If 'opts.Template' is defined it will be executed and passed the "public" error and status as template variables. The response has the status code assigned with `AssignError` (or 500 if that value is outside the range 100-999 accepted by `net/http`).
func ImplementsFaultHandlerVars ¶
ImplementsFaultHandlerVars returns a boolean value indicating whether 'vars' conforms to the required fields of the `FaultHandlerVars` struct type or implements the `FaultHandlerVarsSetter` interface.
func RetrieveError ¶
RetrieveError returns the status code and error assigned to 'req' with `AssignError`. If either value is missing, or of the wrong type, a status of `http.StatusInternalServerError` and a generic error are used instead.
func TemplatedFaultHandler ¶
TemplatedFaultHandler returns a `http.Handler` instance for handling errors in a web application with a custom HTML template.
func TemplatedFaultHandlerWrapper ¶
TemplatedFaultHandlerWrapper will return a middleware `http.Handler` that when invoked will serve 'h' and if the response status code is >= `http.StatusBadRequest` (400) will serve a new fault handler using 't' and 'l'.
Types ¶
type FaultError ¶
type FaultError interface {
error
// Public returns an `error` instance whose string value is considered safe for publishing in a public setting or context.
Public() error
// Private returns an `error` instance whose string value may contain details not suitable for publishing in a public setting or context.
Private() error
}
FaultError is an interface for providing custom public and private error messages.
type FaultHandlerOptions ¶
type FaultHandlerOptions struct {
// Template is an optional `html/template.Template` to use for reporting errors.
Template *template.Template
// VarsFunc is a `FaultHandlerVarsFunc` used to derive variables passed to templates. If nil and `Template`
// is non-nil the default `FaultHandlerVars` are used. The function is called once per request and
// should return a new value each time; values returned by pointer are copied before being modified.
VarsFunc FaultHandlerVarsFunc
// GenericPublicErrors, if true, replaces the public error of any error which does not implement `FaultError`
// with `http.StatusText(status)` so that internal error text is never passed to templates. The full error
// is still logged.
GenericPublicErrors bool
}
FaultHandlerOptions is a struct containing configuration options for the `FaultHandlerWithOptions` method.
type FaultHandlerVars ¶
type FaultHandlerVars struct {
// Status is the HTTP status code of the error being reported.
Status int
// Error is the public error being reported.
Error error
}
FaultHandlerVars are the minimal required variables that will be passed to a fault handler template. If you need to pass additional fields then you will need to create your handler using the `FaultHandlerWithOptions` method specifying a custom `FaultHandlerVarsFunc` property.
type FaultHandlerVarsFunc ¶
type FaultHandlerVarsFunc func() any
FaultHandlerVarsFunc is a custom function that returns a pointer to a struct that conforms to the required fields of the `FaultHandlerVars` struct type or that implements the `FaultHandlerVarsSetter` interface.
type FaultHandlerVarsSetter ¶
FaultHandlerVarsSetter is an optional interface that custom template variables may implement, as an alternative to having exported `Status` and `Error` fields, to receive the status and public error.
type FaultWrapper ¶
type FaultWrapper struct {
// contains filtered or unexported fields
}
FaultWrapper is a struct to make assigning `TemplatedFaultHandlerWrapper` instances easier.
func NewFaultWrapper ¶
func NewFaultWrapper(template *template.Template) *FaultWrapper
NewFaultWrapper creates a new `FaultWrapper` instance.
func (*FaultWrapper) HandleWithMux ¶
HandleWithMux will wrap 'h' in a new `TemplatedFaultHandlerWrapper` middleware handler and then assign it to 'mux' with pattern 'uri'.
type StatusWriter ¶
type StatusWriter struct {
http.ResponseWriter
// Status is the status code that was sent to the client: the first one written, or `http.StatusOK`.
Status int
// contains filtered or unexported fields
}
StatusWriter type adds a `Status` property to Go's `http.ResponseWriter` type.
func NewStatusWriter ¶
func NewStatusWriter(w http.ResponseWriter) *StatusWriter
NewStatusWriter returns a new `StatusWriter` wrapping 'w'. It is intended for middleware (such as a logging function) that requires access to the status code written to `http.ResponseWriter`. The default status is `http.StatusOK`.
func MyMiddleware(h http.HandleFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sw := fault.NewStatusWriter(w) // Here we override http.ResponseWriter's `WriteHeader` function
h(sw, r)
statusCode := sw.Status // Now work with the status code
}
}
func (*StatusWriter) Flush ¶
func (w *StatusWriter) Flush()
Flush calls the underlying writer's `Flush` method, if it has one.
func (*StatusWriter) Unwrap ¶
func (w *StatusWriter) Unwrap() http.ResponseWriter
Unwrap returns the underlying `http.ResponseWriter` for use with `http.ResponseController`.
func (*StatusWriter) Write ¶
func (w *StatusWriter) Write(b []byte) (int, error)
Write marks the status as sent (an implicit 200) and writes to the underlying `http.ResponseWriter`.
func (*StatusWriter) WriteHeader ¶
func (w *StatusWriter) WriteHeader(status int)
WriteHeader records 'status' (only the first call counts, as that is what the client sees) and then calls the underlying `http.ResponseWriter`'s `WriteHeader` method.