---
title: All Error Handling Must Log Root Cause
impact: HIGH
impactDescription: enables debugging and incident response
tags: error-handling, logging, debugging, observability, quality
---

## All Error Handling Must Log Root Cause

Silent failures make debugging impossible. Without proper logging, you cannot trace issues in production.

**Incorrect (silent or minimal logging):**

```go
func processPayment(order order.Order) {
    err := paymentGateway.Charge(order)
    if err != nil {
        // Silent failure!
        return
    }
}

func saveUser(user *User) error {
    if err := db.Save(user); err != nil {
        return nil // No logging, no context
    }
    return nil
}
```

**Correct (comprehensive error logging/wrapping):**

```go
func processPayment(ctx context.Context, order order.Order) error {
    if err := paymentGateway.Charge(order); err != nil {
        slog.Error("payment processing failed",
            "order_id", order.ID,
            "user_id", order.UserID,
            "amount", order.Amount,
            "error", err,
            "request_id", ctx.Value("request_id"),
        )
        return fmt.Errorf("charging order %s: %w", order.ID, err)
    }
    return nil
}
```

**Log context should include:**
- Error message (and stack trace if available)
- Relevant entity IDs (order, user, etc.)
- Request/correlation ID
- Input that caused the error
- Timing information

**Tools:** GolangCI-Lint, PR review, `slog`
