Skip to content

Commit

Permalink
upgraded - dependencies, replaced - fold[either] with switch[pattern-…
Browse files Browse the repository at this point in the history
…matching]
  • Loading branch information
Meshkat-Shadik committed Nov 28, 2024
1 parent 924dd82 commit 7e3f10a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 80 deletions.
28 changes: 15 additions & 13 deletions lib/src/core/failure/network_failure.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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<String, dynamic> 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, dynamic>?;
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;
Expand Down
16 changes: 15 additions & 1 deletion lib/src/core/networking/network_misc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
typedef JSON = Map<String, dynamic>;

enum StatusCode {
redirectResponse(code: 302, name: 'Redirect Response'),
badRequest(code: 400, name: 'Bad Request'),
unauthorized(code: 401, name: 'Unauthorized'),
forbidden(code: 403, name: 'Forbidden'),
Expand All @@ -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,
Expand All @@ -53,3 +54,16 @@ class NetworkMisc {
tokenField: true
};
}

enum ErrorMessage {
nonProducable,
timeOut,
badResponse,
internalServerError,
connectionError,
unknownError,
dataUnavailable,
unrecognizedError,
noInternetConnection,
somethingWentWrong,
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<WeatherFullEntity>.data(

state = switch (data) {
Left(value: final l) => ApiRequestState.failed(reason: l),
Right(value: final r) => ApiRequestState<WeatherFullEntity>.data(
data: WeatherFullEntity.fromDTO(r),
);
},
);
),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class WeatherInformationScreen extends HookConsumerWidget {
margin: EdgeInsets.only(top: 140),
alignment: Alignment.center,
child: Text(
e.toString(),
e.message,
style: bigTitleStyle,
),
);
Expand Down
Loading

0 comments on commit 7e3f10a

Please sign in to comment.