fault

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: BSD-3-Clause Imports: 6 Imported by: 1

README

go-http-fault

Go package providing a net/http handler for logging errors and rendering them to the browser using custom templates.

Documentation

Go Reference

Example

import (
       "net/http"
       "html/template"
       "github.com/sfomuseum/go-http-fault"
       "log"
)

// Create a custom HTTP handler specific to your application that accepts a `fault.FaultHandler` instance

func ExampleHandler(fault_handler http.Handler) http.Handler {

	fn := func(rsp http.ResponseWriter, req *http.Request) {

		// In your handler invoke some custom code that may return an error
		
		err := SomeOtherFunction(req)

		// If it does assign the error returned and a status code (not required to be an HTTP status code)
		// that will be used for logging errors
		
		if err != nil {
			req = fault.AssignError(req, err, http.StatusInternalServerError)
			fault_handler.ServeHTTP(rsp, req)
			return
		}

		rsp.Write([]byte("Hello world"))
		return
	}

	h := http.HandlerFunc(fn)
	return h, nil
}

func main() {

	// In your application's `main` routine create and `html/template` instance
	// and retrieve a template named "fault" (or whatever you choose)
	
     	t, _ := template.New("example").Parse(...)
        fault_t := t.Lookup("fault")

	// Create a custom `log.Logger` instance that will be used to record errors
	
	error_logger := log.Default()

	// Now pass both to `fault.TemplatedFaultHandler` which will return an `http.Handler`
	
	fault_handler, _ := fault.TemplatedFaultHandler(error_logger, fault_t)

	// And pass that to any other handlers where you need a consistent interface
	// for handling public-facing errors
	
	handler := ExampleHandler(fault_handler)

	mux.Handle("/", handler)

	http.ListenAndServer(":8080", mux)

Documentation

Overview

package fault provides a net/http handler for logging errors and rendering them to the browser using custom templates.

Index

Constants

View Source
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.

View Source
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

func AssignError(req *http.Request, err error, status int) *http.Request

AssignError assigns 'err' and 'status' the `ErrorKey` and `StatusKey` values of 'req.Context' and returns an updated instance of 'req'

func FaultHandler

func FaultHandler(l *log.Logger) (http.Handler, error)

FaultHandler returns a `http.Handler` instance for handling errors in a web application.

func RetrieveError added in v0.0.2

func RetrieveError(req *http.Request) (int, error)

RetrieveError returns the values of the `StatusKey` and `ErrorKey` values of 'req.Context'

func TemplatedFaultHandler added in v0.0.2

func TemplatedFaultHandler(l *log.Logger, t *template.Template) (http.Handler, error)

TemplatedFaultHandler returns a `http.Handler` instance for handling errors in a web application with a custom HTML template.

Types

type FaultError added in v0.0.4

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 FaultHandlerVars added in v0.0.2

type FaultHandlerVars struct {
	Status int
	Error  error
}

Jump to

Keyboard shortcuts

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