package pkg

import (
	"encoding/json"
	"fmt"

	"github.com/gofiber/fiber/v2"
)

func HandleResponse(c *fiber.Ctx, data interface{}, e error) (err error) {
	if e != nil {
		return e
	}
	switch data.(type) {
	case error:
		err = data.(error)
	case int, int32, int64, float32, float64, string, bool:
		c.Response().SetBody([]byte(fmt.Sprintf("%v", data)))
	case []byte:
		c.Response().SetBody(data.([]byte))
	default:
		dataByte, _ := json.Marshal(data)
		c.Response().SetBody(dataByte)
	}
	return
}
