Skip to content

Commit

Permalink
✨ add error handling for the classes page
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuta1409 committed May 31, 2024
1 parent 2585e3b commit 06e37ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:app_student/api/class_groups/entities/class_group_entity.dart';
Expand All @@ -19,16 +20,18 @@ class ClassGroupRepository {
throw FormatException('Failed to parse JSON: $e');
}
});
} on HttpException catch (he) {
throw Exception('HTTP error: $he');
} on FormatException catch (fe) {
throw Exception('Failed to parse JSON: $fe');
} on SocketException catch (se) {
throw Exception('No internet connection: $se');
} on TypeError catch (te) {
throw Exception('Type error: $te');
} on ArgumentError catch (_) {
rethrow;
} on SocketException catch (_) {
rethrow;
} on FormatException catch (_) {
rethrow;
} on TimeoutException catch (_) {
rethrow;
} on HttpException catch (_) {
rethrow;
} catch (e) {
throw Exception('Failed to load data: $e');
throw Exception(e.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class ClassGroupCubit extends Cubit<ClassGroupState> {
emit(ClassGroupLoading());
final classes = await classRepository.getClasses();
emit(ClassGroupLoaded(classes));
} on HttpException catch (he) {
emit(ClassGroupError(he.message));
} on SocketException catch (se) {
emit(ClassGroupError(se.message));
} on FormatException catch (fe) {
emit(ClassGroupError(fe.message));
} on HttpException catch (he) {
emit(ClassGroupError(he.message));
} on ArgumentError catch (ae) {
emit(ClassGroupError(ae.message));
} catch (e) {
emit(ClassGroupError(e.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:app_student/users/cubit/user_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:go_router/go_router.dart';

import '../../shared_components/app_bar.dart';
Expand Down Expand Up @@ -54,7 +55,15 @@ class ClassGroupPage extends StatelessWidget {
],
);
} else if (classState is ClassGroupError) {
return const NetworkError();
WidgetsBinding.instance.addPostFrameCallback((_) {
Fluttertoast.showToast(
msg: AppLocalizations.of(context)!.errors_code(classState.message),
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
textColor: Colors.white,
);
});
return const SizedBox.shrink();
} else {
return const Center(child: CircularProgressIndicator());
}
Expand Down

0 comments on commit 06e37ab

Please sign in to comment.