Handle plain-text upstream API errors#286
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Code ReviewOverall this is a solid, focused fix for a real problem: plain-text gateway errors (e.g. 503 from Istio/Envoy) were previously swallowed into a noisy "Failed to parse ApiError" message. The approach – check Content-Type and body heuristics before attempting JSON deserialization – is pragmatic and correct for the common cases. Bugs / Correctness1. Empty body with In if (!string.IsNullOrWhiteSpace(mediaType)) {
if (normalizedMediaType.Contains("json", ...))
return true; // <-- returns true even when body is empty/null
}
if (string.IsNullOrWhiteSpace(body)) {
return false; // <-- never reached when Content-Type is JSON
}If a server responds with private static bool ShouldAttemptJsonParse(HttpResponseMessage response, string body) {
if (string.IsNullOrWhiteSpace(body))
return false;
string? mediaType = response.Content.Headers.ContentType?.MediaType;
if (!string.IsNullOrWhiteSpace(mediaType) &&
mediaType.Contains("json", StringComparison.OrdinalIgnoreCase))
return true;
ReadOnlySpan<char> trimmed = body.AsSpan().TrimStart();
return !trimmed.IsEmpty && (trimmed[0] == '{' || trimmed[0] == '[');
}Minor Issues2. Redundant normalizedMediaType.Contains("json", StringComparison.OrdinalIgnoreCase) ||
normalizedMediaType.EndsWith("+json", StringComparison.OrdinalIgnoreCase)Any string that ends with 3. The string normalizedMediaType = mediaType!;This is just a copy of Tests4. No test for the Content-Type-based detection path The new 5. The 503 test verifies the message content but doesn't assert Summary
The core logic is sound and the tests for the primary scenarios are correct. Fixing the empty-body edge case would make this fully robust. |
Code ReviewThis is a clean, well-targeted fix. The core problem — plain-text gateway responses (like Istio/Envoy 503s) causing a misleading "Failed to parse ApiError" message — is real and the approach to solve it is sound. Overall the PR is in good shape. A few observations below.
|
| Area | Assessment |
|---|---|
| Bug fix correctness | Correct — resolves the plain-text upstream error noise |
ShouldAttemptJsonParse design |
Clean; heuristic is appropriate for this API |
| Test coverage | Good — all main code paths exercised |
| Breaking changes | None |
| Minor suggestions | Cache JsonSerializerOptions; clarify one test name |
Good change overall.
Summary
Verification