catch panic in gin
This commit is contained in:
parent
cebb2ae2b6
commit
679277d59e
@ -54,4 +54,6 @@ const (
|
|||||||
FIREBASE_COM_FAILED APIError = 9901
|
FIREBASE_COM_FAILED APIError = 9901
|
||||||
FIREBASE_COM_ERRORED APIError = 9902
|
FIREBASE_COM_ERRORED APIError = 9902
|
||||||
INTERNAL_EXCEPTION APIError = 9903
|
INTERNAL_EXCEPTION APIError = 9903
|
||||||
|
PANIC APIError = 9904
|
||||||
|
NOT_IMPLEMENTED APIError = 9905
|
||||||
)
|
)
|
||||||
|
@ -91,7 +91,7 @@ func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NotImplemented(g *gin.Context) HTTPResponse {
|
func NotImplemented(g *gin.Context) HTTPResponse {
|
||||||
return createApiError(g, "NotImplemented", 500, apierr.UNDEFINED, 0, "Not Implemented", nil)
|
return createApiError(g, "NotImplemented", 500, apierr.NOT_IMPLEMENTED, 0, "Not Implemented", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
|
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) HTTPResponse {
|
||||||
|
@ -2,6 +2,9 @@ package ginresp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
scn "blackforestbytes.com/simplecloudnotifier"
|
scn "blackforestbytes.com/simplecloudnotifier"
|
||||||
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -26,7 +29,11 @@ func Wrap(fn WHandlerFunc) gin.HandlerFunc {
|
|||||||
|
|
||||||
for ctr := 1; ; ctr++ {
|
for ctr := 1; ; ctr++ {
|
||||||
|
|
||||||
wrap := fn(g)
|
wrap, panicObj := callPanicSafe(fn, g)
|
||||||
|
if panicObj != nil {
|
||||||
|
log.Error().Interface("panicObj", panicObj).Msg("Panic occured (in gin handler)")
|
||||||
|
wrap = APIError(g, 500, apierr.PANIC, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
|
||||||
|
}
|
||||||
|
|
||||||
if g.Writer.Written() {
|
if g.Writer.Written() {
|
||||||
panic("Writing in WrapperFunc is not supported")
|
panic("Writing in WrapperFunc is not supported")
|
||||||
@ -55,6 +62,18 @@ func Wrap(fn WHandlerFunc) gin.HandlerFunc {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, panicObj any) {
|
||||||
|
defer func() {
|
||||||
|
if rec := recover(); rec != nil {
|
||||||
|
res = nil
|
||||||
|
panicObj = rec
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
res = fn(g)
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
func resetBody(g *gin.Context) error {
|
func resetBody(g *gin.Context) error {
|
||||||
if g.Request.Body == nil {
|
if g.Request.Body == nil {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user