refactor(logging): Refactor error handling in CreateTag, CreateTagInBatch, and DeleteTag functions
Some checks failed
Gitea Build Check / Build (push) Failing after 29s

Improve error handling in the CreateTag, CreateTagInBatch, and DeleteTag functions by utilizing a new utility function. Simplify logging and error management, ensuring more consistent error responses throughout these operations.
This commit is contained in:
SoXX 2024-08-11 22:34:53 +02:00
parent 4707ea3f00
commit c17b78cc48

View File

@ -3,6 +3,7 @@ package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
@ -22,9 +23,7 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
span.AddEvent("Starting tag creation")
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
tag := models.Tag{
@ -41,24 +40,18 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"name": tagName,
"type": tagType,
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"name": tagName,
"type": tagType,
}).Error(result.Error)
span.RecordError(result.Error)
return models.Tag{}, result.Error
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -81,21 +74,15 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err
span.AddEvent("Starting batch tag creation")
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tags == nil || len(tags) == 0 {
logger.WithContext(ctx).Error(otterError.TagListIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
}
if batchSize == 0 {
logger.WithContext(ctx).Error(otterError.BatchSizeIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -105,17 +92,16 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"tag_length": len(tags),
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return &otterError.Database{Reason: otterError.DuplicateKey}
}
logger.WithContext(ctx).WithFields(log.Fields{
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_length": len(tags),
}).Error(result.Error)
span.RecordError(result.Error)
return result.Error
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -139,15 +125,11 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
var tag models.Tag
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagName) == 0 {
logger.WithContext(ctx).Error(otterError.TagNameIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -157,11 +139,17 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
result := client.WithContext(ctx).Delete(&tag, tagName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
span.RecordError(&otterError.Database{Reason: otterError.NoDataFound})
return &otterError.Database{Reason: otterError.NoDataFound}
loggerFields := log.Fields{
"tag_name": tagName,
}
span.RecordError(result.Error)
return result.Error
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
}
loggerFields := log.Fields{
"tag_name": tagName,
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{