diff --git a/lib/model/exceptions/bad_request_error.dart b/lib/model/exceptions/bad_request_error.dart index 7e6e9060..1a3c9fbe 100644 --- a/lib/model/exceptions/bad_request_error.dart +++ b/lib/model/exceptions/bad_request_error.dart @@ -8,7 +8,7 @@ class BadRequestError extends NetworkError { static const statusCode = HttpStatus.badRequest; BadRequestError( - super.dioError, { + super.dioException, { super.statusCodeValue, }); @@ -25,7 +25,7 @@ class BadRequestError extends NetworkError { final dynamic data = err.response?.data; if (data is! Map) return BadRequestError(err); if (!data.containsKey('code')) return BadRequestError(err); - final code = data['code'] as String; + final code = data['code'] as String?; switch (code) { default: return BadRequestError(err); diff --git a/lib/model/exceptions/forbidden_error.dart b/lib/model/exceptions/forbidden_error.dart index e9f603a4..a6e73069 100644 --- a/lib/model/exceptions/forbidden_error.dart +++ b/lib/model/exceptions/forbidden_error.dart @@ -8,7 +8,7 @@ class ForbiddenError extends NetworkError { static const statusCode = HttpStatus.forbidden; ForbiddenError( - super.dioError, { + super.dioException, { super.statusCodeValue, }); @@ -25,7 +25,7 @@ class ForbiddenError extends NetworkError { final dynamic data = err.response?.data; if (data is! Map) return ForbiddenError(err); if (!data.containsKey('code')) return ForbiddenError(err); - final code = data['code'] as String; + final code = data['code'] as String?; switch (code) { default: return ForbiddenError(err); diff --git a/lib/model/exceptions/general_error.dart b/lib/model/exceptions/general_error.dart index 8f070100..38de4a1c 100644 --- a/lib/model/exceptions/general_error.dart +++ b/lib/model/exceptions/general_error.dart @@ -1,12 +1,9 @@ import 'package:flutter_template/util/locale/localization_keys.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; -class GeneralNetworkError extends NetworkError { - GeneralNetworkError(super.dioError); +class GeneralError with LocalizedError { + GeneralError() : super(); @override String getLocalizedKey() => LocalizationKeys.errorGeneral; - - @override - String? get getErrorCode => null; } diff --git a/lib/model/exceptions/general_network_error.dart b/lib/model/exceptions/general_network_error.dart index 38de4a1c..9653f888 100644 --- a/lib/model/exceptions/general_network_error.dart +++ b/lib/model/exceptions/general_network_error.dart @@ -1,9 +1,12 @@ import 'package:flutter_template/util/locale/localization_keys.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; -class GeneralError with LocalizedError { - GeneralError() : super(); +class GeneralNetworkError extends NetworkError { + GeneralNetworkError(super.dioException); @override String getLocalizedKey() => LocalizationKeys.errorGeneral; + + @override + String? get getErrorCode => null; } diff --git a/lib/model/exceptions/internal_server_error.dart b/lib/model/exceptions/internal_server_error.dart index 5a043857..d69949e7 100644 --- a/lib/model/exceptions/internal_server_error.dart +++ b/lib/model/exceptions/internal_server_error.dart @@ -7,7 +7,7 @@ class InternalServerError extends NetworkError { static const statusCode = HttpStatus.internalServerError; InternalServerError( - super.dioError, { + super.dioException, { super.statusCodeValue, }); diff --git a/lib/model/exceptions/no_internet_error.dart b/lib/model/exceptions/no_internet_error.dart index 24c6eda2..ba665ac6 100644 --- a/lib/model/exceptions/no_internet_error.dart +++ b/lib/model/exceptions/no_internet_error.dart @@ -2,7 +2,7 @@ import 'package:flutter_template/util/locale/localization_keys.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; class NoInternetError extends NetworkError { - NoInternetError(super.dioError); + NoInternetError(super.dioException); @override String getLocalizedKey() => LocalizationKeys.errorNoNetwork; diff --git a/lib/model/exceptions/un_authorized_error.dart b/lib/model/exceptions/un_authorized_error.dart index 36095816..a2e57e62 100644 --- a/lib/model/exceptions/un_authorized_error.dart +++ b/lib/model/exceptions/un_authorized_error.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:dio/dio.dart'; import 'package:flutter_template/util/locale/localization_keys.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; @@ -7,7 +8,7 @@ class UnAuthorizedError extends NetworkError { static const statusCode = HttpStatus.unauthorized; UnAuthorizedError( - super.dioError, { + super.dioException, { super.statusCodeValue, }); @@ -19,4 +20,15 @@ class UnAuthorizedError extends NetworkError { if (statusCodeValue == null) return '$statusCode'; return '$statusCode [$statusCodeValue]'; } + + static NetworkError parseError(DioException err) { + final dynamic data = err.response?.data; + if (data is! Map) return UnAuthorizedError(err); + if (!data.containsKey('code')) return UnAuthorizedError(err); + final code = data['code'] as String?; + switch (code) { + default: + return UnAuthorizedError(err); + } + } } diff --git a/lib/util/interceptor/network_error_interceptor.dart b/lib/util/interceptor/network_error_interceptor.dart index 28d2a185..0246aaef 100644 --- a/lib/util/interceptor/network_error_interceptor.dart +++ b/lib/util/interceptor/network_error_interceptor.dart @@ -2,7 +2,7 @@ import 'package:dio/dio.dart'; import 'package:flutter_template/model/exceptions/bad_request_error.dart'; import 'package:flutter_template/model/exceptions/code_error.dart'; import 'package:flutter_template/model/exceptions/forbidden_error.dart'; -import 'package:flutter_template/model/exceptions/general_error.dart'; +import 'package:flutter_template/model/exceptions/general_network_error.dart'; import 'package:flutter_template/model/exceptions/internal_server_error.dart'; import 'package:flutter_template/model/exceptions/no_internet_error.dart'; import 'package:flutter_template/model/exceptions/un_authorized_error.dart'; @@ -31,7 +31,7 @@ class NetworkErrorInterceptor extends SimpleInterceptor { final statusCode = error.response?.statusCode; switch (statusCode) { case UnAuthorizedError.statusCode: - return UnAuthorizedError(error); + return UnAuthorizedError.parseError(error); case BadRequestError.statusCode: return BadRequestError.parseError(error); case ForbiddenError.statusCode: diff --git a/lib/util/interceptor/network_log_interceptor.dart b/lib/util/interceptor/network_log_interceptor.dart index 7ab2d817..8099cc66 100644 --- a/lib/util/interceptor/network_log_interceptor.dart +++ b/lib/util/interceptor/network_log_interceptor.dart @@ -1,7 +1,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; -import 'package:flutter_template/model/exceptions/general_error.dart'; +import 'package:flutter_template/model/exceptions/general_network_error.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; import 'package:injectable/injectable.dart'; diff --git a/test/repository/refresh/refresh_repository_test.dart b/test/repository/refresh/refresh_repository_test.dart index c58ee11d..b7ede64e 100644 --- a/test/repository/refresh/refresh_repository_test.dart +++ b/test/repository/refresh/refresh_repository_test.dart @@ -1,5 +1,5 @@ import 'package:dio/dio.dart'; -import 'package:flutter_template/model/exceptions/general_network_error.dart'; +import 'package:flutter_template/model/exceptions/general_error.dart'; import 'package:flutter_template/model/exceptions/un_authorized_error.dart'; import 'package:flutter_template/repository/refresh/refresh_repository.dart'; import 'package:flutter_template/repository/secure_storage/auth/auth_storage.dart'; diff --git a/test/util/interceptor/network_error_interceptor_test.dart b/test/util/interceptor/network_error_interceptor_test.dart index 086cfe49..02c1e8be 100644 --- a/test/util/interceptor/network_error_interceptor_test.dart +++ b/test/util/interceptor/network_error_interceptor_test.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_template/model/exceptions/bad_request_error.dart'; import 'package:flutter_template/model/exceptions/code_error.dart'; import 'package:flutter_template/model/exceptions/forbidden_error.dart'; -import 'package:flutter_template/model/exceptions/general_error.dart'; +import 'package:flutter_template/model/exceptions/general_network_error.dart'; import 'package:flutter_template/model/exceptions/internal_server_error.dart'; import 'package:flutter_template/model/exceptions/un_authorized_error.dart'; import 'package:flutter_template/util/env/flavor_config.dart'; diff --git a/test/viewmodel/login/login_viewmodel_test.dart b/test/viewmodel/login/login_viewmodel_test.dart index 19bc9c41..98ea28e5 100644 --- a/test/viewmodel/login/login_viewmodel_test.dart +++ b/test/viewmodel/login/login_viewmodel_test.dart @@ -1,4 +1,4 @@ -import 'package:flutter_template/model/exceptions/general_network_error.dart'; +import 'package:flutter_template/model/exceptions/general_error.dart'; import 'package:flutter_template/navigator/main_navigator.dart'; import 'package:flutter_template/navigator/onboarding_navigator.dart'; import 'package:flutter_template/repository/login/login_repository.dart'; diff --git a/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.dart b/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.dart index 06deb86c..fbce3e79 100644 --- a/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.dart +++ b/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.dart @@ -1,4 +1,4 @@ -import 'package:flutter_template/model/exceptions/general_network_error.dart'; +import 'package:flutter_template/model/exceptions/general_error.dart'; import 'package:flutter_template/model/webservice/todo/todo.dart'; import 'package:flutter_template/navigator/main_navigator.dart'; import 'package:flutter_template/repository/todo/todo_repository.dart';