Skip to content

Gin middleware to render a nice looking error page when recovering from a panic.

License

Notifications You must be signed in to change notification settings

ekyoung/gin-nice-recovery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

gin-nice-recovery

Gin middleware to provide a nice user experience when recovering from a panic.

Why?

The default gin.Recovery() middleware leaves the user looking a blank white page. This middleware calls the specified handler, which can render a nice looking error page, return customized error JSON, or whatever is required. It logs the same HTTP request information and stack trace as gin.Recovery().

Installation

$ go get github.com/ekyoung/gin-nice-recovery

Usage

package main

import (
    "github.com/gin-gonic/gin"

    "github.com/ekyoung/gin-nice-recovery"
)

func main() {
    router := gin.New()      // gin.Default() installs gin.Recovery() so use gin.New() instead
    router.Use(gin.Logger()) // Install the default logger, not required

    // Install nice.Recovery, passing the handler to call after recovery
    router.Use(nice.Recovery(recoveryHandler))

    // Load templates as usual
    router.LoadHTMLFiles("error.tmpl")

    // Define routes as usual
    router.GET("/", func(c *gin.Context) {
        panic("Doh!")
    })

    router.Run(":8080")
}

func recoveryHandler(c *gin.Context, err interface{}) {
    c.HTML(500, "error.tmpl", gin.H{
        "title": "Error",
        "err":   err,
    })
}

Complete example code can be found in the example repository.

About

Gin middleware to render a nice looking error page when recovering from a panic.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages