api_exception.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:eitc_erm_dental_flutter/funcs.dart';
  4. /// 自定义请求异常父类
  5. class ApiException implements Exception {
  6. final int? code;
  7. final String? message;
  8. String? stackInfo;
  9. ApiException([this.code, this.message]);
  10. factory ApiException.fromDioException(DioException exception) {
  11. switch (exception.type) {
  12. case DioExceptionType.connectionTimeout:
  13. return BadRequestException(-1, getS().connectTimeout);
  14. case DioExceptionType.sendTimeout:
  15. return BadRequestException(-1, getS().requestTimeout);
  16. case DioExceptionType.receiveTimeout:
  17. return BadRequestException(-1, getS().responseTimeout);
  18. case DioExceptionType.cancel:
  19. return BadRequestException(-1, getS().requestCancel);
  20. case DioExceptionType.badResponse:
  21. int? errorCode = exception.response?.statusCode;
  22. switch (errorCode) {
  23. case 400:
  24. return BadRequestException(errorCode, getS().requestGrammarError);
  25. case 401:
  26. return UnauthorisedException(errorCode, getS().noPermissions);
  27. case 403:
  28. return UnauthorisedException(errorCode, getS().serverRejectExec);
  29. case 404:
  30. return UnauthorisedException(
  31. errorCode, getS().requestResourceExist);
  32. case 405:
  33. return UnauthorisedException(errorCode, getS().requestMethodBanned);
  34. case 500:
  35. return UnauthorisedException(errorCode, getS().serverInternalError);
  36. case 502:
  37. return UnauthorisedException(errorCode, getS().gateError);
  38. case 503:
  39. return UnauthorisedException(errorCode, getS().serverError);
  40. case 504:
  41. return UnauthorisedException(errorCode, getS().gateTimeout);
  42. case 505:
  43. return UnauthorisedException(
  44. errorCode, getS().notSupportHttpRequest);
  45. default:
  46. return ApiException(errorCode,
  47. exception.response?.statusMessage ?? getS().unknownException);
  48. }
  49. case DioExceptionType.connectionError:
  50. if (exception.error is SocketException) {
  51. return DisconnectException(-1, getS().notworkNotConnect);
  52. } else {
  53. return ApiException(-1, getS().connectError);
  54. }
  55. case DioExceptionType.badCertificate:
  56. return ApiException(-1, getS().certificateError);
  57. case DioExceptionType.unknown:
  58. return ApiException(
  59. -1,
  60. exception.error != null
  61. ? exception.error.toString()
  62. : getS().unknownException);
  63. }
  64. }
  65. // 将各种异常转换为ApiException方便进行统一处理
  66. factory ApiException.from(dynamic exception) {
  67. if (exception is DioException) {
  68. return ApiException.fromDioException(exception);
  69. } else if (exception is ApiException) {
  70. return exception;
  71. } else {
  72. return ApiException(-1, "未知错误")..stackInfo = exception.toString();
  73. }
  74. }
  75. @override
  76. String toString() {
  77. return 'ApiException{code: $code, message: $message, stackInfo: $stackInfo}';
  78. }
  79. }
  80. /// 错误请求异常
  81. class BadRequestException extends ApiException {
  82. BadRequestException(super.code, super.message);
  83. }
  84. /// 未认证异常
  85. class UnauthorisedException extends ApiException {
  86. UnauthorisedException(super.code, super.message);
  87. }
  88. /// 未登录异常
  89. class NeedLoginException extends ApiException {
  90. NeedLoginException(super.code, super.message);
  91. }
  92. /// 网络未连接异常
  93. class DisconnectException extends ApiException {
  94. DisconnectException(super.code, super.message);
  95. }
  96. /// 应用需要强更
  97. class NeedUpdateException extends ApiException {
  98. NeedUpdateException(super.code, super.message);
  99. }
  100. /// 错误响应格式异常
  101. class ErrorResponseFormatException extends ApiException {
  102. ErrorResponseFormatException(super.code, super.message);
  103. }
  104. /// 未知响应类型异常
  105. class UnknowResponseTypeException extends ApiException {
  106. UnknowResponseTypeException(super.code, super.message);
  107. }