---
title: All Catch Blocks Must Log Root Cause
impact: HIGH
impactDescription: enables debugging and incident response
tags: error-handling, logging, debugging, observability, quality, python, pyspark
---

## All Catch Blocks Must Log Root Cause

Silent failures make debugging impossible. Without proper logging, you cannot trace issues in production. In PySpark, unlogged exceptions in executors can lead to data loss or silent corruption.

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

```python
try:
    process_payment(order)
except Exception:
    pass  # Empty except - silent failure!

try:
    save_user(user)
except Exception as e:
    return None  # No logging, no context
```

**Correct (comprehensive error logging):**

```python
import logging

logger = logging.getLogger(__name__)

try:
    process_payment(order)
except Exception as error:
    logger.error('Payment processing failed', extra={
        'order_id': order.id,
        'user_id': order.user_id,
        'amount': order.amount,
        'error_msg': str(error),
        'exc_info': True,  # Capture stack trace
        'request_id': getattr(context, 'request_id', None)
    })
    raise PaymentFailedError('Payment could not be processed') from error

# PySpark Context
try:
    df.write.save(path)
except Exception as error:
    logger.error(f'Failed to save Spark DataFrame to {path}', exc_info=True)
    raise SparkProcessError(f"Spark write failed for {path}") from error
```

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

**Tools:** Static analyzer, Pylint, PR review
