render.go 1.43 KB
package render

import (
	"errors"
	"net/http"

	"github.com/meiqia/chi/render"

	"client/bulletin/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 NullResultError(w http.ResponseWriter, r *http.Request, err error) {
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugINfoRespNull,
		"msg":  common.Errors[common.BugINfoRespNull] + 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 InternalNetError(w http.ResponseWriter, r *http.Request, err error) {
	if err == nil {
		err = errors.New("网络错误")
	}
	JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.BugInfoInternalNet,
		"msg":  common.Errors[common.BugInfoInternalNet] + 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],
	})
}