onError method

  1. @override
Future<void> onError(
  1. DioException err,
  2. ErrorInterceptorHandler handler
)

Called when an exception was occurred during the request.

Implementation

@override
Future<void> onError(
  DioException err,
  ErrorInterceptorHandler handler,
) async {
  final status = err.response?.statusCode;
  if (status == 401) {
    onAuthFailure?.call();
  } else if (status == 403) {
    final body = err.response?.data;
    // Match against the server-side reason strings rest_client also
    // recognises. Anything else (room ban, missing membership) stays
    // a regular 403 — we don't want to log the user out for those.
    final asString = body is String
        ? body.toLowerCase()
        : (body is Map<String, dynamic>
              ? (body['detail'] ?? body['error'] ?? '')
                    .toString()
                    .toLowerCase()
              : '');
    if (asString.contains('user_deactivated') ||
        asString.contains('account_deactivated') ||
        asString.contains('account_banned')) {
      onAuthFailure?.call();
    }
  }
  handler.next(err);
}