shaheeramjad opened a new pull request, #37423:
URL: https://github.com/apache/beam/pull/37423

   ## Description
   
   This PR adds exception chaining (`raise ... from`) throughout the Python SDK 
to preserve error context and improve debuggability. When exceptions are 
re-raised without chaining, the original traceback context is lost, making it 
difficult to identify root causes of failures.
   
   ### Changes Made
   
   1. **CloudSQLEnrichmentHandler** 
(`transforms/enrichment_handlers/cloudsql.py`)
      - Added `from e` to exception re-raises in `_execute_query()` method
      - Preserves original database exception context when database operations 
fail
   
   2. **Process Utilities** (`utils/processes.py`)
      - Added exception chaining for `OSError` and `CalledProcessError` in 
`call()`, `check_call()`, and `check_output()` functions
      - Preserves original subprocess exception context when external 
executables fail
   
   3. **Core Transforms** (`transforms/core.py`)
      - Improved exception logging to include `exc_info=True` when failure 
callbacks fail
      - Preserves full traceback in logs for better debugging
   
   ### Benefits
   
   - **Better Debugging**: Developers can now see the full error chain when 
exceptions occur
   - **Faster Issue Resolution**: Root causes are immediately visible in stack 
traces
   - **Python Best Practice**: Follows PEP 3134 (Exception Chaining and 
Embedded Tracebacks)
   - **Improved Error Messages**: More informative stack traces for production 
debugging
   
   ### Example
   
   **Before:**
   ```python
   except Exception as e:
       raise RuntimeError(f"Database operation failed: {e}")
   ```
   
   **After:**
   ```python
   except Exception as e:
       raise RuntimeError(f"Database operation failed: {e}") from e
   ```
   
   Now when this fails, developers see:
   ```
   RuntimeError: Database operation failed: connection timeout
   The above exception was the direct cause of the following exception:
   ConnectionTimeoutError: Failed to connect within 30 seconds
     [full original traceback here]
   ```
   
   ### Testing
   
   - Syntax validation passed
   - No linter errors introduced
   - Changes preserve existing behavior (only adds exception context)
   - Backward compatible (no breaking changes)
   
   ---
   
   ## Checklist
   
   - [x] Mention the appropriate issue in your description (addresses #37422)
   - [x] Update `CHANGES.md` with noteworthy changes
   - [ ] If this contribution is large, please file an Apache [Individual 
Contributor License Agreement](https://www.apache.org/licenses/icla.pdf). _(Not 
required - this is a small code quality improvement)_
   
   ---
   
   ## Related
   
   - Addresses issue referenced in `.pylintrc` line 139: 
`#TODO(https://github.com/apache/beam/issues/21169)`
   - Follows Python PEP 3134 standard for exception chaining
   - Related to GitHub Issue #37422
   
   
   ------------------------


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to