| @@ -0,0 +1,47 @@ | |||
| import 'package:dio/dio.dart'; | |||
| import 'package:farm_tpf/utils/const_string.dart'; | |||
| class AppException { | |||
| static String handleError(dynamic error) { | |||
| String errorDescription = ""; | |||
| if (error is DioError) { | |||
| DioError dioError = error; | |||
| switch (dioError.type) { | |||
| case DioErrorType.CANCEL: | |||
| errorDescription = exception_dio_cancle; | |||
| break; | |||
| case DioErrorType.CONNECT_TIMEOUT: | |||
| errorDescription = exception_dio_connect_timeout; | |||
| break; | |||
| case DioErrorType.DEFAULT: | |||
| errorDescription = exception_dio_default; | |||
| break; | |||
| case DioErrorType.RECEIVE_TIMEOUT: | |||
| errorDescription = exception_dio_receive_timeout; | |||
| break; | |||
| case DioErrorType.RESPONSE: | |||
| int statusCode = dioError.response.statusCode; | |||
| if (statusCode == 400) { | |||
| errorDescription = exception_dio_400; | |||
| } else if (statusCode == 401) { | |||
| errorDescription = exception_dio_401; | |||
| } else if (statusCode == 403) { | |||
| errorDescription = exception_dio_403; | |||
| } else if (statusCode == 404) { | |||
| errorDescription = exception_dio_404; | |||
| } else if (statusCode == 500) { | |||
| errorDescription = exception_dio_500; | |||
| } else { | |||
| errorDescription = exception_common; | |||
| } | |||
| break; | |||
| case DioErrorType.SEND_TIMEOUT: | |||
| errorDescription = exception_dio_send_timeout; | |||
| break; | |||
| } | |||
| } else { | |||
| errorDescription = exception_common; | |||
| } | |||
| return errorDescription; | |||
| } | |||
| } | |||
| @@ -70,7 +70,7 @@ class _InfinityViewState extends State<InfinityView> { | |||
| return BlocBuilder<InfinityScrollBloc, InfinityScrollState>( | |||
| builder: (context, state) { | |||
| if (state is InfinityScrollFailure) { | |||
| return Center(child: Text(label_error_get_data)); | |||
| return Center(child: Text(state.errorString)); | |||
| } | |||
| if (state is InfinityScrollSuccess) { | |||
| if (state.items.isEmpty) { | |||
| @@ -2,6 +2,7 @@ import 'dart:async'; | |||
| import 'package:bloc/bloc.dart'; | |||
| import 'package:equatable/equatable.dart'; | |||
| import 'package:farm_tpf/data/api/app_exception.dart'; | |||
| import 'package:farm_tpf/data/repository/repository.dart'; | |||
| import 'package:meta/meta.dart'; | |||
| @@ -44,8 +45,8 @@ class InfinityScrollBloc | |||
| hasReachedMax: false); | |||
| } | |||
| } catch (e) { | |||
| print(e); | |||
| yield InfinityScrollFailure(); | |||
| var errorString = AppException.handleError(e); | |||
| yield InfinityScrollFailure(errorString: errorString); | |||
| } | |||
| } | |||
| if (event is OnRefresh) { | |||
| @@ -56,8 +57,8 @@ class InfinityScrollBloc | |||
| items: response.results, | |||
| page: 0, | |||
| hasReachedMax: response.results.length < pageSize ? true : false); | |||
| } catch (_) { | |||
| yield InfinityScrollFailure(); | |||
| } catch (e) { | |||
| yield InfinityScrollFailure(errorString: AppException.handleError(e)); | |||
| } | |||
| } | |||
| } | |||
| @@ -9,7 +9,10 @@ abstract class InfinityScrollState extends Equatable { | |||
| class InfinityScrollInitial extends InfinityScrollState {} | |||
| class InfinityScrollFailure extends InfinityScrollState {} | |||
| class InfinityScrollFailure extends InfinityScrollState { | |||
| final String errorString; | |||
| InfinityScrollFailure({@required this.errorString}); | |||
| } | |||
| class InfinityScrollSuccess<T> extends InfinityScrollState { | |||
| final List<T> items; | |||
| @@ -23,3 +23,19 @@ const String label_record_video = "Quay video"; | |||
| const String label_cancel = "Huỷ"; | |||
| const String label_list_empty = "Dữ liệu rỗng"; | |||
| const String label_error_get_data = "Lỗi tải dữ liệu"; | |||
| //Exception | |||
| const String exception_common = "Đã có lỗi xảy ra"; | |||
| const String exception_dio_cancle = "Truy vấn đến máy chủ bị huỷ"; | |||
| const String exception_dio_connect_timeout = | |||
| "Quá thời gian kết nối đến máy chủ"; | |||
| const String exception_dio_default = "Kết nối đến máy chủ thất bại"; | |||
| const String exception_dio_receive_timeout = | |||
| "Quá thời gian nhận dữ liệu từ máy chủ"; | |||
| const String exception_dio_send_timeout = | |||
| "Quá thời gian gửi dữ liệu tới máy chủ"; | |||
| const String exception_dio_400 = "Định dạng dữ liệu không đúng"; | |||
| const String exception_dio_401 = "Lỗi xác thực người dùng"; | |||
| const String exception_dio_403 = "Không có quyền truy cập"; | |||
| const String exception_dio_404 = "Không tìm thấy dữ liệu"; | |||
| const String exception_dio_500 = "Lỗi máy chủ"; | |||