feat: add recordErrorLog option to NewAPIError for conditional error logging
This commit is contained in:
@@ -47,7 +47,7 @@ func relayHandler(c *gin.Context, relayMode int) *types.NewAPIError {
|
|||||||
err = relay.TextHelper(c)
|
err = relay.TextHelper(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if constant2.ErrorLogEnabled && err != nil {
|
if constant2.ErrorLogEnabled && err != nil && types.IsRecordErrorLog(err) {
|
||||||
// 保存错误日志到mysql中
|
// 保存错误日志到mysql中
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
tokenName := c.GetString("token_name")
|
tokenName := c.GetString("token_name")
|
||||||
|
|||||||
@@ -305,10 +305,10 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
|
|||||||
return 0, 0, types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
|
return 0, 0, types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry())
|
||||||
}
|
}
|
||||||
if userQuota <= 0 {
|
if userQuota <= 0 {
|
||||||
return 0, 0, types.NewErrorWithStatusCode(errors.New("user quota is not enough"), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry())
|
return 0, 0, types.NewErrorWithStatusCode(errors.New("user quota is not enough"), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
|
||||||
}
|
}
|
||||||
if userQuota-preConsumedQuota < 0 {
|
if userQuota-preConsumedQuota < 0 {
|
||||||
return 0, 0, types.NewErrorWithStatusCode(fmt.Errorf("pre-consume quota failed, user quota: %s, need quota: %s", common.FormatQuota(userQuota), common.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry())
|
return 0, 0, types.NewErrorWithStatusCode(fmt.Errorf("pre-consume quota failed, user quota: %s, need quota: %s", common.FormatQuota(userQuota), common.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
|
||||||
}
|
}
|
||||||
relayInfo.UserQuota = userQuota
|
relayInfo.UserQuota = userQuota
|
||||||
if userQuota > 100*preConsumedQuota {
|
if userQuota > 100*preConsumedQuota {
|
||||||
@@ -332,7 +332,7 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
|
|||||||
if preConsumedQuota > 0 {
|
if preConsumedQuota > 0 {
|
||||||
err := service.PreConsumeTokenQuota(relayInfo, preConsumedQuota)
|
err := service.PreConsumeTokenQuota(relayInfo, preConsumedQuota)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden, types.ErrOptionWithSkipRetry())
|
return 0, 0, types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog())
|
||||||
}
|
}
|
||||||
err = model.DecreaseUserQuota(relayInfo.UserId, preConsumedQuota)
|
err = model.DecreaseUserQuota(relayInfo.UserId, preConsumedQuota)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -76,12 +76,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type NewAPIError struct {
|
type NewAPIError struct {
|
||||||
Err error
|
Err error
|
||||||
RelayError any
|
RelayError any
|
||||||
skipRetry bool
|
skipRetry bool
|
||||||
errorType ErrorType
|
recordErrorLog *bool
|
||||||
errorCode ErrorCode
|
errorType ErrorType
|
||||||
StatusCode int
|
errorCode ErrorCode
|
||||||
|
StatusCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *NewAPIError) GetErrorCode() ErrorCode {
|
func (e *NewAPIError) GetErrorCode() ErrorCode {
|
||||||
@@ -278,3 +279,20 @@ func ErrOptionWithSkipRetry() NewAPIErrorOptions {
|
|||||||
e.skipRetry = true
|
e.skipRetry = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ErrOptionWithNoRecordErrorLog() NewAPIErrorOptions {
|
||||||
|
return func(e *NewAPIError) {
|
||||||
|
e.recordErrorLog = common.GetPointer(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsRecordErrorLog(e *NewAPIError) bool {
|
||||||
|
if e == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if e.recordErrorLog == nil {
|
||||||
|
// default to true if not set
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return *e.recordErrorLog
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user