Recovery

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2022 License: MIT Imports: 12 Imported by: 0

README

Recovery Module

Often there are panics causing the runtime to crash. This package is to protect the go-routines spin-up & gin server started with appropriate recovery mechanisms

Sample
package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "log"
    "net/http"
)

func GetTest(c *gin.Context) {
    fmt.Println("serving test")
    c.JSON(http.StatusOK, gin.H{
	    "message": "hello world",
    })
}

func CustomRecoveryFunction() {
    if r := recover(); r != nil {
	    err, ok := r.(error)
	    if !ok {
		    err = fmt.Errorf("%v", r)
	    }
	    fmt.Println("error is", err)

	    newStack := stack(3)
	    log.Printf("%s\n", string(newStack))
    }
}

func DoPanicMain(c *gin.Context) {
    // this does not causes server crash
    fmt.Println("serving panic")
    b := 0
    fmt.Println(2 / b)
}

func DoPanicAsync(c *gin.Context) {
    Execute(DoPanicRoutine)
}

func DoPanicAsyncCustom(c *gin.Context) {
    CustomRecoveryExecute(DoPanicRoutine, CustomRecoveryFunction)
}

func DoPanicRoutine() {
    fmt.Println("serving panic go routine")
    b := 0
    fmt.Println(2 / b)
}

func AbortWithInternalServerError(c *gin.Context, err any) {
    c.JSON(http.StatusInternalServerError, gin.H{
	    "message": "Internal server error",
    })
}

func main() {
    r := gin.New()
    r.Use(gin.Logger())
    r.Use(CustomRecovery(AbortWithInternalServerError))
    r.Use(gin.Recovery())
    r.GET("/test", GetTest)
    r.GET("/panic", DoPanicMain)
    r.GET("/panic/go", DoPanicAsync)
    r.GET("/panic/go/custom", DoPanicAsyncCustom)
    r.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRecovery

func CustomRecovery(handle RecoveryFunc) gin.HandlerFunc

CustomRecovery returns a middleware that recovers from any panics and calls the provided handle func to handle it.

func GoRoutineCustomRecovery

func GoRoutineCustomRecovery(fn func(), recoveryFunc func(err error))

func GoRoutineRecovery

func GoRoutineRecovery(fn func())

Types

type RecoveryFunc

type RecoveryFunc func(c *gin.Context, err interface{})

RecoveryFunc defines the function passable to CustomRecovery.

Jump to

Keyboard shortcuts

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