Commit 27ff0ae
fix: handle ExceptionGroup and CancelledError in MCP error flow (#87)
* fix: handle ExceptionGroup and CancelledError in MCP error flow
When an MCP connection fails, two issues were masking the real error:
1. Cleanup raises ExceptionGroup containing the HTTP error (Python 3.11+)
2. Session initialization raises CancelledError instead of the real error
This fix addresses both:
**ExceptionGroup handling:**
- Catch BaseException in cleanup to handle ExceptionGroup
- Extract and log individual exceptions from the group
- Gracefully handle Python 3.10 (no ExceptionGroup) and 3.11+
- Log HTTPStatusError at debug level (expected during cleanup)
**CancelledError handling:**
- Catch BaseException in connection loop (not just Exception)
- Properly convert CancelledError to ADCPConnectionError
- Preserve KeyboardInterrupt and SystemExit behavior
Now users see the actual connection error (e.g., "405 Method Not Allowed")
instead of confusing cleanup error messages.
Added test to verify ExceptionGroup handling works correctly.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: improve ExceptionGroup and error handling robustness
Addresses code review feedback to make error handling more robust:
1. Fix ExceptionGroup import - use built-in type directly, not from builtins
2. Add KeyboardInterrupt/SystemExit re-raise in cleanup path
3. Replace string-based HTTPStatusError check with isinstance
4. Update all references from BaseExceptionGroup to ExceptionGroup
These changes improve type safety, handle critical interrupts correctly,
and make the code more maintainable.
All tests pass and mypy type checking succeeds.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: use try/except for ExceptionGroup to satisfy ruff
Ruff doesn't recognize sys.version_info guards for undefined names.
Changed to try/except pattern which both ruff and mypy accept.
Also removed unused sys import since we no longer need version checking.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix: skip ExceptionGroup test on Python 3.10
The test_cleanup_handles_exception_group test uses ExceptionGroup
which doesn't exist in Python 3.10. Added skipif marker to skip
this test on Python < 3.11.
The functionality still works correctly in 3.10 (it gracefully handles
the absence of ExceptionGroup), we just can't test it directly since
we can't create an ExceptionGroup in the test.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent aa933f6 commit 27ff0ae
2 files changed
+94
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
12 | 20 | | |
13 | 21 | | |
14 | 22 | | |
| |||
23 | 31 | | |
24 | 32 | | |
25 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
26 | 42 | | |
27 | 43 | | |
28 | 44 | | |
| |||
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
66 | 91 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
75 | 114 | | |
76 | 115 | | |
77 | 116 | | |
| |||
146 | 185 | | |
147 | 186 | | |
148 | 187 | | |
149 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
150 | 193 | | |
151 | 194 | | |
152 | 195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
844 | 845 | | |
845 | 846 | | |
846 | 847 | | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
0 commit comments