Skip to content

Commit

Permalink
refactor: enhance error logging with additional context and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
azalio committed Dec 28, 2024
1 parent 4d3ab53 commit 0da070b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
20 changes: 14 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,11 @@ func (a *App) handleMemeCommand(ctx context.Context, update tgbotapi.Update, arg
processingMsg, err := a.bot.SendMessage(ctx, update.Message.Chat.ID, "Генерирую мем, пожалуйста подождите...")
if err != nil {
a.log.Error(ctx, "Failed to send start message", map[string]interface{}{
"error": err.Error(),
"chat_id": update.Message.Chat.ID,
"user": update.Message.From.UserName,
"error": err.Error(),
"chat_id": update.Message.Chat.ID,
"user": update.Message.From.UserName,
"command": "meme",
"function": "handleMemeCommand",
})
return fmt.Errorf("failed to send start message: %w", err)
}
Expand All @@ -365,6 +367,9 @@ func (a *App) handleMemeCommand(ctx context.Context, update tgbotapi.Update, arg
"orig_err": err.Error(),
"chat_id": update.Message.Chat.ID,
"user_name": update.Message.From.UserName,
"command": "meme",
"function": "handleMemeCommand",
"prompt": args,
})
}
return fmt.Errorf("failed to generate image: %w", err)
Expand All @@ -374,9 +379,12 @@ func (a *App) handleMemeCommand(ctx context.Context, update tgbotapi.Update, arg
// Fail Gracefully Pattern: Продолжаем даже при ошибке удаления
if err := a.bot.DeleteMessage(ctx, update.Message.Chat.ID, processingMsg.MessageID); err != nil {
a.log.Error(ctx, "Failed to delete generation message", map[string]interface{}{
"error": err.Error(),
"chat_id": update.Message.Chat.ID,
"msg_id": processingMsg.MessageID,
"error": err.Error(),
"chat_id": update.Message.Chat.ID,
"msg_id": processingMsg.MessageID,
"command": "meme",
"function": "handleMemeCommand",
"user": update.Message.From.UserName,
})
}

Expand Down
7 changes: 5 additions & 2 deletions internal/service/fusion_brain_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewFusionBrainService(log *logger.Logger) *FusionBrainServiceImpl {
if apiKey == "" || secretKey == "" {
log.Error(context.Background(), "Environment variables not set", map[string]interface{}{
"missing_vars": []string{"FUSION_BRAIN_API_KEY", "FUSION_BRAIN_SECRET_KEY"},
"service": "fusion_brain",
})
return nil
}
Expand Down Expand Up @@ -180,8 +181,10 @@ func (s *FusionBrainServiceImpl) checkAvailability(ctx context.Context) (bool, e
fmt.Sprintf(fusionBrainBaseURL+"key/api/v1/text2image/availability?model_id=%d", s.modelID), nil)
if err != nil {
s.logger.Error(ctx, "Failed to create availability check request", map[string]interface{}{
"error": err.Error(),
"modelID": s.modelID,
"error": err.Error(),
"modelID": s.modelID,
"service": "fusion_brain",
"function": "checkAvailability",
})
return false, fmt.Errorf("creating request: %w", err)
}
Expand Down

0 comments on commit 0da070b

Please sign in to comment.