mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 17:47:28 +00:00
Added Error handling
This commit is contained in:
@ -6,6 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/services/api/authentication_api.dart';
|
||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
||||
|
||||
part 'auth_state.dart';
|
||||
@ -47,11 +48,7 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
password: passwordController.text,
|
||||
);
|
||||
|
||||
emit(AuthSuccess());
|
||||
} catch (e) {
|
||||
emit(AuthError(e.toString()));
|
||||
}
|
||||
|
||||
if (token.accessTokenIsNotEmpty) {
|
||||
final parts = token.accessToken.split('.');
|
||||
if (parts.length != 3) {
|
||||
throw Exception('invalid access token');
|
||||
@ -59,5 +56,15 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
final payload = decodeBase64(parts[1]);
|
||||
final payloadMap = json.decode(payload); //Map dictionary
|
||||
user = UserModel.fromToken(payloadMap);
|
||||
emit(AuthSuccess());
|
||||
} else {
|
||||
emit(AuthError('Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ServerFailure) {
|
||||
emit(AuthError(e.errMessage));
|
||||
}
|
||||
emit(AuthError(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ class Token {
|
||||
: accessToken = '',
|
||||
refreshToken = '';
|
||||
|
||||
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
|
||||
|
||||
Token(
|
||||
this.accessToken,
|
||||
this.refreshToken,
|
||||
|
@ -35,106 +35,4 @@ class AuthenticationAPI {
|
||||
debugPrint("response: $response");
|
||||
return response;
|
||||
}
|
||||
|
||||
// static Future<SuccessResponse> updateUserInfo(
|
||||
// Map<String, dynamic> data) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.updateUserInfo,
|
||||
// body: data,
|
||||
// expectedResponseModel: (json) {
|
||||
// SuccessResponse token = SuccessResponse.fromJson(json);
|
||||
// return token;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<LoginWithPhoneResponse> loginWithPhone(
|
||||
// {required LoginWithPhone data, String? recaptchaToken}) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.loginWithChannel,
|
||||
// body: data.toJson(),
|
||||
// options: Options(headers: {
|
||||
// 'captcha-token': recaptchaToken,
|
||||
// 'captcha-site-key': Platform.isAndroid
|
||||
// ? dotenv.env['RECAPTCH_ANDROID_SITE_KEY'] ?? ''
|
||||
// : dotenv.env['RECAPTCH_IOS_SITE_KEY'] ?? ''
|
||||
// }),
|
||||
// expectedResponseModel: (json) {
|
||||
// LoginWithPhoneResponse result = LoginWithPhoneResponse.fromJson(json);
|
||||
// return result;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<List<dynamic>> countryCodes() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.getCountyCode,
|
||||
// expectedResponseModel: (json) {
|
||||
// List<dynamic> result = json.toList();
|
||||
// return result;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> sendNotificationToken({
|
||||
// required Map<String, String> data,
|
||||
// }) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.notificationToken,
|
||||
// body: data,
|
||||
// showServerMessage: false,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> logout() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.logout,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// // print(json);
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> deleteAccount() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.deleteAccount,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
|
||||
// static Future<Token> refreshToken(Map<String, dynamic> data) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.refreshToken,
|
||||
// showServerMessage: false,
|
||||
// body: data,
|
||||
// expectedResponseModel: (json) {
|
||||
// Token token = Token.fromJson(json);
|
||||
// return token;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
}
|
||||
|
@ -2,23 +2,24 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||
import 'package:syncrow_app/services/api/http_interceptor.dart';
|
||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||
import 'package:syncrow_app/services/locator.dart';
|
||||
|
||||
class HTTPService {
|
||||
late Dio client = serviceLocator.get<Dio>();
|
||||
final Dio client = serviceLocator.get<Dio>();
|
||||
|
||||
// final navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
String certificateString = "";
|
||||
|
||||
Dio setupDioClient() {
|
||||
client = Dio(
|
||||
static Dio setupDioClient() {
|
||||
Dio client = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: ApiEndpoints.baseUrl,
|
||||
receiveDataWhenStatusError: true,
|
||||
followRedirects: false,
|
||||
connectTimeout: const Duration(minutes: 1),
|
||||
receiveTimeout: const Duration(minutes: 1),
|
||||
connectTimeout: const Duration(seconds: 60),
|
||||
receiveTimeout: const Duration(seconds: 60),
|
||||
),
|
||||
);
|
||||
|
||||
@ -53,16 +54,17 @@ class HTTPService {
|
||||
bool showServerMessage = true,
|
||||
required T Function(dynamic) expectedResponseModel}) async {
|
||||
try {
|
||||
final response = await client.post(path,
|
||||
data: body, queryParameters: queryParameters, options: options);
|
||||
print("post response is $response");
|
||||
final response = await client.post(
|
||||
path,
|
||||
data: body,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
debugPrint("status code is ${response.statusCode}");
|
||||
debugPrint("response data is ${response.data}");
|
||||
return expectedResponseModel(response.data);
|
||||
} catch (error) {
|
||||
debugPrint("******* Error ********");
|
||||
debugPrint(error.toString());
|
||||
rethrow;
|
||||
} on DioException catch (error) {
|
||||
throw ServerFailure.fromDioError(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,12 +17,12 @@ class ServerFailure extends Failure {
|
||||
factory ServerFailure.fromDioError(DioException dioError) {
|
||||
switch (dioError.type) {
|
||||
case DioExceptionType.connectionTimeout:
|
||||
return ServerFailure("Connection timeout with ApiServer.");
|
||||
return ServerFailure("Connection timeout with the Server.");
|
||||
case DioExceptionType.sendTimeout:
|
||||
return ServerFailure("Send timeout with ApiServer.");
|
||||
return ServerFailure("Send timeout with the Server.");
|
||||
|
||||
case DioExceptionType.receiveTimeout:
|
||||
return ServerFailure("Receive timeout with ApiServer.");
|
||||
return ServerFailure("Receive timeout with the Server.");
|
||||
|
||||
case DioExceptionType.badCertificate:
|
||||
return ServerFailure("Bad certificate!");
|
||||
|
@ -9,5 +9,5 @@ initialSetup() {
|
||||
serviceLocator.registerSingleton<HTTPInterceptor>(HTTPInterceptor());
|
||||
//Base classes
|
||||
|
||||
serviceLocator.registerSingleton<Dio>(HTTPService().setupDioClient());
|
||||
serviceLocator.registerSingleton<Dio>(HTTPService.setupDioClient());
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ class ParserHelper {
|
||||
}
|
||||
|
||||
static String roundArea(dynamic value) {
|
||||
if (value.isNotEmpty && value.contains(' ')) {
|
||||
if (value.accessTokenIsNotEmpty && value.contains(' ')) {
|
||||
List<String> split = value!.split(' ');
|
||||
String formattedArea = ParserHelper.roundNumber(split[0]);
|
||||
|
||||
|
Reference in New Issue
Block a user