-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed as not planned
Closed as not planned
Copy link
Labels
closed-as-intendedClosed as the reported issue is expected behaviorClosed as the reported issue is expected behaviorlegacy-area-analyzerUse area-devexp instead.Use area-devexp instead.
Description
Dart SDK Version: 3.6.1
Flutter Version: 3.27.3
Problem description:
I am trying to follow the avoid_catches_without_on_clauses linter rule by using on Exception for error handling. However, I’ve encountered an issue where TypeError (a subclass of Error) isn’t caught when processing invalid external data (e.g., API responses). The linter flag for using catch (e) forces me to use on Exception, but this doesn’t catch TypeError, leading to test failures when dealing with invalid data types in API responses.
Expected Behavior:
on Exceptionshould catch errors likeTypeError, which occur during external data validation.
Actual Behavior:
on Exceptiondoes not catchTypeError, causing the test to fail when an invalid type is encountered.
Steps to reproduce: First Option
Test Code: Line 152
test('should return null tokens when tokens are not strings', () {
final response = Response<dynamic>(
requestOptions: RequestOptions(path: '/test'),
data: <String, dynamic>{
'accessToken': 12345, // int (invalid)
'refreshToken': true, // bool (invalid)
},
);
final tokens = netKitManager.extractTokens(response: response);
expect(tokens.accessToken, isNull);
expect(tokens.refreshToken, isNull);
});Implementation Code: Line 29
AuthTokenModel extractTokens({required Response<dynamic> response}) {
try {
if ((response.statusCode ?? 0) >= HttpStatus.internalServerError) {
return const AuthTokenModel();
}
// Ensure response.data is treated as a Map
final data = response.data as Map<String, dynamic>?;
if (data == null) {
return const AuthTokenModel();
}
return AuthTokenModel(
accessToken: data[parameters.accessTokenBodyKey] as String?, // Throws TypeError
refreshToken: data[parameters.refreshTokenBodyKey] as String?,
);
} on Exception catch (_) {
return const AuthTokenModel(); // catch should have been executed on errro
}
}Steps to Reproduce: Second option
- Clone the repository NetKit
- After getting the packages run:
cd packages/net-kit && dart test- see error:
'NetKitManager Extract tokens from body should return null tokens when tokens are not strings'
00:04 +57 -1: Some tests failed.
Behavior
With on Exception:
❌ Test fails (TypeError propagates and isn't caught by on Exception).
With catch (e) (violates the linter rule):
Metadata
Metadata
Assignees
Labels
closed-as-intendedClosed as the reported issue is expected behaviorClosed as the reported issue is expected behaviorlegacy-area-analyzerUse area-devexp instead.Use area-devexp instead.