feat: implement centralized error response handling and add logger middleware

This commit is contained in:
Laisky.Cai
2025-02-26 00:52:24 +00:00
parent 607406dae1
commit db992c26c4
5 changed files with 595 additions and 111 deletions

View File

@@ -6,11 +6,14 @@ import (
"html/template"
"log"
"net"
"net/http"
"os/exec"
"runtime"
"strconv"
"strings"
gmw "github.com/Laisky/gin-middlewares/v6"
"github.com/Laisky/zap"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common/random"
)
@@ -31,6 +34,16 @@ func OpenBrowser(url string) {
}
}
// RespondError sends a JSON response with a success status and an error message.
func RespondError(c *gin.Context, err error) {
logger := gmw.GetLogger(c)
logger.Error("http server error", zap.Error(err))
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
}
func GetIp() (ip string) {
ips, err := net.InterfaceAddrs()
if err != nil {