Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modularize error handling for context-related errors #1005

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Loading