Tweak search results error handling

This commit is contained in:
tommyxchow 2022-01-15 13:16:43 -05:00
parent 5227b80f71
commit 98fabafbdb
4 changed files with 22 additions and 23 deletions

View file

@ -243,16 +243,18 @@ class TwitchApi {
final url = Uri.parse(id != null ? 'https://api.twitch.tv/helix/users?id=$id' : 'https://api.twitch.tv/helix/users?login=$userLogin');
final response = await _client.get(url, headers: headers);
final decoded = jsonDecode(response.body);
if (response.statusCode == 200) {
final userData = jsonDecode(response.body)['data'] as List;
final userData = decoded['data'] as List;
if (userData.isNotEmpty) {
return UserTwitch.fromJson(userData.first);
} else {
throw Exception('User does not exist.');
return Future.error('User does not exist');
}
} else {
throw Exception('Failed to get user.');
return Future.error('Failed to get user: ${decoded['message']}');
}
}
@ -264,16 +266,17 @@ class TwitchApi {
final url = Uri.parse('https://api.twitch.tv/helix/channels?broadcaster_id=$userId');
final response = await _client.get(url, headers: headers);
final decoded = jsonDecode(response.body);
if (response.statusCode == 200) {
final channelData = jsonDecode(response.body)['data'] as List;
final channelData = decoded['data'] as List;
if (channelData.isNotEmpty) {
return Channel.fromJson(channelData.first);
} else {
throw Exception('Channel does not exist');
return Future.error('Channel does not exist');
}
} else {
throw Exception('Failed to get channel.');
return Future.error('Failed to get channel: ${decoded['message']}');
}
}

View file

@ -27,7 +27,7 @@ class SearchResultsCategories extends StatelessWidget {
case FutureStatus.rejected:
return const SliverToBoxAdapter(
child: Center(
child: Text('Failed to get categories.'),
child: Text('Failed to get categories'),
),
);
case FutureStatus.fulfilled:
@ -36,7 +36,7 @@ class SearchResultsCategories extends StatelessWidget {
if (categories == null) {
return const SliverToBoxAdapter(
child: Center(
child: Text('Failed to get categories.'),
child: Text('Failed to get categories'),
),
);
}
@ -45,7 +45,7 @@ class SearchResultsCategories extends StatelessWidget {
return const SliverToBoxAdapter(
child: SizedBox(
height: 100.0,
child: Center(child: Text('No matching categories.')),
child: Center(child: Text('No matching categories')),
),
);
}

View file

@ -33,7 +33,7 @@ class SearchResultsChannels extends StatelessWidget {
context,
MaterialPageRoute(
builder: (context) => VideoChat(
displayName: channelInfo!.broadcasterName,
displayName: channelInfo.broadcasterName,
chatStore: ChatStore(
chatDetailsStore: ChatDetailsStore(
twitchApi: context.read<TwitchApi>(),
@ -52,8 +52,8 @@ class SearchResultsChannels extends StatelessWidget {
),
);
} catch (e) {
const snackBar = SnackBar(
content: Text('Failed to get channel info :('),
final snackBar = SnackBar(
content: Text(e.toString()),
behavior: SnackBarBehavior.floating,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
@ -76,7 +76,7 @@ class SearchResultsChannels extends StatelessWidget {
case FutureStatus.rejected:
return const SliverToBoxAdapter(
child: Center(
child: Text('Failed to fetch streams.'),
child: Text('Failed to get channels'),
),
);
case FutureStatus.fulfilled:
@ -101,7 +101,9 @@ class SearchResultsChannels extends StatelessWidget {
),
)
: null,
subtitle: channel.isLive ? Text('Uptime: ${DateTime.now().difference(DateTime.parse(channel.startedAt)).toString().split('.')[0]}') : null,
subtitle: channel.isLive
? Text('Uptime: ${DateTime.now().difference(DateTime.parse(channel.startedAt)).toString().split('.')[0]}')
: null,
onTap: () => Navigator.push(
context,
MaterialPageRoute(

View file

@ -1,4 +1,3 @@
import 'package:flutter/material.dart';
import 'package:frosty/api/twitch_api.dart';
import 'package:frosty/core/auth/auth_store.dart';
import 'package:frosty/models/category.dart';
@ -68,13 +67,8 @@ abstract class _SearchStoreBase with Store {
.asObservable();
}
Future<Channel?> searchChannel(String query) async {
try {
final user = await twitchApi.getUser(userLogin: query, headers: authStore.headersTwitch);
return await twitchApi.getChannel(userId: user!.id, headers: authStore.headersTwitch);
} catch (e) {
debugPrint(e.toString());
rethrow;
}
Future<Channel> searchChannel(String query) async {
final user = await twitchApi.getUser(userLogin: query, headers: authStore.headersTwitch);
return await twitchApi.getChannel(userId: user.id, headers: authStore.headersTwitch);
}
}