errors

package
v0.0.0-...-464f0ff Latest Latest
Warning

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

Go to latest
Published: May 8, 2016 License: MIT Imports: 5 Imported by: 2

README

Scaffold Errors

This package improves error handling in scaffold.

Examples:

This example uses the handler builder to accept handlers that return errors:

package main

import (
    "net/http"

    "github.com/ThatsMrTalbot/scaffold"
    "github.com/ThatsMrTalbot/scaffold/errors"
	"golang.org/x/net/context"
)

func ExampleHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
    return errors.NewErrorStatus(500, "Oh dear!")
}

func ErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error) {
    http.Error(w, err.Error(), status)
}

func main() {
    dispatcher := scaffold.DefaultDispatcher()
    router := scaffold.New(dispatcher)

    // Add handler builder to accept handlers that return errors
    router.AddHandlerBuilder(errors.HandlerBuilder)

    // Set the error handler for 500 errors
    router.Use(errors.SetErrorHandlerFunc(500, ErrorHandler))

    // Route the example handler
    router.Handle("/", ExampleHandler)

    http.ListenAndServe(":8080", dispatcher)
}

This example shows getting the error handler without using the handler builder:

package main

import (
    "fmt"
    "net/http"

    "github.com/ThatsMrTalbot/scaffold"
    "github.com/ThatsMrTalbot/scaffold/errors"
	"golang.org/x/net/context"
)

func ExampleHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
    err := errors.NewErrorStatus(500, "Oh dear!")
    if err != nil {
        GetErrorHandler(ctx, 500).ServeErrorPage(ctx, w, r, 500, err)
        return
    }
}

func ErrorHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error) {
    http.Error(w, err.Error(), status)
}

func main() {
    dispatcher := scaffold.DefaultDispatcher()
    router := scaffold.New(dispatcher)

    // Set the error handler for all errors
    router.Use(errors.SetErrorHandlerFunc(errors.AllStatusCodes, ErrorHandler))

    // Route the example handler
    router.Handle("/", ExampleHandler)

    http.ListenAndServe(":8080", dispatcher)
}

Documentation

Index

Constants

View Source
const AllStatusCodes = 0

AllStatusCodes is a nice way of saying add for all status codes

Variables

View Source
var DefaultErrorHandler = &defaultErrorHandler{}

DefaultErrorHandler is the default error handler

Functions

func ConvertErrorStatus

func ConvertErrorStatus(status int, err error) error

ConvertErrorStatus creates an error that implements ErrorStatus based off an error

func HandlerBuilder

func HandlerBuilder(i interface{}) (scaffold.Handler, error)

HandlerBuilder can be used to create a scaffold.Handler based on a Handler

func NewErrorStatus

func NewErrorStatus(status int, err string) error

NewErrorStatus creates an error that implements ErrorStatus based off a string

func SetErrorHandler

func SetErrorHandler(status int, handler ErrorHandler) scaffold.Middleware

SetErrorHandler returns Middleware that can be used to set the error handler

func SetErrorHandlerFunc

func SetErrorHandlerFunc(status int, handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error)) scaffold.Middleware

SetErrorHandlerFunc returns Middleware that can be used to set the error handler

Types

type ErrorHandler

type ErrorHandler interface {
	ServeErrorPage(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error)
}

ErrorHandler is an error handler

func GetErrorHandler

func GetErrorHandler(ctx context.Context, status int) ErrorHandler

GetErrorHandler gets the error handler from the context or returns the default

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error)

ErrorHandlerFunc is an error handler

func (ErrorHandlerFunc) ServeErrorPage

func (e ErrorHandlerFunc) ServeErrorPage(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, err error)

ServeErrorPage Implements ErrorHandler.ServeErrorPage

type ErrorStatus

type ErrorStatus interface {
	error
	Status() int
}

ErrorStatus extends error adding a status method

type Handler

type Handler interface {
	CtxServeHTTP(context.Context, http.ResponseWriter, *http.Request) error
}

Handler is similar to scaffold.Handler with the difference that an error is returned

type HandlerFunc

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request) error

HandlerFunc is similar to scaffold.HandlerFunc with the difference that an error is returned

func (HandlerFunc) CtxServeHTTP

func (h HandlerFunc) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) error

CtxServeHTTP implements Handler.CtxServeHTTP

Jump to

Keyboard shortcuts

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