utils.go 898 B

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