Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/platform_android/network_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ class NetworkServiceImpl implements NetworkService {
FlutterConfig.get('ACTUATOR_INFO_PATH')))
.timeout(const Duration(seconds: 3));
if (response.statusCode == 200) {
ActuatorInfo actuatorInfo =
ActuatorInfo.fromJson(jsonDecode(response.body));
versionInfo = actuatorInfo.build['version']!;
try {
ActuatorInfo actuatorInfo =
ActuatorInfo.fromJson(jsonDecode(response.body));
versionInfo = actuatorInfo.build['version'] ?? 'Unknown';
} on FormatException catch (e) {
debugPrint('Fatal JSON Parsing Error: ${e.message}');
versionInfo = 'Unknown'; // Safe fallback prevents crash
}
Comment on lines +42 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check ActuatorInfo.fromJson implementation to identify potential exceptions

# Find the ActuatorInfo class definition
ast-grep --pattern $'class ActuatorInfo {
  $$$
}'

# Find the fromJson factory method
ast-grep --pattern $'factory ActuatorInfo.fromJson($$$) {
  $$$
}'

# Check if ActuatorInfo uses code generation (json_serializable)
rg -n "part.*actuator_info.g.dart" lib/model/

# Check the build field declaration
rg -n -A2 "build" lib/model/actuator_info.dart

Repository: mosip/android-registration-client

Length of output: 341


Catch all exceptions to prevent crashes from unexpected JSON structure.

The try-catch only handles FormatException, but ActuatorInfo.fromJson (which uses generated json_serializable code) will throw other exceptions like TypeError if the JSON is valid but structurally unexpected—for example, if the build field is missing or has the wrong type. Catching only FormatException leaves these parsing-related crashes unhandled, which defeats the defensive programming goal.

🛡️ Proposed fix to catch all exceptions
-        } on FormatException catch (e) {
-          debugPrint('Fatal JSON Parsing Error: ${e.message}');
+        } catch (e) {
+          debugPrint('JSON Parsing Error: $e');
           versionInfo = 'Unknown'; // Safe fallback prevents crash
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
ActuatorInfo actuatorInfo =
ActuatorInfo.fromJson(jsonDecode(response.body));
versionInfo = actuatorInfo.build['version'] ?? 'Unknown';
} on FormatException catch (e) {
debugPrint('Fatal JSON Parsing Error: ${e.message}');
versionInfo = 'Unknown'; // Safe fallback prevents crash
}
try {
ActuatorInfo actuatorInfo =
ActuatorInfo.fromJson(jsonDecode(response.body));
versionInfo = actuatorInfo.build['version'] ?? 'Unknown';
} catch (e) {
debugPrint('JSON Parsing Error: $e');
versionInfo = 'Unknown'; // Safe fallback prevents crash
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/platform_android/network_service_impl.dart` around lines 42 - 49, The
current try block only catches FormatException but other parsing errors (e.g.,
TypeError thrown by ActuatorInfo.fromJson) can still crash; change the catch to
a general catch (catch (e, st)) that handles any thrown object, log the error
and stack trace (use debugPrint) and set versionInfo = 'Unknown' as the safe
fallback; reference the ActuatorInfo.fromJson call and the versionInfo
assignment so you update the existing try { ActuatorInfo.fromJson(...) ... } on
FormatException catch (...) block to use a broad catch and preserve the same
fallback behavior.

}
} catch (e) {
debugPrint("Fetch actuator info failed $e");
Expand Down