Skip to content

Commit

Permalink
refactor: modularize error handling for context-related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgaOzen committed Jan 12, 2024
1 parent f291920 commit 5e17308
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/storage/postgres/utils/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"fmt"
"log/slog"

Expand Down Expand Up @@ -124,9 +125,22 @@ func HandleError(span trace.Span, err error, errorCode base.ErrorCode) error {
// Set the status of the span
span.SetStatus(codes.Error, err.Error())

// Log the error
slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))
// Check if the error is context-related
if IsContextRelatedError(err) {
// Use debug level logging for context-related errors
slog.Debug("Context-related error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))
} else {
// Use error level logging for all other errors
slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))
}

// Return a new standardized error with the provided error code
return errors.New(errorCode.String())
}

// IsContextRelatedError checks if the error is due to context cancellation, deadline exceedance, or closed connection
func IsContextRelatedError(err error) bool {
return errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded) ||
err.Error() == "conn closed"
}

0 comments on commit 5e17308

Please sign in to comment.