onError is not returning any error
AnkitKh095 opened this issue · 3 comments
AnkitKh095 commented
Package
dio
Version
^5.4.3+1
Operating-System
Android, iOS
Adapter
Default Dio
Output of flutter doctor -v
flutter doctor -v
[✓] Flutter (Channel stable, 3.22.2, on macOS 14.4.1 23E224 darwin-arm64, locale en-IN)
• Flutter version 3.22.2 on channel stable at /Users/ankitkhaire/Documents/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 761747bfc5 (3 weeks ago), 2024-06-05 22:15:13 +0200
• Engine revision edd8546116
• Dart version 3.4.3
• DevTools version 2.34.3
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/ankitkhaire/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_SDK_ROOT = /Users/ankitkhaire/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15F31d
• CocoaPods version 1.14.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.3)
• 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.10+0-17.0.10b1087.21-11572160)
[✓] VS Code (version 1.90.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.90.0
Dart Version
3.4.3
Steps to Reproduce
- package version:-- ^5.4.3+1.
onError: (DioException e, handler) async {
print("e---${e.response?.statusCode}");
if (e.response?.statusCode == 401 &&
e.requestOptions.extra['refreshed'] != true) {
try {
String? newToken = await refreshTokenAPI();
if (newToken != null) {
e.requestOptions.headers['Authorization'] =
newToken; // Correctly set the new Bearer token
e.requestOptions.extra['refreshed'] = true;
return handler.resolve(await _dio.fetch(e.requestOptions));
}
} catch (e) {
print("Error while refreshing token: $e");
throw e;
}
} else if (e.response?.statusCode == 404) {
print("404 error: ${e.response?.data}");
} else {
print("Non-401 error occurred: ${e.response?.statusCode}");
print("Non-401 error occurred: ${e.response?.data}");
}
return handler.next(e);
},
Expected Result
- the onError should return the entire error response.
Actual Result
if there is any Error from the api request on Error method from the interceptor not giving any logs for the same.
AlexV525 commented
- Do not
return handler.resolve
. - Use
handler.reject
rather thanthrow e
.
AnkitKh095 commented
hello,
Thank you for the prompt response.
here if I make any api call using same instance and if there is any error like 401 or 422,404 i am not getting any logs in inteceptor's onError.
AlexV525 commented
You'll fall into an infinite loop using the same instance. Check the example about how to refresh tokens properly at https://github.com/cfug/dio/blob/main/example_dart/lib/queued_interceptor_crsftoken.dart.