Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The catch block is not triggered when parsing the json into an object fails #2162

Closed
phamquoctrongnta opened this issue Mar 26, 2024 · 4 comments

Comments

@phamquoctrongnta
Copy link

phamquoctrongnta commented Mar 26, 2024

Package

dio

Version

5.4.2

Operating-System

Android, iOS

Adapter

Default Dio

Output of flutter doctor -v

[✓] Flutter (Channel stable, 3.19.2, on macOS 14.0 23A344 darwin-arm64, locale en-VN)
    • Flutter version 3.19.2 on channel stable at /Users/user/fvm/versions/3.19.2
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7482962148 (4 weeks ago), 2024-02-27 16:51:22 -0500
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/user/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A240d
    • CocoaPods version 1.15.0

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (2 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64  • macOS 14.0 23A344 darwin-arm64
    ! Error: Browsing on the local area network for iPhone 11. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Dart Version

No response

Steps to Reproduce

Sorry for my bad English, hope you understand.
When I intentionally parse the json by entering the wrong key, the catch block is not triggered.
Wrong key: title1
Right key: title

Future<void> _testing() async {
  final dio = Dio();
  try {
    final response = await dio.get('https://api.sampleapis.com/coffee/hot');
   // ISSUE: "title1" is wrong key. Right key is "title".
    final listBody = (response.data as List).map((e) => e['title1'] as String).toList(); 
    for(var i in listBody){
      log(i, name: 'Try');
    }
  } on Exception catch (e) {
    log(e.toString(), name: 'Catch');
  }
}

JSON:

[
    {
        "title": "Black Coffee",
        "description": "Svart kaffe är så enkelt som det kan bli med malda kaffebönor dränkta i hett vatten, serverat varmt. Och om du vill låta fancy kan du kalla svart kaffe med sitt rätta namn: café noir.",
        "ingredients": [
            "Coffee"
        ],
        "image": "https://images.unsplash.com/photo-1494314671902-399b18174975?auto=format&fit=crop&q=80&w=1887&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
        "id": 1
    },
]

Expected Result

catch block triggered when parsing json to an object fails.

Actual Result

catch block NOT triggered when parsing json to an object fails.

@phamquoctrongnta phamquoctrongnta added h: need triage This issue needs to be categorized s: bug Something isn't working labels Mar 26, 2024
@AlexV525
Copy link
Member

It throws TypeError, which is not a subtype of Exception that you're catching.

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 26, 2024
@AlexV525 AlexV525 added i: not related and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels Mar 26, 2024
@phamquoctrongnta
Copy link
Author

@AlexV525
What is the best practice in this case?

@AlexV525
Copy link
Member

@AlexV525 What is the best practice in this case?

In this case, it should catch anything.

@phamquoctrongnta
Copy link
Author

phamquoctrongnta commented Mar 26, 2024

@AlexV525
I got it.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants