recovr

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: MIT Imports: 10 Imported by: 0

README

recovr

godoc

Recovr is a HTTP middleware that catches any panics and serves a proper error response.

Works with all frameworks that support native http handler (eg. Echo, Goji, etc.).

HTML

Installation

$ go get github.com/afansv/recovr

Usage

package main

import (
    "net/http"

    "github.com/afansv/recovr"
)

var myPanicHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    panic("you should not have a handler that just panics ;)")
})

func main() {
    recovery := recovr.New()
    app := recovery(myPanicHandler)
    http.ListenAndServe("0.0.0.0:3000", app)
}

Examples

Echo Example
import "github.com/labstack/echo/v4"

func main() {
    e := echo.New()
    e.Use(echo.WrapMiddleware(recovr.New()))
    e.Logger.Fatal(e.Start(":1323"))
}
Goji Example
import "github.com/zenazn/goji"

func main() {
    goji.Use(recovr.New())
    goji.Serve()
}

Screenshots

  • Logger

Logger

  • JSON

JSON

  • HTML

HTML

Documentation

Overview

Package recovr is a HTTP middleware that catches any panics and serves a proper error response.

package main

import (
	"net/http"

	"github.com/afansv/recovr"
)

var myPanicHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	panic("you should not have a handler that just panics ;)")
})

func main() {
	recovery := recovr.New()
	app := recovery(myPanicHandler)
	http.ListenAndServe("0.0.0.0:3000", app)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) func(h http.Handler) http.Handler

New returns a middleware that: recovers from panics, logs the panic and backtrace, writes a HTML response and Internal Server Error status.

If a JSON content type is detected it returns a JSON response.

Types

type Option

type Option struct {
	StatusCode     int
	DisableLogging *bool
}

func OptionDisableLogging

func OptionDisableLogging(disable bool) Option

func OptionStatusCode

func OptionStatusCode(statusCode int) Option

Directories

Path Synopsis
You can use the "packr2 clean" command to clean up this, and any other packr generated files.
You can use the "packr2 clean" command to clean up this, and any other packr generated files.

Jump to

Keyboard shortcuts

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