Skip to content
Snippets Groups Projects
response.go 648 B
Newer Older
package model

Gabriel Zachmann's avatar
Gabriel Zachmann committed
import (
	"github.com/gofiber/fiber/v2"
	"github.com/valyala/fasthttp"
)

type Response struct {
	Status   int
	Response interface{}
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	Cookies  []*fiber.Cookie
}

func (r *Response) Send(ctx *fiber.Ctx) error {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	if fasthttp.StatusCodeIsRedirect(r.Status) {
		ctx.Redirect(r.Response.(string), r.Status)
	}
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	if r.Cookies != nil && len(r.Cookies) > 0 {
		for _, c := range r.Cookies {
			ctx.Cookie(c)
		}
	}
	return ctx.Status(r.Status).JSON(r.Response)
}

func ErrorToInternalServerErrorResponse(err error) *Response {
	return &Response{
		Status:   fiber.StatusInternalServerError,
		Response: InternalServerError(err.Error()),
	}
}