| 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; | |||||
| } | |||||
| } |
| return BlocBuilder<InfinityScrollBloc, InfinityScrollState>( | return BlocBuilder<InfinityScrollBloc, InfinityScrollState>( | ||||
| builder: (context, state) { | builder: (context, state) { | ||||
| if (state is InfinityScrollFailure) { | if (state is InfinityScrollFailure) { | ||||
| return Center(child: Text(label_error_get_data)); | |||||
| return Center(child: Text(state.errorString)); | |||||
| } | } | ||||
| if (state is InfinityScrollSuccess) { | if (state is InfinityScrollSuccess) { | ||||
| if (state.items.isEmpty) { | if (state.items.isEmpty) { |
| import 'package:bloc/bloc.dart'; | import 'package:bloc/bloc.dart'; | ||||
| import 'package:equatable/equatable.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:farm_tpf/data/repository/repository.dart'; | ||||
| import 'package:meta/meta.dart'; | import 'package:meta/meta.dart'; | ||||
| hasReachedMax: false); | hasReachedMax: false); | ||||
| } | } | ||||
| } catch (e) { | } catch (e) { | ||||
| print(e); | |||||
| yield InfinityScrollFailure(); | |||||
| var errorString = AppException.handleError(e); | |||||
| yield InfinityScrollFailure(errorString: errorString); | |||||
| } | } | ||||
| } | } | ||||
| if (event is OnRefresh) { | if (event is OnRefresh) { | ||||
| items: response.results, | items: response.results, | ||||
| page: 0, | page: 0, | ||||
| hasReachedMax: response.results.length < pageSize ? true : false); | hasReachedMax: response.results.length < pageSize ? true : false); | ||||
| } catch (_) { | |||||
| yield InfinityScrollFailure(); | |||||
| } catch (e) { | |||||
| yield InfinityScrollFailure(errorString: AppException.handleError(e)); | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| class InfinityScrollInitial extends InfinityScrollState {} | class InfinityScrollInitial extends InfinityScrollState {} | ||||
| class InfinityScrollFailure extends InfinityScrollState {} | |||||
| class InfinityScrollFailure extends InfinityScrollState { | |||||
| final String errorString; | |||||
| InfinityScrollFailure({@required this.errorString}); | |||||
| } | |||||
| class InfinityScrollSuccess<T> extends InfinityScrollState { | class InfinityScrollSuccess<T> extends InfinityScrollState { | ||||
| final List<T> items; | final List<T> items; |
| const String label_cancel = "Huỷ"; | const String label_cancel = "Huỷ"; | ||||
| const String label_list_empty = "Dữ liệu rỗng"; | const String label_list_empty = "Dữ liệu rỗng"; | ||||
| const String label_error_get_data = "Lỗi tải dữ liệu"; | 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ủ"; |