HandleError records an error in the given span, logs the error, and returns a standardized error. This function is used for consistent error handling across different parts of the application.
(ctx context.Context, span trace.Span, err error, errorCode base.ErrorCode)
| 119 | // HandleError records an error in the given span, logs the error, and returns a standardized error. |
| 120 | // This function is used for consistent error handling across different parts of the application. |
| 121 | func HandleError(ctx context.Context, span trace.Span, err error, errorCode base.ErrorCode) error { |
| 122 | // Check if the error is context-related |
| 123 | if IsContextRelatedError(ctx, err) { |
| 124 | slog.DebugContext(ctx, "A context-related error occurred", |
| 125 | slog.String("error", err.Error())) |
| 126 | return errors.New(base.ErrorCode_ERROR_CODE_CANCELLED.String()) |
| 127 | } |
| 128 | |
| 129 | // Check if the error is serialization-related |
| 130 | if IsSerializationRelatedError(err) { |
| 131 | slog.DebugContext(ctx, "A serialization-related error occurred", |
| 132 | slog.String("error", err.Error())) |
| 133 | return errors.New(base.ErrorCode_ERROR_CODE_SERIALIZATION.String()) |
| 134 | } |
| 135 | |
| 136 | // For all other types of errors, log them at the error level and record them in the span |
| 137 | slog.ErrorContext(ctx, "An operational error occurred", |
| 138 | slog.Any("error", err)) |
| 139 | span.RecordError(err) |
| 140 | span.SetStatus(codes.Error, err.Error()) |
| 141 | |
| 142 | // Return a new error with the standard error code provided |
| 143 | return errors.New(errorCode.String()) |
| 144 | } |
| 145 | |
| 146 | // IsContextRelatedError checks if the error is due to context cancellation, deadline exceedance, or closed connection |
| 147 | func IsContextRelatedError(ctx context.Context, err error) bool { |
no test coverage detected