package controllers

import (
	"fmt"

	beego "github.com/beego/beego/v2/server/web"
)

func HandleControllerErrors(c beego.Controller, code int, err *error, data *map[string]interface{}, messages ...string) {
	for _, mess := range messages {
		fmt.Println(mess)
	}
	if data != nil {
		c.Data["json"] = data
	}
	if err != nil {
		c.Data["json"] = map[string]string{"error": (*err).Error()}
	}
	c.Ctx.Output.SetStatus(code)
	c.ServeJSON()
}