Skip to content

Question: Will 401 errors always be shown in console / network using Flutter web / WASM? #2476

@large

Description

@large

Hi, I am using Dio 5.9.0 in Flutter web (using WASM) with an interceptor to ensure that bearer is added to the header when a 401 occure.

Example code:

  @override
  Future<void> onError(DioException err, ErrorInterceptorHandler handler) async {
    if (err.response?.statusCode == 401) {
      _refreshTokenFuture ??= _refreshAccessToken();
      final newToken = await _refreshTokenFuture;
      _refreshTokenFuture=null;
      if (newToken != null) {
        // Retry the original request
        final clonedRequest = _retryRequest(err.requestOptions, newToken);
        try {
          final response = await dio.fetch(clonedRequest);
          return handler.resolve(response);
        } catch (e) {
          return handler.next(e as DioException);
        }
      }
      // If refresh fails, newToken == null => pass the 401 up
    }
    //return handler.next(err);
    super.onError(err, handler);
  }

In the Chrome console I get always first 401 failed (release version):
Image

Here is a debug view, with all the js-handlers between, dio_mixin.dart line 532 is last call:
Image

Tried a try / catch block around but it still shows the first 401 error.
Chrome error links to this file:

Future<Response<dynamic>> _dispatchRequest<T>(RequestOptions reqOpt) async {
final cancelToken = reqOpt.cancelToken;
try {
final stream = await _transformData(reqOpt);
final operation = CancelableOperation.fromFuture(
httpClientAdapter.fetch(
reqOpt,
stream,
cancelToken?.whenCancel,
),
);

httpClientAdapter.fetch() will trigger and then catch block should create exception based on assureDioException() which seems right in the state of DioException() and nothing else.

Will always 4xx errors show when using WASM?

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.38.5, on macOS 26.2 25C56 darwin-arm64, locale nb-NO)
[✓] Android toolchain - develop for Android devices (Android SDK version 36.1.0)
[✓] Xcode - develop for iOS and macOS (Xcode 26.2)
[✓] Chrome - develop for the web
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions