Skip to content

Commit

Permalink
Fix issue #159 The error message is not shown for feed or correct for…
Browse files Browse the repository at this point in the history
… searches/trend.
  • Loading branch information
j-fbriere committed Jan 16, 2024
1 parent 2b8d309 commit a8f62f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
17 changes: 12 additions & 5 deletions lib/client_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TwitterAndroid {
static const String _oauthConsumerKey = '3nVuSoBZnx6U4vzUxf5w';
static const String _oauthConsumerSecret = 'Bcs59EFbbsdF6Sl9Ng71smgStWEGwXXKSjYvPVt7qys';

static GuestAccountException? _lastGuestAccountExc;
static Map? _guestAccountTokens;
static final List<Map<String,Object?>> _guestAccountTokensLst = [];
static final Map<String,List<Map<String,int>>> _rateLimits = {};
Expand Down Expand Up @@ -74,7 +75,7 @@ class TwitterAndroid {

static Future<void> initGuestAccount(String uriPath, int total) async {
// first try to create a guest account if it's been at least 24 hours since the last creation
GuestAccountException? lastGuestAccountExc;
_lastGuestAccountExc = null;
if (_guestAccountTokensLst.isEmpty || DateTime.now().difference(_guestAccountTokensLst.last['createdAt'] as DateTime).inHours >= 24) {
try {
Map<String,Object?> guestAccount = await _createGuestAccountTokens();
Expand All @@ -84,15 +85,15 @@ class TwitterAndroid {
}
on GuestAccountException catch (_, ex) {
log.warning('*** Try to create a guest account after 24 hours with error: ${_.toString()}');
lastGuestAccountExc = _;
_lastGuestAccountExc = _;
}
}

// now find the first guest account that is available or at least with the minimum waiting time
Map<String,dynamic>? guestAccountInfo = getNextGuestAccount(uriPath, total);
if (guestAccountInfo == null) {
if (lastGuestAccountExc != null) {
throw lastGuestAccountExc;
if (_lastGuestAccountExc != null) {
throw _lastGuestAccountExc!;
}
else {
throw GuestAccountException('There is a problem getting a guest account.');
Expand Down Expand Up @@ -343,6 +344,12 @@ class TwitterAndroid {
}

static Future<String> _getSignOauth(Uri uri, String method) async {
if (_lastGuestAccountExc != null) {
throw _lastGuestAccountExc!;
}
if (_guestAccountTokens == null) {
throw GuestAccountException('There is a problem getting a guest account.');
}
Map guestAccountTokens = _guestAccountTokens!;
Map<String,String> params = Map<String,String>.from(uri.queryParameters);
params['oauth_version'] = '1.0';
Expand Down Expand Up @@ -397,7 +404,7 @@ class TwitterAndroid {
static Future<http.Response> fetch(Uri uri, {Map<String, String>? headers, RateFetchContext? fetchContext}) async {
if (fetchContext == null) {
fetchContext = RateFetchContext(uri.path, 1);
fetchContext.init();
await fetchContext.init();
}

http.Response rsp = await _doFetch(uri, fetchContext, headers: headers);
Expand Down
16 changes: 9 additions & 7 deletions lib/group/_feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
} catch (e, stackTrace) {
if (e is Exception) {
log.severe(e.toString());
_errorResponse ??= ExceptionResponse(e);
setState(() {
_errorResponse ??= ExceptionResponse(e);
});
}
if (mounted) {
// probably something to do
Expand Down Expand Up @@ -433,17 +435,17 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
}
});

if (widget.chunks.isEmpty) {
if (_errorResponse != null && _data.isEmpty && (_errorResponse!.statusCode < 200 || _errorResponse!.statusCode >= 300)) {
return Scaffold(
body: Center(
child: Text(L10n.of(context).this_group_contains_no_subscriptions),
),
body: FullPageErrorWidget(error: _errorResponse, prefix: 'Error request Twitter/X', stackTrace: null)
);
}

if (_errorResponse != null && _data.isEmpty && (_errorResponse!.statusCode < 200 || _errorResponse!.statusCode >= 300)) {
if (widget.chunks.isEmpty) {
return Scaffold(
body: FullPageErrorWidget(error: _errorResponse, prefix: 'Error request Twitter/X', stackTrace: null)
body: Center(
child: Text(L10n.of(context).this_group_contains_no_subscriptions),
),
);
}

Expand Down

0 comments on commit a8f62f2

Please sign in to comment.