Просмотр исходного кода

feat: print user id when error

CalciumIon 1 год назад
Родитель
Сommit
ab1d61d910
3 измененных файлов с 6 добавлено и 4 удалено
  1. 2 2
      common/utils.go
  2. 2 1
      controller/relay.go
  3. 2 1
      middleware/utils.go

+ 2 - 2
common/utils.go

@@ -179,8 +179,8 @@ func Max(a int, b int) int {
 	}
 }
 
-func MessageWithRequestId(message string, id string) string {
-	return fmt.Sprintf("%s (request id: %s)", message, id)
+func MessageWithRequestId(message string, id string, userId int) string {
+	return fmt.Sprintf("%s (request id: %s, user id: %d)", message, id, userId)
 }
 
 func RandomSleep() {

+ 2 - 1
controller/relay.go

@@ -82,7 +82,8 @@ func Relay(c *gin.Context) {
 		if openaiErr.StatusCode == http.StatusTooManyRequests {
 			openaiErr.Error.Message = "当前分组上游负载已饱和,请稍后再试"
 		}
-		openaiErr.Error.Message = common.MessageWithRequestId(openaiErr.Error.Message, requestId)
+		userId := c.GetInt("id")
+		openaiErr.Error.Message = common.MessageWithRequestId(openaiErr.Error.Message, requestId, userId)
 		c.JSON(openaiErr.StatusCode, gin.H{
 			"error": openaiErr.Error,
 		})

+ 2 - 1
middleware/utils.go

@@ -6,9 +6,10 @@ import (
 )
 
 func abortWithOpenAiMessage(c *gin.Context, statusCode int, message string) {
+	userId := c.GetInt("id")
 	c.JSON(statusCode, gin.H{
 		"error": gin.H{
-			"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
+			"message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey), userId),
 			"type":    "new_api_error",
 		},
 	})