123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package utils
-
- import (
- "net/http"
-
- "github.com/gin-gonic/gin"
- )
-
- func Must(i interface{}, err error) interface{} {
- if err != nil {
- panic(err)
- }
-
- return i
- }
-
- func Error(ctx *gin.Context, err error) error {
- if err != nil {
- ctx.JSON(http.StatusBadRequest, gin.H{"info": err.Error()})
- return err
- }
- return nil
- }
-
- func Bool(ctx *gin.Context, boolean bool) bool {
- if !boolean {
- ctx.JSON(http.StatusBadRequest, gin.H{"info": "validate failed"})
- return !boolean
- }
- return boolean
- }
-
- func Success(ctx *gin.Context, data ...interface{}) {
- response := gin.H{
- "info": "success",
- }
- if len(data) > 0 {
- response["data"] = data[0]
- }
- ctx.JSON(http.StatusOK, response)
- }
|