Documentation
¶
Overview ¶
package fault provides a net/http handler for logging errors and rendering them to the browser using custom templates.
Index ¶
- Constants
- func AssignError(req *http.Request, err error, status int)
- func FaultHandler(l *log.Logger) http.Handler
- func FaultHandlerWithOptions(opts *FaultHandlerOptions) http.Handler
- func ImplementsFaultHandlerVars(vars interface{}) bool
- func RetrieveError(req *http.Request) (int, error)
- func TemplatedFaultHandler(l *log.Logger, t *template.Template) http.Handler
- func TemplatedFaultHandlerWrapper(l *log.Logger, t *template.Template, h http.Handler) http.Handler
- type FaultError
- type FaultHandlerOptions
- type FaultHandlerVars
- type FaultHandlerVarsFunc
- type FaultWrapper
- type StatusWriter
Constants ¶
const ErrorKey string = "github.com/sfomuseum/go-http-fault#error"
ErrorKey is the name of the key for assigning `error` values to a `context.Context` instance.
const StatusKey string = "github.com/sfomuseum/go-http-fault#status"
StatusKey is the name of the key for assigning status code values to a `context.Context` instance.
Variables ¶
This section is empty.
Functions ¶
func AssignError ¶
AssignError assigns 'err' and 'status' the `ErrorKey` and `StatusKey` values of 'req.Context' and updates 'req' in place.
func FaultHandler ¶
FaultHandler returns a `http.Handler` instance for handling errors in a web application.
func FaultHandlerWithOptions ¶ added in v2.1.0
func FaultHandlerWithOptions(opts *FaultHandlerOptions) http.Handler
faultHandler returns a `http.Handler` for handling errors in a web application. It will retrieve and "public" and "private" errors that have been recorded and log them to 'l'. If 't is defined it will executed and passed the "public" error as a template variable.
func ImplementsFaultHandlerVars ¶ added in v2.1.0
func ImplementsFaultHandlerVars(vars interface{}) bool
ImplementsFaultHandlerVars returns a boolean value indicating whether 'vars' conforms to the required fields of the `FaultHandlerVars` struct type.
func RetrieveError ¶
RetrieveError returns the values of the `StatusKey` and `ErrorKey` values of 'req.Context'
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` (300) 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 that 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 ¶ added in v2.1.0
type FaultHandlerOptions struct { // Logger is a custom `log.Logger` instance used for logging and feedback. Logger *log.Logger // Template is an optional `html/template.Template` to use for reporting errors. Template *template.Template // VarFunc is a `FaultHandlerVarsFunc` used to derive variables passed to templates. Required if `Template` is non-nil. VarsFunc FaultHandlerVarsFunc }
FaultHandlerOptions is a struct containing configuration options for the `FaultHandlerWithOptions` method.
type FaultHandlerVars ¶
FaultHandlerVars are the minimal required variables that will be pass to a fault handler template. If you need to pass additional fields then you will need to create your handler using the the `FaultHandlerWithOptions` method specifying a custom `FaultHandlerVarsFunc` property.
type FaultHandlerVarsFunc ¶ added in v2.1.0
type FaultHandlerVarsFunc func() interface{}
FaultHandlerVarsFunc is a custom function that returns a pointer to a struct that conforms to the required fields of the `FaultHandlerVars` struct type.
type FaultWrapper ¶
type FaultWrapper struct {
// contains filtered or unexported fields
}
FaultWrapper is a struct to make assigning `TemplatedFaultHandlerWrapper` instances easier.
func NewFaultWrapper ¶
func NewFaultWrapper(logger *log.Logger, template *template.Template) *FaultWrapper
NewFaultWrapper will create 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 int }
StatusWriter type adds a `Status` property to Go's `http.ResponseWriter` type.
func NewStatusWriter ¶
func NewStatusWriter(w http.ResponseWriter) *StatusWriter
New shows setup for a middleware function. This could be a logging function or similar whereby you require access to the status code from `http.ResponseWriter`
func MyMiddleware(h http.HandleFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { sw := status_writer.New(w) // Here we override http.ResponseWriter's `WriteHeader` function h(sw, r) statusCode := sw.Status // Now work with the status code } }
func (*StatusWriter) WriteHeader ¶
func (w *StatusWriter) WriteHeader(status int)
WriteHeader override the http.ResponseWriter's `WriteHeader` method