You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.9 KiB

2 years ago
  1. import 'package:itasmob/presentation/resources/strings_manager.dart';
  2. import 'package:dio/dio.dart';
  3. import 'failure.dart';
  4. enum DataSource {
  5. SUCCESS,
  6. NO_CONTENT,
  7. BAD_REQUEST,
  8. FORBIDDEN,
  9. UNAUTHORISED,
  10. NOT_FOUND,
  11. INTERNAL_SERVER_ERROR,
  12. CONNECT_TIMEOUT,
  13. CANCEL,
  14. RECEIVE_TIMEOUT,
  15. SEND_TIMEOUT,
  16. CACHE_ERROR,
  17. NO_INTERNET_CONNECTION,
  18. DEFAULT
  19. }
  20. class ErrorHandler implements Exception {
  21. late Failure failure;
  22. ErrorHandler.handle(dynamic error) {
  23. if (error is DioError) {
  24. // dio error so its error from response of the API
  25. failure = _handleError(error);
  26. } else {
  27. // default error
  28. failure = DataSource.DEFAULT.getFailure();
  29. }
  30. }
  31. Failure _handleError(DioError error) {
  32. switch (error.type) {
  33. case DioErrorType.connectTimeout:
  34. return DataSource.CONNECT_TIMEOUT.getFailure();
  35. case DioErrorType.sendTimeout:
  36. return DataSource.SEND_TIMEOUT.getFailure();
  37. case DioErrorType.receiveTimeout:
  38. return DataSource.RECEIVE_TIMEOUT.getFailure();
  39. case DioErrorType.response:
  40. switch (error.response?.statusCode) {
  41. case ResponseCode.BAD_REQUEST:
  42. return DataSource.BAD_REQUEST.getFailure();
  43. case ResponseCode.FORBIDDEN:
  44. return DataSource.FORBIDDEN.getFailure();
  45. case ResponseCode.UNAUTHORISED:
  46. return DataSource.UNAUTHORISED.getFailure();
  47. case ResponseCode.NOT_FOUND:
  48. return DataSource.NOT_FOUND.getFailure();
  49. case ResponseCode.INTERNAL_SERVER_ERROR:
  50. return DataSource.INTERNAL_SERVER_ERROR.getFailure();
  51. default:
  52. return DataSource.DEFAULT.getFailure();
  53. }
  54. case DioErrorType.cancel:
  55. return DataSource.CANCEL.getFailure();
  56. case DioErrorType.other:
  57. return DataSource.DEFAULT.getFailure();
  58. }
  59. }
  60. }
  61. extension DataSourceExtension on DataSource {
  62. Failure getFailure() {
  63. switch (this) {
  64. case DataSource.BAD_REQUEST:
  65. return Failure(ResponseCode.BAD_REQUEST, ResponseMessage.BAD_REQUEST);
  66. case DataSource.FORBIDDEN:
  67. return Failure(ResponseCode.FORBIDDEN, ResponseMessage.FORBIDDEN);
  68. case DataSource.UNAUTHORISED:
  69. return Failure(ResponseCode.UNAUTHORISED, ResponseMessage.UNAUTHORISED);
  70. case DataSource.NOT_FOUND:
  71. return Failure(ResponseCode.NOT_FOUND, ResponseMessage.NOT_FOUND);
  72. case DataSource.INTERNAL_SERVER_ERROR:
  73. return Failure(ResponseCode.INTERNAL_SERVER_ERROR,
  74. ResponseMessage.INTERNAL_SERVER_ERROR);
  75. case DataSource.CONNECT_TIMEOUT:
  76. return Failure(
  77. ResponseCode.CONNECT_TIMEOUT, ResponseMessage.CONNECT_TIMEOUT);
  78. case DataSource.CANCEL:
  79. return Failure(ResponseCode.CANCEL, ResponseMessage.CANCEL);
  80. case DataSource.RECEIVE_TIMEOUT:
  81. return Failure(
  82. ResponseCode.RECEIVE_TIMEOUT, ResponseMessage.RECEIVE_TIMEOUT);
  83. case DataSource.SEND_TIMEOUT:
  84. return Failure(ResponseCode.SEND_TIMEOUT, ResponseMessage.SEND_TIMEOUT);
  85. case DataSource.CACHE_ERROR:
  86. return Failure(ResponseCode.CACHE_ERROR, ResponseMessage.CACHE_ERROR);
  87. case DataSource.NO_INTERNET_CONNECTION:
  88. return Failure(ResponseCode.NO_INTERNET_CONNECTION,
  89. ResponseMessage.NO_INTERNET_CONNECTION);
  90. case DataSource.DEFAULT:
  91. return Failure(ResponseCode.DEFAULT, ResponseMessage.DEFAULT);
  92. default:
  93. return Failure(ResponseCode.DEFAULT, ResponseMessage.DEFAULT);
  94. }
  95. }
  96. }
  97. class ResponseCode {
  98. // API status codes
  99. static const int SUCCESS = 200; // success with data
  100. static const int NO_CONTENT = 201; // success with no content
  101. static const int BAD_REQUEST = 400; // failure, api rejected the request
  102. static const int FORBIDDEN = 403; // failure, api rejected the request
  103. static const int UNAUTHORISED = 401; // failure user is not authorised
  104. static const int NOT_FOUND =
  105. 404; // failure, api url is not correct and not found
  106. static const int INTERNAL_SERVER_ERROR =
  107. 500; // failure, crash happened in server side
  108. // local status code
  109. static const int DEFAULT = -1;
  110. static const int CONNECT_TIMEOUT = -2;
  111. static const int CANCEL = -3;
  112. static const int RECEIVE_TIMEOUT = -4;
  113. static const int SEND_TIMEOUT = -5;
  114. static const int CACHE_ERROR = -6;
  115. static const int NO_INTERNET_CONNECTION = -7;
  116. }
  117. class ResponseMessage {
  118. // API status codes
  119. // API response codes
  120. static const String SUCCESS = AppStrings.success; // success with data
  121. static const String NO_CONTENT =
  122. AppStrings.noContent; // success with no content
  123. static const String BAD_REQUEST =
  124. AppStrings.badRequestError; // failure, api rejected our request
  125. static const String FORBIDDEN =
  126. AppStrings.forbiddenError; // failure, api rejected our request
  127. static const String UNAUTHORISED =
  128. AppStrings.unauthorizedError; // failure, user is not authorised
  129. static const String NOT_FOUND = AppStrings
  130. .notFoundError; // failure, API url is not correct and not found in api side.
  131. static const String INTERNAL_SERVER_ERROR =
  132. AppStrings.internalServerError; // failure, a crash happened in API side.
  133. // local responses codes
  134. static const String DEFAULT =
  135. AppStrings.defaultError; // unknown error happened
  136. static const String CONNECT_TIMEOUT =
  137. AppStrings.timeoutError; // issue in connectivity
  138. static const String CANCEL =
  139. AppStrings.defaultError; // API request was cancelled
  140. static const String RECEIVE_TIMEOUT =
  141. AppStrings.timeoutError; // issue in connectivity
  142. static const String SEND_TIMEOUT =
  143. AppStrings.timeoutError; // issue in connectivity
  144. static const String CACHE_ERROR = AppStrings
  145. .defaultError; // issue in getting data from local data source (cache)
  146. static const String NO_INTERNET_CONNECTION =
  147. AppStrings.noInternetError; // issue in connectivity
  148. }
  149. class ApiInternalStatus {
  150. static const String SUCCESS = "ok";
  151. static const int FAILURE = 1;
  152. }