utils.go 955 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package middleware
  2. import (
  3. "fmt"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/logger"
  6. "github.com/QuantumNous/new-api/types"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func abortWithOpenAiMessage(c *gin.Context, statusCode int, message string, code ...types.ErrorCode) {
  10. codeStr := ""
  11. if len(code) > 0 {
  12. codeStr = string(code[0])
  13. }
  14. userId := c.GetInt("id")
  15. c.JSON(statusCode, gin.H{
  16. "error": gin.H{
  17. "message": common.MessageWithRequestId(message, c.GetString(common.RequestIdKey)),
  18. "type": "new_api_error",
  19. "code": codeStr,
  20. },
  21. })
  22. c.Abort()
  23. logger.LogError(c.Request.Context(), fmt.Sprintf("user %d | %s", userId, message))
  24. }
  25. func abortWithMidjourneyMessage(c *gin.Context, statusCode int, code int, description string) {
  26. c.JSON(statusCode, gin.H{
  27. "description": description,
  28. "type": "new_api_error",
  29. "code": code,
  30. })
  31. c.Abort()
  32. logger.LogError(c.Request.Context(), description)
  33. }