Fix: Stop reporting network errors as crashes#4432
Conversation
Network connectivity issues (DNS failures, timeouts, connection errors) are expected when users go offline. These were being reported as fatal crashes, causing misleading crash reports. Added _isNetworkError() helper to detect expected network failures and skip crash reporting for them. The app already handles null responses gracefully. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request correctly prevents network-related errors from being reported as crashes by introducing a _isNetworkError helper function. The implementation is sound and applied across all relevant API call methods. My review includes suggestions to improve code readability and reduce duplication by extracting common logic into helper functions, aligning with the principle of preferring helper functions over inlined logic (Rule 4), which will enhance long-term maintainability.
| final message = error.message.toLowerCase(); | ||
| return message.contains('socketexception') || | ||
| message.contains('failed host lookup') || | ||
| message.contains('connection refused') || | ||
| message.contains('connection reset') || | ||
| message.contains('connection closed') || | ||
| message.contains('network is unreachable') || | ||
| message.contains('no route to host') || | ||
| message.contains('nodename nor servname provided'); |
There was a problem hiding this comment.
This long chain of || conditions can be hard to read and maintain. You can improve readability by using a Set of error messages and the any() method.
final message = error.message.toLowerCase();
const networkErrorMessages = {
'socketexception',
'failed host lookup',
'connection refused',
'connection reset',
'connection closed',
'network is unreachable',
'no route to host',
'nodename nor servname provided',
};
return networkErrorMessages.any(message.contains);| } catch (e, stackTrace) { | ||
| Logger.debug('HTTP request failed: $e, $stackTrace'); | ||
| PlatformManager.instance.crashReporter.reportCrash(e, stackTrace, userAttributes: {'url': url, 'method': method}); | ||
| Logger.debug('HTTP request failed: $e'); | ||
| // Only report non-network errors to crash reporter. | ||
| // Network errors (offline, DNS failure, timeouts) are expected and not bugs. | ||
| if (!_isNetworkError(e)) { | ||
| PlatformManager.instance.crashReporter.reportCrash(e, stackTrace, userAttributes: {'url': url, 'method': method}); | ||
| } | ||
| return null; | ||
| } |
There was a problem hiding this comment.
The error handling logic, specifically checking _isNetworkError before reporting a crash, is duplicated across four methods (makeApiCall, makeMultipartApiCall, makeStreamingApiCall, makeMultipartStreamingApiCall). This duplication can lead to maintenance issues. To improve this, you can extract this logic into a dedicated helper function.
For example, you could add a new private function in this file:
void _reportErrorIfNotNetwork(Object e, StackTrace stackTrace, {required String url, required String method}) {
if (!_isNetworkError(e)) {
PlatformManager.instance.crashReporter.reportCrash(e, stackTrace, userAttributes: {'url': url, 'method': method});
}
}Then, you can call this function from each catch block, which will make the code cleaner and easier to maintain. For example, this block would become:
} catch (e, stackTrace) {
Logger.debug('HTTP request failed: $e');
_reportErrorIfNotNetwork(e, stackTrace, url: url, method: method);
return null;
}References
- Prefer using existing helper functions over inlining their logic, especially when they handle complex cases like fallbacks or error handling. Inlining can introduce subtle bugs by missing parts of the original logic. This comment suggests creating a new helper function to avoid duplicating error handling logic, which aligns with the principle of this rule.
|
@mdmohsin7 bump |
Summary
_isNetworkError()helper to detect expected network failures and skip crash reporting for themTest plan
🤖 Generated with Claude Code