fault

package
Version: v2.1.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2018 License: MIT Imports: 5 Imported by: 13

Documentation

Overview

Package fault provides a panic and error handler for the ozzo routing package.

Package fault provides a panic and error handler for the ozzo routing package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(logf LogFunc, errorf ...ConvertErrorFunc) routing.Handler

ErrorHandler returns a handler that handles errors returned by the handlers following this one. If the error implements routing.HTTPError, the handler will set the HTTP status code accordingly. Otherwise the HTTP status is set as http.StatusInternalServerError. The handler will also write the error as the response body.

A log function can be provided to log a message whenever an error is handled. If nil, no message will be logged.

An optional error conversion function can also be provided to convert an error into a normalized one before sending it to the response.

import (
    "log"
    "github.com/go-ozzo/ozzo-routing"
    "github.com/go-ozzo/ozzo-routing/fault"
)

r := routing.New()
r.Use(fault.ErrorHandler(log.Printf))
r.Use(fault.PanicHandler(log.Printf))

func PanicHandler

func PanicHandler(logf LogFunc) routing.Handler

PanicHandler returns a handler that recovers from panics happened in the handlers following this one. When a panic is recovered, it will be converted into an error and returned to the parent handlers.

A log function can be provided to log the panic call stack information. If the log function is nil, no message will be logged.

import (
    "log"
    "github.com/go-ozzo/ozzo-routing"
    "github.com/go-ozzo/ozzo-routing/fault"
)

r := routing.New()
r.Use(fault.ErrorHandler(log.Printf))
r.Use(fault.PanicHandler(log.Printf))

func Recovery

func Recovery(logf LogFunc, errorf ...ConvertErrorFunc) routing.Handler

Recovery returns a handler that handles both panics and errors occurred while servicing an HTTP request. Recovery can be considered as a combination of ErrorHandler and PanicHandler.

The handler will recover from panics and render the recovered error or the error returned by a handler. If the error implements routing.HTTPError, the handler will set the HTTP status code accordingly. Otherwise the HTTP status is set as http.StatusInternalServerError. The handler will also write the error as the response body.

A log function can be provided to log a message whenever an error is handled. If nil, no message will be logged.

An optional error conversion function can also be provided to convert an error into a normalized one before sending it to the response.

import (
    "log"
    "github.com/go-ozzo/ozzo-routing"
    "github.com/go-ozzo/ozzo-routing/fault"
)

r := routing.New()
r.Use(fault.Recovery(log.Printf))

Types

type ConvertErrorFunc

type ConvertErrorFunc func(*routing.Context, error) error

ConvertErrorFunc converts an error into a different format so that it is more appropriate for rendering purpose.

type LogFunc

type LogFunc func(format string, a ...interface{})

LogFunc logs a message using the given format and optional arguments. The usage of format and arguments is similar to that for fmt.Printf(). LogFunc should be thread safe.

Jump to

Keyboard shortcuts

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