mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-10 10:33:41 +08:00
feat: implement centralized error response handling and add logger middleware
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user