render.go 1.48 KB
package render

import (
	"errors"
	"net/http"

	"github.com/meiqia/chi/render"

	"client/gift/common"
)

func JSON(w http.ResponseWriter, r *http.Request, statusCode int, resp interface{}) {
	render.Status(r, statusCode)
	render.JSON(w, r, resp)
}

func BindError(w http.ResponseWriter, r *http.Request, err error) {
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugInfoBindFailed,
		"msg":  common.Errors[common.BugInfoBindFailed] + err.Error(),
	})
}

func InternalError(w http.ResponseWriter, r *http.Request, err error) {
	if err == nil {
		err = errors.New("未知错误")
	}
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugInfoInternalError,
		"msg":  common.Errors[common.BugInfoInternalError] + err.Error(),
	})
}

func InternalNullError(w http.ResponseWriter, r *http.Request, err error) {
	if err == nil {
		err = errors.New("未知错误")
	}
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugInfoClientNull,
		"msg":  common.Errors[common.BugInfoClientNull] + err.Error(),
	})
}

func TaskRepeatError(w http.ResponseWriter, r *http.Request, err error) {
	if err == nil {
		err = errors.New(" taskId 重复 ")
	}
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugInfoTaskRepeat,
		"msg":  common.Errors[common.BugInfoTaskRepeat] + err.Error(),
	})
}

func Success(w http.ResponseWriter, r *http.Request) {
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.OK,
		"msg":  common.Errors[common.OK],
	})
}