I've come across a scenario where
func main (c *gin.Context){
if err := g.Bind(&data); err != nil {
log.Fatalln(err)
helper.TraceLog(err)
helper.Fail(c, helper.INPUT_PARAMS_ERROR, "", err)
return
}
}
func Fail(c *gin.Context, errorCode string, result string, message interface{}) {
response := Response{}
response.Code = errorCode
response.Result = message
response.Message = result
json.Marshal(response)
c.JSON(http.StatusBadRequest, response)
}
Is it possible that I do not have to pass gin to Fail func? I've tried every solution I can think of and nothing works.
The reason I'm doing this is to make the code looks more simple and clean.
What I'm looking for is something like this:
func main (c *gin.Context){
if err := g.Bind(&data); err != nil {
log.Fatalln(err)
helper.TraceLog(err)
helper.Fail(helper.INPUT_PARAMS_ERROR, "", err)
return
}
}
func Fail(errorCode string, result string, message interface{}) {
var c *gin.Context
response := Response{}
response.Code = errorCode
response.Result = message
response.Message = result
json.Marshal(response)
c.JSON(http.StatusBadRequest, response)
}