|
| 1 | +# Exception Analysis and Recommendations for GoodDataHttpClient |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document analyzes the three TODO comments in `GoodDataHttpClient.java` related to exception handling and provides specific recommendations for appropriate exceptions to throw. |
| 5 | + |
| 6 | +## Current TODO Locations and Analysis |
| 7 | + |
| 8 | +### 1. TODO #1 (Line 299): HttpException in Response Handler |
| 9 | +```java |
| 10 | +public <T> T execute(ClassicHttpRequest request, HttpContext context, |
| 11 | + HttpClientResponseHandler<? extends T> responseHandler) throws IOException { |
| 12 | + final HttpResponse resp = execute(request, context); |
| 13 | + try { |
| 14 | + return responseHandler.handleResponse((ClassicHttpResponse) resp); |
| 15 | + } catch (HttpException e) { |
| 16 | + throw new RuntimeException(e); //TODO:: clarify which exception to use |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +**Context Analysis:** |
| 22 | +- Method declares `throws IOException` |
| 23 | +- `HttpException` is thrown by `responseHandler.handleResponse()` |
| 24 | +- This is a wrapper method implementing the `HttpClient` interface |
| 25 | + |
| 26 | +**Recommendation: Wrap in IOException** |
| 27 | +```java |
| 28 | +} catch (HttpException e) { |
| 29 | + throw new IOException("Failed to handle HTTP response", e); |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +**Rationale:** |
| 34 | +- The method signature only allows `IOException` to be thrown |
| 35 | +- `HttpException` represents HTTP protocol-level issues |
| 36 | +- Wrapping preserves the original cause while conforming to the interface |
| 37 | +- This follows the principle of translating checked exceptions at API boundaries |
| 38 | + |
| 39 | +### 2. TODO #2 (Line 309): URISyntaxException in URI Parsing |
| 40 | +```java |
| 41 | +public HttpResponse execute(ClassicHttpRequest request, HttpContext context) throws IOException { |
| 42 | + final URI uri; |
| 43 | + try { |
| 44 | + uri = request.getUri(); |
| 45 | + } catch (URISyntaxException e) { |
| 46 | + throw new RuntimeException(e); //TODO:: clarify which exception to use |
| 47 | + } |
| 48 | + final HttpHost httpHost = new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort()); |
| 49 | + return execute(httpHost, request, context); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +**Context Analysis:** |
| 54 | +- Method declares `throws IOException` |
| 55 | +- `URISyntaxException` indicates malformed URI in the request |
| 56 | +- This is a client-side configuration error, not a network I/O issue |
| 57 | + |
| 58 | +**Recommendation: Use IllegalArgumentException** |
| 59 | +```java |
| 60 | +} catch (URISyntaxException e) { |
| 61 | + throw new IllegalArgumentException("Invalid URI in request: " + e.getMessage(), e); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +**Rationale:** |
| 66 | +- `URISyntaxException` represents a programming error - invalid input |
| 67 | +- `IllegalArgumentException` is the standard Java exception for invalid arguments |
| 68 | +- The method signature only allows `IOException`, but `IllegalArgumentException` is unchecked |
| 69 | +- This clearly indicates a client-side error rather than a server/network issue |
| 70 | + |
| 71 | +### 3. TODO #3 (Line 367): HttpException in Response Handler (duplicate pattern) |
| 72 | +```java |
| 73 | +public <T> T execute(HttpHost target, ClassicHttpRequest request, HttpContext context, |
| 74 | + HttpClientResponseHandler<? extends T> responseHandler) throws IOException { |
| 75 | + HttpResponse resp = execute(target, request, context); |
| 76 | + try { |
| 77 | + return responseHandler.handleResponse((ClassicHttpResponse) resp); |
| 78 | + } catch (HttpException e) { |
| 79 | + throw new RuntimeException(e); //TODO:: clarify which one to use here |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +**Context Analysis:** |
| 85 | +- Identical pattern to TODO #1 |
| 86 | +- Method declares `throws IOException` |
| 87 | +- Same `HttpException` handling issue |
| 88 | + |
| 89 | +**Recommendation: Same as TODO #1 - Wrap in IOException** |
| 90 | +```java |
| 91 | +} catch (HttpException e) { |
| 92 | + throw new IOException("Failed to handle HTTP response", e); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +**Rationale:** |
| 97 | +- Consistency with TODO #1 resolution |
| 98 | +- Same underlying issue and constraints |
| 99 | + |
| 100 | +## Summary of Recommended Changes |
| 101 | + |
| 102 | +### Exception Mapping Strategy |
| 103 | + |
| 104 | +1. **HttpException → IOException** |
| 105 | + - Used for: Response handling failures |
| 106 | + - Reason: Protocol-level errors that should be treated as I/O issues |
| 107 | + - Pattern: `new IOException("Failed to handle HTTP response", cause)` |
| 108 | + |
| 109 | +2. **URISyntaxException → IllegalArgumentException** |
| 110 | + - Used for: Invalid URI in requests |
| 111 | + - Reason: Client-side configuration error |
| 112 | + - Pattern: `new IllegalArgumentException("Invalid URI in request: " + cause.getMessage(), cause)` |
| 113 | + |
| 114 | +### Implementation Priority |
| 115 | + |
| 116 | +**High Priority (Breaking Changes):** |
| 117 | +- All three TODOs should be resolved together |
| 118 | +- These are runtime behavior changes that could affect error handling |
| 119 | + |
| 120 | +**Benefits of These Changes:** |
| 121 | +1. **Clearer Error Semantics**: Different exception types clearly indicate different error categories |
| 122 | +2. **Better Debugging**: More specific exception messages help identify root causes |
| 123 | +3. **Interface Compliance**: Proper adherence to method signatures |
| 124 | +4. **Consistency**: Uniform approach to exception translation |
| 125 | + |
| 126 | +### Alternative Considerations |
| 127 | + |
| 128 | +**Option 1: Custom GoodData Exceptions** |
| 129 | +Could create `GoodDataHttpException` extending `IOException`, but this adds complexity without clear benefits since the existing exceptions adequately represent the error conditions. |
| 130 | + |
| 131 | +**Option 2: Just Use IOException for All** |
| 132 | +Could wrap `URISyntaxException` in `IOException` for consistency, but `IllegalArgumentException` better represents the nature of URI syntax errors. |
| 133 | + |
| 134 | +**Recommended Approach: Mixed Strategy** |
| 135 | +Use the most semantically appropriate exception type for each case while respecting method signature constraints. |
| 136 | + |
| 137 | +## Code Quality Impact |
| 138 | + |
| 139 | +These changes will: |
| 140 | +- ✅ Improve error handling clarity |
| 141 | +- ✅ Provide better debugging information |
| 142 | +- ✅ Follow Java exception handling best practices |
| 143 | +- ✅ Maintain backward compatibility (exception types, not RuntimeException) |
| 144 | +- ✅ Reduce generic RuntimeException usage |
| 145 | + |
| 146 | +## Testing Considerations |
| 147 | + |
| 148 | +After implementing these changes: |
| 149 | +1. Update tests to expect the new exception types |
| 150 | +2. Verify error messages are descriptive |
| 151 | +3. Ensure exception causes are preserved |
| 152 | +4. Test both success and failure scenarios |
0 commit comments