Answers for "gin custom error messages"

Go
0

gin custom error messages

package util

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

type Responses struct {
	StatusCode int
	Method     string
	Message    string
	Data       interface{}
}

func APIResponse(ctx *gin.Context, Message string, StatusCode int, Method string, Data interface{}) {

	jsonResponse := Responses{
		StatusCode: StatusCode,
		Method:     Method,
		Message:    Message,
		Data:       Data,
	}

	if StatusCode >= 400 {
		ctx.JSON(StatusCode, jsonResponse)
		defer ctx.AbortWithStatus(StatusCode)
	} else {
		ctx.JSON(StatusCode, jsonResponse)
	}
}
Posted by: Guest on April-24-2021

Code answers related to "gin custom error messages"

Browse Popular Code Answers by Language