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

Misleading error response when JSON data can not be composed #2323

Closed
liquidiert opened this issue Nov 9, 2024 · 8 comments
Closed

Misleading error response when JSON data can not be composed #2323

liquidiert opened this issue Nov 9, 2024 · 8 comments

Comments

@liquidiert
Copy link

liquidiert commented Nov 9, 2024

Package

dio

Version

5.4.3+1

Operating-System

Windows

Adapter

Default Dio

Output of flutter doctor -v

[√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.22631.4317], locale
    en-US)
    • Flutter version 3.24.3 on channel stable at
      C:\Users\redacted\.version-fox\cache\flutter\v-3.24.3\flutter-3.24.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (8 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\redacted\Android\android-sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Users\redacted\.version-fox\cache\java\current\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.2+8-86)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.10.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.10.35122.118
    • Windows 10 SDK version 10.0.22621.0

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html  
      (or visit https://flutter.dev/to/windows-android-setup for detailed instructions).       

[√] VS Code (version 1.95.0-insider)
    • VS Code at C:\Users\redacted\AppData\Local\Programs\Microsoft VS Code Insiders
    • Flutter extension version 3.99.20240930

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version
      10.0.22631.4317]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 130.0.6723.117
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 130.0.2849.68

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

Dart Version

3.5.3

Steps to Reproduce

Was told in #2322 that you do not manipulate error codes. Thus I want to open a bug report stating, that it is misleading to get a 500 exception when Dio failed to compose a valid JSON object.

Steps to reproduce

  1. Create a class with a faulty toJson method:
class DeleteFolder {
  final String root;
  final List<DeleteFolderEntry> entries;

  const DeleteFolder({
    required this.root,
    required this.entries,
  });

  factory DeleteFolder.fromJson(Map<String, dynamic> json) {
    return DeleteFolder(
      root: json["root"],
      entries:
          json["entries"].map((e) => DeleteFolderEntry.fromJson(e)).toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      "root": root,
      "entries": entries
          .map(
            (e) => e.toJson(),
          ), // toList() is missing -> throws an exception like can't use MapEntry as Iterable  
    };
  }
}
  1. fire for example a POST request to your next available API

Expected Result

Dio should respond with a different status code in this case. Maybe 499 so the developer knows it's the clients fault.

Actual Result

Just a Dio exception with a status code 500. When you activate the logging interceptor it outputs the JSON compose error. That helped me solve my problem but I still think a different status code should be used.

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

AlexV525 commented Nov 9, 2024

Could you provide a minimal runnable example so we can directly run and reproduce your issue?

@AlexV525 AlexV525 added the h: need more info Further information is requested label Nov 9, 2024
@liquidiert
Copy link
Author

@AlexV525 sure here it is. The logger interceptor outputs the following:

flutter: *** Request ***
flutter: uri: http://localhost:8000
flutter: method: POST
flutter: responseType: ResponseType.json
flutter: followRedirects: true
flutter: persistentConnection: true
flutter: connectTimeout: null
flutter: sendTimeout: null
flutter: receiveTimeout: null
flutter: receiveDataWhenStatusError: true
flutter: extra: {}
flutter: headers:
flutter:  content-type: application/json
flutter: data:
flutter: Instance of 'Test'
flutter:
flutter: *** DioException ***:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DioException [unknown]: null
Error: Converting object to an encodable object failed: Instance of 'Test'
#0      DioMixin.fetch (package:dio/src/dio_mixin.dart:520:7)
<asynchronous suspension>

flutter: uri: http://localhost:8000
flutter: DioException [unknown]: null
Error: Converting object to an encodable object failed: Instance of 'Test'

But the base exception has a status code of 500.
Also you don't need an API running

@AlexV525
Copy link
Member

But the base exception has a status code of 500. Also you don't need an API running

What is the base exception you are indicating?

@liquidiert
Copy link
Author

liquidiert commented Nov 10, 2024

The exception dio throws in case something is wrong with the response

@AlexV525
Copy link
Member

Your log does not tell 500, and neither your example does. The log interceptor only tells that the conversion error happens.

@liquidiert
Copy link
Author

Try to remove the log interceptor :)

@AlexV525
Copy link
Member

image

@AlexV525
Copy link
Member

Again, we don't manipulate the response. It is very likely that the 500 returned from your server, rather than this library.

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2024
@AlexV525 AlexV525 added i: cannot reproduce and removed h: need more info Further information is requested h: need triage This issue needs to be categorized s: bug Something isn't working labels Nov 10, 2024
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