|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "log/slog" |
6 | 7 |
|
@@ -124,9 +125,22 @@ func HandleError(span trace.Span, err error, errorCode base.ErrorCode) error { |
124 | 125 | // Set the status of the span |
125 | 126 | span.SetStatus(codes.Error, err.Error()) |
126 | 127 |
|
127 | | - // Log the error |
128 | | - slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode)) |
| 128 | + // Check if the error is context-related |
| 129 | + if IsContextRelatedError(err) { |
| 130 | + // Use debug level logging for context-related errors |
| 131 | + slog.Debug("Context-related error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode)) |
| 132 | + } else { |
| 133 | + // Use error level logging for all other errors |
| 134 | + slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode)) |
| 135 | + } |
129 | 136 |
|
130 | 137 | // Return a new standardized error with the provided error code |
131 | 138 | return errors.New(errorCode.String()) |
132 | 139 | } |
| 140 | + |
| 141 | +// IsContextRelatedError checks if the error is due to context cancellation, deadline exceedance, or closed connection |
| 142 | +func IsContextRelatedError(err error) bool { |
| 143 | + return errors.Is(err, context.Canceled) || |
| 144 | + errors.Is(err, context.DeadlineExceeded) || |
| 145 | + err.Error() == "conn closed" |
| 146 | +} |
0 commit comments