Answers for "go custom error"

Go
0

go custom error

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 March-26-2021

Browse Popular Code Answers by Language