|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:dio/dio.dart'; |
| 4 | +import 'package:freezed_annotation/freezed_annotation.dart'; |
| 5 | + |
| 6 | +part 'network_exceptions.freezed.dart'; |
| 7 | + |
| 8 | +@freezed |
| 9 | +class NetworkExceptions with _$NetworkExceptions { |
| 10 | + const factory NetworkExceptions.requestCancelled() = RequestCancelled; |
| 11 | + |
| 12 | + const factory NetworkExceptions.unauthorisedRequest() = UnauthorisedRequest; |
| 13 | + |
| 14 | + const factory NetworkExceptions.badRequest() = BadRequest; |
| 15 | + |
| 16 | + const factory NetworkExceptions.notFound(String reason) = NotFound; |
| 17 | + |
| 18 | + const factory NetworkExceptions.methodNotAllowed() = MethodNotAllowed; |
| 19 | + |
| 20 | + const factory NetworkExceptions.notAcceptable() = NotAcceptable; |
| 21 | + |
| 22 | + const factory NetworkExceptions.requestTimeout() = RequestTimeout; |
| 23 | + |
| 24 | + const factory NetworkExceptions.receiveTimeout() = ReceiveTimeout; |
| 25 | + |
| 26 | + const factory NetworkExceptions.sendTimeout() = SendTimeout; |
| 27 | + |
| 28 | + const factory NetworkExceptions.conflict() = Conflict; |
| 29 | + |
| 30 | + const factory NetworkExceptions.internalServerError() = InternalServerError; |
| 31 | + |
| 32 | + const factory NetworkExceptions.notImplemented() = NotImplemented; |
| 33 | + |
| 34 | + const factory NetworkExceptions.serviceUnavailable() = ServiceUnavailable; |
| 35 | + |
| 36 | + const factory NetworkExceptions.noInternetConnection() = NoInternetConnection; |
| 37 | + |
| 38 | + const factory NetworkExceptions.formatException() = FormatException; |
| 39 | + |
| 40 | + const factory NetworkExceptions.unableToProcess() = UnableToProcess; |
| 41 | + |
| 42 | + const factory NetworkExceptions.defaultError(String error) = DefaultError; |
| 43 | + |
| 44 | + const factory NetworkExceptions.unexpectedError() = UnexpectedError; |
| 45 | + |
| 46 | + static NetworkExceptions fromDioException(error) { |
| 47 | + if (error is Exception) { |
| 48 | + try { |
| 49 | + NetworkExceptions networkExceptions; |
| 50 | + if (error is DioError) { |
| 51 | + switch (error.type) { |
| 52 | + case DioErrorType.cancel: |
| 53 | + networkExceptions = const NetworkExceptions.requestCancelled(); |
| 54 | + break; |
| 55 | + case DioErrorType.connectTimeout: |
| 56 | + networkExceptions = const NetworkExceptions.requestTimeout(); |
| 57 | + break; |
| 58 | + case DioErrorType.other: |
| 59 | + networkExceptions = |
| 60 | + const NetworkExceptions.noInternetConnection(); |
| 61 | + break; |
| 62 | + case DioErrorType.receiveTimeout: |
| 63 | + networkExceptions = const NetworkExceptions.receiveTimeout(); |
| 64 | + break; |
| 65 | + case DioErrorType.sendTimeout: |
| 66 | + networkExceptions = const NetworkExceptions.sendTimeout(); |
| 67 | + break; |
| 68 | + case DioErrorType.response: |
| 69 | + switch (error.response?.statusCode) { |
| 70 | + case 400: |
| 71 | + networkExceptions = const NetworkExceptions.badRequest(); |
| 72 | + break; |
| 73 | + case 401: |
| 74 | + networkExceptions = |
| 75 | + const NetworkExceptions.unauthorisedRequest(); |
| 76 | + break; |
| 77 | + case 403: |
| 78 | + networkExceptions = |
| 79 | + const NetworkExceptions.unauthorisedRequest(); |
| 80 | + break; |
| 81 | + case 404: |
| 82 | + networkExceptions = |
| 83 | + const NetworkExceptions.notFound("Not found"); |
| 84 | + break; |
| 85 | + case 409: |
| 86 | + networkExceptions = const NetworkExceptions.conflict(); |
| 87 | + break; |
| 88 | + case 408: |
| 89 | + networkExceptions = const NetworkExceptions.requestTimeout(); |
| 90 | + break; |
| 91 | + case 500: |
| 92 | + networkExceptions = |
| 93 | + const NetworkExceptions.internalServerError(); |
| 94 | + break; |
| 95 | + case 503: |
| 96 | + networkExceptions = |
| 97 | + const NetworkExceptions.serviceUnavailable(); |
| 98 | + break; |
| 99 | + default: |
| 100 | + var responseCode = error.response?.statusCode; |
| 101 | + networkExceptions = NetworkExceptions.defaultError( |
| 102 | + "Received invalid status code: $responseCode", |
| 103 | + ); |
| 104 | + } |
| 105 | + break; |
| 106 | + } |
| 107 | + } else if (error is SocketException) { |
| 108 | + networkExceptions = const NetworkExceptions.noInternetConnection(); |
| 109 | + } else { |
| 110 | + networkExceptions = const NetworkExceptions.unexpectedError(); |
| 111 | + } |
| 112 | + return networkExceptions; |
| 113 | + } on FormatException catch (_) { |
| 114 | + return const NetworkExceptions.formatException(); |
| 115 | + } catch (_) { |
| 116 | + return const NetworkExceptions.unexpectedError(); |
| 117 | + } |
| 118 | + } else { |
| 119 | + if (error.toString().contains("is not a subtype of")) { |
| 120 | + return const NetworkExceptions.unableToProcess(); |
| 121 | + } else { |
| 122 | + return const NetworkExceptions.unexpectedError(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + static String getErrorMessage(NetworkExceptions networkExceptions) { |
| 128 | + String? errorMessage; |
| 129 | + networkExceptions.when(notImplemented: () { |
| 130 | + errorMessage = "Not Implemented"; |
| 131 | + }, requestCancelled: () { |
| 132 | + errorMessage = "Request Cancelled"; |
| 133 | + }, internalServerError: () { |
| 134 | + errorMessage = "Internal Server Error"; |
| 135 | + }, notFound: (String reason) { |
| 136 | + errorMessage = reason; |
| 137 | + }, serviceUnavailable: () { |
| 138 | + errorMessage = "Service unavailable"; |
| 139 | + }, methodNotAllowed: () { |
| 140 | + errorMessage = "Method not allowed"; |
| 141 | + }, badRequest: () { |
| 142 | + errorMessage = "Bad request"; |
| 143 | + }, unauthorisedRequest: () { |
| 144 | + errorMessage = "Unauthorised request"; |
| 145 | + }, unexpectedError: () { |
| 146 | + errorMessage = "Unexpected error occurred"; |
| 147 | + }, requestTimeout: () { |
| 148 | + errorMessage = "Connection request timeout"; |
| 149 | + }, noInternetConnection: () { |
| 150 | + errorMessage = "No internet connection"; |
| 151 | + }, conflict: () { |
| 152 | + errorMessage = "Error due to a conflict"; |
| 153 | + }, sendTimeout: () { |
| 154 | + errorMessage = "Send timeout in connection with API server"; |
| 155 | + }, receiveTimeout: () { |
| 156 | + errorMessage = "Receive timeout in connection with API server"; |
| 157 | + }, unableToProcess: () { |
| 158 | + errorMessage = "Unable to process the data"; |
| 159 | + }, defaultError: (String error) { |
| 160 | + errorMessage = error; |
| 161 | + }, formatException: () { |
| 162 | + errorMessage = "Unexpected error occurred"; |
| 163 | + }, notAcceptable: () { |
| 164 | + errorMessage = "Not acceptable"; |
| 165 | + }); |
| 166 | + return errorMessage ?? "Unknown error occurred"; |
| 167 | + } |
| 168 | +} |
0 commit comments