Compare commits

..

No commits in common. "c17b78cc48779d838df8277e54ec6e5ce4517759" and "bb64c08185d885a76e8fce88099e0ec4b0d749fe" have entirely different histories.

2 changed files with 42 additions and 43 deletions

View File

@ -1,13 +0,0 @@
package utils
import (
"context"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)
func HandleError(ctx context.Context, span trace.Span, logger *log.Logger, fields log.Fields, error error) error {
logger.WithContext(ctx).WithFields(fields).Error(error)
span.RecordError(error)
return error
}

View File

@ -3,7 +3,6 @@ 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"
@ -23,7 +22,9 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
span.AddEvent("Starting tag creation")
if client == nil {
return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
tag := models.Tag{
@ -40,18 +41,24 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
loggerFields := log.Fields{
logger.WithContext(ctx).WithFields(log.Fields{
"name": tagName,
"type": tagType,
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
loggerFields := log.Fields{
logger.WithContext(ctx).WithFields(log.Fields{
"name": tagName,
"type": tagType,
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}).Error(result.Error)
span.RecordError(result.Error)
return models.Tag{}, result.Error
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -74,15 +81,21 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err
span.AddEvent("Starting batch tag creation")
if client == nil {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if tags == nil || len(tags) == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
logger.WithContext(ctx).Error(otterError.TagListIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}
}
if batchSize == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
logger.WithContext(ctx).Error(otterError.BatchSizeIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -92,16 +105,17 @@ 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) {
loggerFields := log.Fields{
logger.WithContext(ctx).WithFields(log.Fields{
"tag_length": len(tags),
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
logger.WithContext(ctx).WithFields(log.Fields{
"tag_length": len(tags),
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}).Error(result.Error)
span.RecordError(result.Error)
return result.Error
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -125,11 +139,15 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
var tag models.Tag
if client == nil {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(tagName) == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty})
logger.WithContext(ctx).Error(otterError.TagNameIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -139,17 +157,11 @@ 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) {
loggerFields := log.Fields{
"tag_name": tagName,
span.RecordError(&otterError.Database{Reason: otterError.NoDataFound})
return &otterError.Database{Reason: otterError.NoDataFound}
}
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)
span.RecordError(result.Error)
return result.Error
}
logger.WithContext(ctx).WithFields(log.Fields{