diff --git a/lib/src/core/failure/network_failure.dart b/lib/src/core/failure/network_failure.dart index 6f4b961..af0bbe2 100644 --- a/lib/src/core/failure/network_failure.dart +++ b/lib/src/core/failure/network_failure.dart @@ -1,5 +1,4 @@ -// ignore_for_file: , depend_on_referenced_packages - +// ignore_for_file: depend_on_referenced_packages, no_leading_underscores_for_local_identifiers import 'package:dio/dio.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:weather_app/src/core/failure/app_failure.dart'; @@ -31,7 +30,7 @@ class NetworkFailure with _$NetworkFailure implements AppFailure { ); } //we can't be here because we checked before calling this method - throw Exception('Terminating: We can\'t be here!'); + throw Exception(ErrorMessage.nonProducable.name); } // Helper method to handle different types of Dio exceptions @@ -40,42 +39,45 @@ class NetworkFailure with _$NetworkFailure implements AppFailure { case DioExceptionType.connectionTimeout: case DioExceptionType.sendTimeout: case DioExceptionType.receiveTimeout: - return "Timeout occurred while sending or receiving"; + return ErrorMessage.timeOut.name; case DioExceptionType.badResponse: - return error.message ?? 'Bad response'; + return error.message ?? ErrorMessage.badResponse.name; case DioExceptionType.cancel: break; case DioExceptionType.unknown: return _getErrorResponseBodyFromServer(error); case DioExceptionType.badCertificate: - return "Internal Server Error"; + return ErrorMessage.internalServerError.name; case DioExceptionType.connectionError: - return error.message ?? 'Connection Error'; + return error.message ?? ErrorMessage.connectionError.name; default: - return "Unknown Error"; + return ErrorMessage.unknownError.name; } - return "Unknown Error"; + return ErrorMessage.unknownError.name; } static String _getErrorResponseBodyFromServer(DioException error) { assert(error.type == DioExceptionType.unknown); + if (error.response?.data is String) { + return error.response?.data ?? 'Redirected to login page'; + } // Here Map is the type of the response body // You can use a proper Model class to parse the response body final responseBody = error.response?.data as Map?; - String? msg = responseBody?['detail'] ?? error.message; + String? msg = responseBody?['message'] ?? error.message; if (msg == null || msg.isEmpty || msg == 'null') { - msg = 'Data is not available from server'; + msg = ErrorMessage.dataUnavailable.name; } return msg; } static String _getErrorName(DioException error) { - String _name = 'Unrecognized error'; + String _name = ErrorMessage.unrecognizedError.name; if (error.type == DioExceptionType.connectionError || error.type == DioExceptionType.sendTimeout || error.type == DioExceptionType.receiveTimeout || error.type == DioExceptionType.connectionTimeout) { - _name = 'No internet connection'; + _name = ErrorMessage.noInternetConnection.name; } final code = error.response?.statusCode; diff --git a/lib/src/core/networking/network_misc.dart b/lib/src/core/networking/network_misc.dart index 8ee29bf..32b7a08 100644 --- a/lib/src/core/networking/network_misc.dart +++ b/lib/src/core/networking/network_misc.dart @@ -1,6 +1,7 @@ typedef JSON = Map; enum StatusCode { + redirectResponse(code: 302, name: 'Redirect Response'), badRequest(code: 400, name: 'Bad Request'), unauthorized(code: 401, name: 'Unauthorized'), forbidden(code: 403, name: 'Forbidden'), @@ -26,11 +27,11 @@ enum StatusCode { StatusCode? getStatusCode(int? code) { return switch (code) { + 302 => StatusCode.redirectResponse, 400 => StatusCode.badRequest, 401 => StatusCode.unauthorized, 403 => StatusCode.forbidden, 404 => StatusCode.notFound, - 408 => StatusCode.requestTimeout, 409 => StatusCode.conflict, 419 => StatusCode.tokenExpired, 422 => StatusCode.unprocessableEntity, @@ -53,3 +54,16 @@ class NetworkMisc { tokenField: true }; } + +enum ErrorMessage { + nonProducable, + timeOut, + badResponse, + internalServerError, + connectionError, + unknownError, + dataUnavailable, + unrecognizedError, + noInternetConnection, + somethingWentWrong, +} diff --git a/lib/src/feature/weather/application/notifiers/weather_notifier.dart b/lib/src/feature/weather/application/notifiers/weather_notifier.dart index 9d02cf5..49e8420 100644 --- a/lib/src/feature/weather/application/notifiers/weather_notifier.dart +++ b/lib/src/feature/weather/application/notifiers/weather_notifier.dart @@ -1,3 +1,4 @@ +import 'package:fpdart/fpdart.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:weather_app/src/core/helper/colored_logger.dart'; import 'package:weather_app/src/feature/common/states/api_state.dart'; @@ -19,19 +20,12 @@ class WeatherNotifier extends _$WeatherNotifier { final repo = ref.read(getWeatherRepositoryProvider); var data = await repo.getWeather(cityName); ColoredLogger.Green.log('Got weather for $cityName'); - data.fold( - (l) { - ColoredLogger.Red.log('Failed to get weather for $cityName'); - //we can handle error here - state = ApiRequestState.failed(reason: l); - }, - (r) { - //we can handle success here - //convert the dto to entity as we don't want dto to be exposed to the presentation layer - state = ApiRequestState.data( + + state = switch (data) { + Left(value: final l) => ApiRequestState.failed(reason: l), + Right(value: final r) => ApiRequestState.data( data: WeatherFullEntity.fromDTO(r), - ); - }, - ); + ), + }; } } diff --git a/lib/src/feature/weather/presentation/screen/weather_infromation_screen.dart b/lib/src/feature/weather/presentation/screen/weather_infromation_screen.dart index 8ac365a..3561918 100644 --- a/lib/src/feature/weather/presentation/screen/weather_infromation_screen.dart +++ b/lib/src/feature/weather/presentation/screen/weather_infromation_screen.dart @@ -108,7 +108,7 @@ class WeatherInformationScreen extends HookConsumerWidget { margin: EdgeInsets.only(top: 140), alignment: Alignment.center, child: Text( - e.toString(), + e.message, style: bigTitleStyle, ), ); diff --git a/pubspec.lock b/pubspec.lock index f0e5b97..0987e6a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -106,10 +106,10 @@ packages: dependency: "direct dev" description: name: build_runner - sha256: dd09dd4e2b078992f42aac7f1a622f01882a8492fef08486b27ddde929c19f04 + sha256: "028819cfb90051c6b5440c7e574d1896f8037e3c96cf17aaeb054c9311cfbf4d" url: "https://pub.dev" source: hosted - version: "2.4.12" + version: "2.4.13" build_runner_core: dependency: transitive description: @@ -218,26 +218,34 @@ packages: dependency: transitive description: name: custom_lint - sha256: "4939d89e580c36215e48a7de8fd92f22c79dcc3eb11fda84f3402b3b45aec663" + sha256: "3486c470bb93313a9417f926c7dd694a2e349220992d7b9d14534dc49c15bba9" url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.7.0" custom_lint_builder: dependency: transitive description: name: custom_lint_builder - sha256: d9e5bb63ed52c1d006f5a1828992ba6de124c27a531e8fba0a31afffa81621b3 + sha256: "42cdc41994eeeddab0d7a722c7093ec52bd0761921eeb2cbdbf33d192a234759" url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.7.0" custom_lint_core: dependency: transitive description: name: custom_lint_core - sha256: "4ddbbdaa774265de44c97054dcec058a83d9081d071785ece601e348c18c267d" + sha256: "02450c3e45e2a6e8b26c4d16687596ab3c4644dd5792e3313aa9ceba5a49b7f5" url: "https://pub.dev" source: hosted - version: "0.6.5" + version: "0.7.0" + custom_lint_visitor: + dependency: transitive + description: + name: custom_lint_visitor + sha256: "8aeb3b6ae2bb765e7716b93d1d10e8356d04e0ff6d7592de6ee04e0dd7d6587d" + url: "https://pub.dev" + source: hosted + version: "1.0.0+6.7.0" dart_style: dependency: transitive description: @@ -250,10 +258,10 @@ packages: dependency: "direct main" description: name: device_info_plus - sha256: a7fd703482b391a87d60b6061d04dfdeab07826b96f9abd8f5ed98068acc0074 + sha256: f545ffbadee826f26f2e1a0f0cbd667ae9a6011cc0f77c0f8f00a969655e6e95 url: "https://pub.dev" source: hosted - version: "10.1.2" + version: "11.1.1" device_info_plus_platform_interface: dependency: transitive description: @@ -327,10 +335,10 @@ packages: dependency: transitive description: name: flutter_riverpod - sha256: "0f1974eff5bbe774bf1d870e406fc6f29e3d6f1c46bd9c58e7172ff68a785d7d" + sha256: "9532ee6db4a943a1ed8383072a2e3eeda041db5657cdf6d2acecf3c21ecbe7e1" url: "https://pub.dev" source: hosted - version: "2.5.1" + version: "2.6.1" flutter_test: dependency: "direct dev" description: flutter @@ -353,10 +361,10 @@ packages: dependency: "direct main" description: name: fpdart - sha256: "7413acc5a6569a3fe8277928fc7487f3198530f0c4e635d0baef199ea36e8ee9" + sha256: "9c82219bf8f7ab481c1d6c76e85c62e36aeb00429758100e1655b7cf5e22d055" url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "2.0.0-dev.3" freezed: dependency: "direct dev" description: @@ -417,10 +425,10 @@ packages: dependency: "direct main" description: name: geolocator - sha256: "0ec58b731776bc43097fcf751f79681b6a8f6d3bc737c94779fe9f1ad73c1a81" + sha256: d2ec66329cab29cb297d51d96c067d457ca519dca8589665fa0b82ebacb7dbe4 url: "https://pub.dev" source: hosted - version: "13.0.1" + version: "13.0.2" geolocator_android: dependency: transitive description: @@ -433,10 +441,10 @@ packages: dependency: transitive description: name: geolocator_apple - sha256: bc2aca02423ad429cb0556121f56e60360a2b7d694c8570301d06ea0c00732fd + sha256: "6154ea2682563f69fc0125762ed7e91e7ed85d0b9776595653be33918e064807" url: "https://pub.dev" source: hosted - version: "2.3.7" + version: "2.3.8+1" geolocator_platform_interface: dependency: transitive description: @@ -489,10 +497,10 @@ packages: dependency: "direct main" description: name: hooks_riverpod - sha256: "97266a91c994951a06ef0ff3a1c7fb261e52ec7f74e87f0614ea0b7411b859b2" + sha256: "70bba33cfc5670c84b796e6929c54b8bc5be7d0fe15bb28c2560500b9ad06966" url: "https://pub.dev" source: hosted - version: "2.5.2" + version: "2.6.1" hotreloader: dependency: transitive description: @@ -529,10 +537,10 @@ packages: dependency: "direct main" description: name: intl - sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf + sha256: "00f33b908655e606b86d2ade4710a231b802eec6f11e87e4ea3783fd72077a50" url: "https://pub.dev" source: hosted - version: "0.19.0" + version: "0.20.1" io: dependency: transitive description: @@ -561,10 +569,10 @@ packages: dependency: "direct dev" description: name: json_serializable - sha256: ea1432d167339ea9b5bb153f0571d0039607a873d6e04e0117af043f14a1fd4b + sha256: c2fcb3920cf2b6ae6845954186420fca40bc0a8abcc84903b7801f17d7050d7c url: "https://pub.dev" source: hosted - version: "6.8.0" + version: "6.9.0" leak_tracker: dependency: transitive description: @@ -817,50 +825,50 @@ packages: dependency: "direct dev" description: name: retrofit_generator - sha256: "8ea6a9d1bd82862fb396462357cce20d8bab57cd7e737ac9e5af8947a2029f32" + sha256: f76fdb2b66854690d5a332e7364d7561fc9dc2b3c924d7956ab8070495e21f6a url: "https://pub.dev" source: hosted - version: "9.1.1" + version: "9.1.5" riverpod: dependency: transitive description: name: riverpod - sha256: f21b32ffd26a36555e501b04f4a5dca43ed59e16343f1a30c13632b2351dfa4d + sha256: "59062512288d3056b2321804332a13ffdd1bf16df70dcc8e506e411280a72959" url: "https://pub.dev" source: hosted - version: "2.5.1" + version: "2.6.1" riverpod_analyzer_utils: dependency: transitive description: name: riverpod_analyzer_utils - sha256: ac28d7bc678471ec986b42d88e5a0893513382ff7542c7ac9634463b044ac72c + sha256: c6b8222b2b483cb87ae77ad147d6408f400c64f060df7a225b127f4afef4f8c8 url: "https://pub.dev" source: hosted - version: "0.5.4" + version: "0.5.8" riverpod_annotation: dependency: "direct main" description: name: riverpod_annotation - sha256: e5e796c0eba4030c704e9dae1b834a6541814963292839dcf9638d53eba84f5c + sha256: e14b0bf45b71326654e2705d462f21b958f987087be850afd60578fcd502d1b8 url: "https://pub.dev" source: hosted - version: "2.3.5" + version: "2.6.1" riverpod_generator: dependency: "direct dev" description: name: riverpod_generator - sha256: "63311e361ffc578d655dfc31b48dfa4ed3bc76fd06f9be845e9bf97c5c11a429" + sha256: "63546d70952015f0981361636bf8f356d9cfd9d7f6f0815e3c07789a41233188" url: "https://pub.dev" source: hosted - version: "2.4.3" + version: "2.6.3" riverpod_lint: dependency: "direct dev" description: name: riverpod_lint - sha256: a35a92f2c2a4b7a5d95671c96c5432b42c20f26bb3e985e83d0b186471b61a85 + sha256: "83e4caa337a9840469b7b9bd8c2351ce85abad80f570d84146911b32086fbd99" url: "https://pub.dev" source: hosted - version: "2.3.13" + version: "2.6.3" rxdart: dependency: transitive description: @@ -998,10 +1006,10 @@ packages: dependency: transitive description: name: uuid - sha256: f33d6bb662f0e4f79dcd7ada2e6170f3b3a2530c28fc41f49a411ddedd576a77 + sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff url: "https://pub.dev" source: hosted - version: "4.5.0" + version: "4.5.1" vector_math: dependency: transitive description: @@ -1030,10 +1038,10 @@ packages: dependency: transitive description: name: web - sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062 + sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb url: "https://pub.dev" source: hosted - version: "1.0.0" + version: "1.1.0" web_socket: dependency: transitive description: @@ -1091,5 +1099,5 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.5.0-259.0.dev <4.0.0" + dart: ">=3.5.0 <4.0.0" flutter: ">=3.22.0" diff --git a/pubspec.yaml b/pubspec.yaml index c664c48..d8e9822 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,32 +23,32 @@ environment: dependencies: auto_route: ^9.2.2 cupertino_icons: ^1.0.8 - device_info_plus: 10.1.2 + device_info_plus: ^11.1.1 flutter: sdk: flutter flutter_hooks: ^0.20.5 fluttertoast: ^8.2.8 - fpdart: ^1.1.0 + fpdart: ^2.0.0-dev.3 freezed_annotation: ^2.4.4 geocoding: ^3.0.0 - geolocator: ^13.0.1 + geolocator: ^13.0.2 google_fonts: ^6.2.1 - hooks_riverpod: ^2.5.2 - intl: ^0.19.0 + hooks_riverpod: ^2.6.1 + intl: ^0.20.1 permission_handler: ^11.3.1 retrofit: ^4.4.1 - riverpod_annotation: ^2.3.5 + riverpod_annotation: ^2.6.1 dev_dependencies: auto_route_generator: ^9.0.0 - build_runner: ^2.4.12 + build_runner: ^2.4.13 flutter_test: sdk: flutter freezed: ^2.5.7 - json_serializable: ^6.8.0 - retrofit_generator: ^9.1.1 - riverpod_generator: ^2.4.3 - riverpod_lint: ^2.3.13 + json_serializable: ^6.9.0 + retrofit_generator: ^9.1.5 + riverpod_generator: ^2.6.3 + riverpod_lint: ^2.6.3 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter.