Refactor HTTPInterceptor and add CustomSnackBar helper

Refactor HTTPInterceptor to handle error responses and add a CustomSnackBar
helper to display snack bars. This will improve error handling and user
feedback in the application.
This commit is contained in:
Mohammad Salameh
2024-04-15 12:02:34 +03:00
parent 590c70a7d8
commit cfc395e210
10 changed files with 287 additions and 188 deletions

View File

@ -1,46 +1,63 @@
import 'package:dio/dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
import 'package:syncrow_app/features/auth/model/token.dart';
import 'package:syncrow_app/navigation/navigation_service.dart';
import 'dart:async';
import 'dart:developer' as developer;
import 'package:syncrow_app/services/api/network_exception.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
class HTTPInterceptor extends InterceptorsWrapper {
// @override
// void onResponse(Response response, ResponseInterceptorHandler handler) async {
// // Pass the response to the next interceptor or response handler.
// return handler.next(response);
// }
//
@override
@override
void onResponse(Response response, ResponseInterceptorHandler handler) async {
return handler.next(response);
}
@override
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
var storage = const FlutterSecureStorage();
var token = await storage.read(key: Token.loginAccessTokenKey);
options.headers['Authorization'] = 'Bearer $token';
// options.headers['Authorization'] = 'Bearer $token';
options.headers['Authorization'] = 'Bearer ${'${token!}123'}';
super.onRequest(options, handler);
}
//
// @override
// void onError(DioException err, ErrorInterceptorHandler handler) async {
// // TODO: Implement error handling logic.
// // This method is called when an error occurs during a request.
// super.onError(err, handler);
// }
//
// /// Validates the response and returns true if it is successful (status code 2xx).
// Future<bool> validateResponse(Response response) async {
// if (response.statusCode != null) {
// if (response.statusCode! >= 200 && response.statusCode! < 300) {
// // If the response status code is within the successful range (2xx),
// // return true indicating a successful response.
// return true;
// } else {
// // If the response status code is not within the successful range (2xx),
// // return false indicating an unsuccessful response.
// return false;
// }
// } else {
// // If the response status code is null, return false indicating an unsuccessful response.
// return false;
// }
// }
@override
void onError(DioException err, ErrorInterceptorHandler handler) async {
developer.log('Error Message: ${err.message}');
developer.log('Error res Code: ${err.response?.statusCode}');
developer.log('Error res Data: ${err.response?.data}');
developer.log('Error res status message: ${err.response?.statusMessage}');
ServerFailure failure = ServerFailure.fromDioError(err);
CustomSnackBar.displaySnackBar(failure.toString());
var storage = const FlutterSecureStorage();
var token = await storage.read(key: Token.loginAccessTokenKey);
if (err.response?.statusCode == 401 && token != null) {
await AuthCubit.get(NavigationService.navigatorKey.currentContext!)
.logout();
super.onError(err, handler);
}
}
/// Validates the response and returns true if it is successful (status code 2xx).
Future<bool> validateResponse(Response response) async {
if (response.statusCode != null) {
if (response.statusCode! >= 200 && response.statusCode! < 300) {
// If the response status code is within the successful range (2xx),
// return true indicating a successful response.
return true;
} else {
// If the response status code is not within the successful range (2xx),
// return false indicating an unsuccessful response.
return false;
}
} else {
// If the response status code is null, return false indicating an unsuccessful response.
return false;
}
}
}

View File

@ -32,9 +32,7 @@ class ServerFailure extends Failure {
// var document = parser.parse(dioError.response!.data.toString());
// var message = document.body!.text;
return ServerFailure.fromResponse(dioError.response!.statusCode!,
dioError.response!.data['message'].toString()
// message
);
dioError.response!.data['message']);
}
case DioExceptionType.cancel:
return ServerFailure("The request to ApiServer was canceled");
@ -50,31 +48,27 @@ class ServerFailure extends Failure {
}
}
factory ServerFailure.fromResponse(int statusCode, dynamic response) {
if (statusCode == 401 || statusCode == 403) {
return ServerFailure(response);
} else if (statusCode == 400) {
//response is list of errors
List<String> errors = [];
response.forEach((element) {
errors.add(element);
});
return ServerFailure(errors.join('\n'));
} else if (statusCode == 404) {
return ServerFailure("Your request not found, Please try later!");
} else if (statusCode == 500) {
return ServerFailure(response);
} else {
return ServerFailure("Opps there was an Error, Please try again!");
factory ServerFailure.fromResponse(int? statusCode, dynamic response) {
switch (statusCode) {
case 401:
case 403:
return ServerFailure(response);
case 400:
List<String> errors = [];
if (response['message'] is List) {
for (var error in response['message']) {
errors.add(error);
}
} else {
errors.add(response['message']);
}
return ServerFailure(errors.join('\n'));
case 404:
return ServerFailure("Your request not found, Please try later!");
case 500:
return ServerFailure(response);
default:
return ServerFailure("Opps there was an Error, Please try again!");
}
}
}
class ResponseFailure extends Failure {
ResponseFailure(super.errMessage);
@override
String toString() {
return errMessage;
}
}