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/token.dart';
|
||||||
import 'package:syncrow_app/features/auth/model/user_model.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/authentication_api.dart';
|
||||||
|
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||||
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
||||||
|
|
||||||
part 'auth_state.dart';
|
part 'auth_state.dart';
|
||||||
@ -47,17 +48,23 @@ class AuthCubit extends Cubit<AuthState> {
|
|||||||
password: passwordController.text,
|
password: passwordController.text,
|
||||||
);
|
);
|
||||||
|
|
||||||
emit(AuthSuccess());
|
if (token.accessTokenIsNotEmpty) {
|
||||||
|
final parts = token.accessToken.split('.');
|
||||||
|
if (parts.length != 3) {
|
||||||
|
throw Exception('invalid access token');
|
||||||
|
}
|
||||||
|
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) {
|
} catch (e) {
|
||||||
|
if (e is ServerFailure) {
|
||||||
|
emit(AuthError(e.errMessage));
|
||||||
|
}
|
||||||
emit(AuthError(e.toString()));
|
emit(AuthError(e.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
final parts = token.accessToken.split('.');
|
|
||||||
if (parts.length != 3) {
|
|
||||||
throw Exception('invalid access token');
|
|
||||||
}
|
|
||||||
final payload = decodeBase64(parts[1]);
|
|
||||||
final payloadMap = json.decode(payload); //Map dictionary
|
|
||||||
user = UserModel.fromToken(payloadMap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ class Token {
|
|||||||
: accessToken = '',
|
: accessToken = '',
|
||||||
refreshToken = '';
|
refreshToken = '';
|
||||||
|
|
||||||
|
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
|
||||||
|
|
||||||
Token(
|
Token(
|
||||||
this.accessToken,
|
this.accessToken,
|
||||||
this.refreshToken,
|
this.refreshToken,
|
||||||
|
@ -35,106 +35,4 @@ class AuthenticationAPI {
|
|||||||
debugPrint("response: $response");
|
debugPrint("response: $response");
|
||||||
return 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:flutter/material.dart';
|
||||||
import 'package:syncrow_app/services/api/api_links_endpoints.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/http_interceptor.dart';
|
||||||
|
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||||
import 'package:syncrow_app/services/locator.dart';
|
import 'package:syncrow_app/services/locator.dart';
|
||||||
|
|
||||||
class HTTPService {
|
class HTTPService {
|
||||||
late Dio client = serviceLocator.get<Dio>();
|
final Dio client = serviceLocator.get<Dio>();
|
||||||
|
|
||||||
// final navigatorKey = GlobalKey<NavigatorState>();
|
// final navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
String certificateString = "";
|
String certificateString = "";
|
||||||
|
|
||||||
Dio setupDioClient() {
|
static Dio setupDioClient() {
|
||||||
client = Dio(
|
Dio client = Dio(
|
||||||
BaseOptions(
|
BaseOptions(
|
||||||
baseUrl: ApiEndpoints.baseUrl,
|
baseUrl: ApiEndpoints.baseUrl,
|
||||||
receiveDataWhenStatusError: true,
|
receiveDataWhenStatusError: true,
|
||||||
followRedirects: false,
|
followRedirects: false,
|
||||||
connectTimeout: const Duration(minutes: 1),
|
connectTimeout: const Duration(seconds: 60),
|
||||||
receiveTimeout: const Duration(minutes: 1),
|
receiveTimeout: const Duration(seconds: 60),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -53,16 +54,17 @@ class HTTPService {
|
|||||||
bool showServerMessage = true,
|
bool showServerMessage = true,
|
||||||
required T Function(dynamic) expectedResponseModel}) async {
|
required T Function(dynamic) expectedResponseModel}) async {
|
||||||
try {
|
try {
|
||||||
final response = await client.post(path,
|
final response = await client.post(
|
||||||
data: body, queryParameters: queryParameters, options: options);
|
path,
|
||||||
print("post response is $response");
|
data: body,
|
||||||
|
queryParameters: queryParameters,
|
||||||
|
options: options,
|
||||||
|
);
|
||||||
debugPrint("status code is ${response.statusCode}");
|
debugPrint("status code is ${response.statusCode}");
|
||||||
debugPrint("response data is ${response.data}");
|
debugPrint("response data is ${response.data}");
|
||||||
return expectedResponseModel(response.data);
|
return expectedResponseModel(response.data);
|
||||||
} catch (error) {
|
} on DioException catch (error) {
|
||||||
debugPrint("******* Error ********");
|
throw ServerFailure.fromDioError(error);
|
||||||
debugPrint(error.toString());
|
|
||||||
rethrow;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,12 +17,12 @@ class ServerFailure extends Failure {
|
|||||||
factory ServerFailure.fromDioError(DioException dioError) {
|
factory ServerFailure.fromDioError(DioException dioError) {
|
||||||
switch (dioError.type) {
|
switch (dioError.type) {
|
||||||
case DioExceptionType.connectionTimeout:
|
case DioExceptionType.connectionTimeout:
|
||||||
return ServerFailure("Connection timeout with ApiServer.");
|
return ServerFailure("Connection timeout with the Server.");
|
||||||
case DioExceptionType.sendTimeout:
|
case DioExceptionType.sendTimeout:
|
||||||
return ServerFailure("Send timeout with ApiServer.");
|
return ServerFailure("Send timeout with the Server.");
|
||||||
|
|
||||||
case DioExceptionType.receiveTimeout:
|
case DioExceptionType.receiveTimeout:
|
||||||
return ServerFailure("Receive timeout with ApiServer.");
|
return ServerFailure("Receive timeout with the Server.");
|
||||||
|
|
||||||
case DioExceptionType.badCertificate:
|
case DioExceptionType.badCertificate:
|
||||||
return ServerFailure("Bad certificate!");
|
return ServerFailure("Bad certificate!");
|
||||||
|
@ -9,5 +9,5 @@ initialSetup() {
|
|||||||
serviceLocator.registerSingleton<HTTPInterceptor>(HTTPInterceptor());
|
serviceLocator.registerSingleton<HTTPInterceptor>(HTTPInterceptor());
|
||||||
//Base classes
|
//Base classes
|
||||||
|
|
||||||
serviceLocator.registerSingleton<Dio>(HTTPService().setupDioClient());
|
serviceLocator.registerSingleton<Dio>(HTTPService.setupDioClient());
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ class ParserHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static String roundArea(dynamic value) {
|
static String roundArea(dynamic value) {
|
||||||
if (value.isNotEmpty && value.contains(' ')) {
|
if (value.accessTokenIsNotEmpty && value.contains(' ')) {
|
||||||
List<String> split = value!.split(' ');
|
List<String> split = value!.split(' ');
|
||||||
String formattedArea = ParserHelper.roundNumber(split[0]);
|
String formattedArea = ParserHelper.roundNumber(split[0]);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user