package controllers

import (
	"fmt"
	"sparrow/pkg/server"

	"github.com/kataras/iris/v12"
)

const (
	ErrOk       = 0
	ErrNormal   = -1
	ErrDatabase = 10000
)

// ErrorResponse 失败的httpResponse
type ErrorResponse struct {
	Code    int         `json:"code"`    //错误码
	Message string      `json:"message"` //错误信息
	Result  interface{} `json:"result"`  //返回的结果
}

func responseError(ctx iris.Context, code int, message string) {
	server.Log.Errorf("错误:%s", message)
	ctx.JSON(ErrorResponse{
		Code:    code,
		Message: message,
	})
}
func fail(ctx iris.Context, code int, format string, a ...interface{}) {
	ctx.StatusCode(code)
	if format != "" {
		ctx.Values().Set("reason", fmt.Sprintf(format, a))
	}
	ctx.StopExecution()
}
func done(ctx iris.Context, result interface{}) {
	ctx.JSON(ErrorResponse{
		Code:    0,
		Message: "success",
		Result:  result,
	})
}

func badRequest(ctx iris.Context, err error) {
	fail(ctx, iris.StatusBadRequest, "请求参数错误:%s", err.Error())
}
