Skip to content

Instantly share code, notes, and snippets.

@lil5
Created June 24, 2021 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lil5/7a3efc8feaa0f91772dcdfb57de76b98 to your computer and use it in GitHub Desktop.
Save lil5/7a3efc8feaa0f91772dcdfb57de76b98 to your computer and use it in GitHub Desktop.
Http Error handeling with GoLang Gin
package tools
import (
"net/http"
"github.com/gin-gonic/gin"
)
type HttpErr struct {
Err error
HttpCode int
}
func (e HttpErr) Error() string {
return e.Err.Error()
}
func HandelHttpErr(c *gin.Context, err error) (ok bool) {
if err != nil {
if herr, ok := err.(HttpErr); ok {
c.AbortWithError(herr.HttpCode, herr.Err)
} else {
c.AbortWithError(http.StatusInternalServerError, err)
}
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment