57 lines
1.3 KiB
Go

package router
import (
"errors"
"net/http"
"time"
"chatapp3-golang/internal/common"
"github.com/gin-gonic/gin"
)
// writeOK 输出统一成功响应。
func writeOK(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{
"status": true,
"errorCode": 0,
"errorMsg": "success",
"body": data,
"code": "success",
"message": "success",
"data": data,
"time": time.Now().UnixMilli(),
})
}
// writeError 输出统一错误响应。
func writeError(c *gin.Context, err error) {
var appErr *common.AppError
if errors.As(err, &appErr) {
c.JSON(appErr.Status, gin.H{
"status": false,
"errorCode": appErr.Status,
"errorCodeName": appErr.Code,
"errorMsg": appErr.Message,
"body": nil,
"code": appErr.Code,
"message": appErr.Message,
"data": nil,
"time": time.Now().UnixMilli(),
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"status": false,
"errorCode": http.StatusInternalServerError,
"errorCodeName": "internal_error",
"errorMsg": err.Error(),
"body": nil,
"code": "internal_error",
"message": err.Error(),
"data": nil,
"time": time.Now().UnixMilli(),
})
}