Skip to content

Commit

Permalink
Fixes #1757 implement Proper error handling for ( getaddrinfo ENOTFOU… (
Browse files Browse the repository at this point in the history
#1767)

* Fixes #1757 implement Proper error handling for ( getaddrinfo ENOTFOUND metadata.google.internal.)

* Adding Docs

* Adding Docs

* Adding Docs

* Add docs

* Add dynamic type warning

* test

* format

* format

* Update hi.json
  • Loading branch information
Ayush0Chaudhary authored Aug 5, 2023
1 parent 791b133 commit fdc6445
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,6 @@
"Information": "Informatie",
"No account registered with this email": "Server werkt niet/verkeerde url",
"Dismiss": "afwijzen",
"No organizations found Please contact your admin": "Geen organisaties gevonden! Neem contact op met uw beheerder"
"No organizations found Please contact your admin": "Geen organisaties gevonden! Neem contact op met uw beheerder",
"Notification Feature is not installed": "Meddelelsesfunktionen er ikke installeret"
}
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"Enter your password": "Enter your password",
"Forgot password": "Forgot password",
"Login": "Login",

"Notification Feature is not installed": "Notification Feature is not installed",
"Sit back relax, we'll": "Sit back relax, we'll",
"Recover": "Recover",
"your password": "your password",
Expand Down
3 changes: 3 additions & 0 deletions lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"Login": "Acceso",
"Sit back relax, we'll": "Siéntese, ",
"Recover": "",

"Notification Feature is not installed": "La función de notificación no está instalada",

"your password": "relájese, recuperaremos su contraseña",
"Recover Password": "Recupera tu contraseña",
"Select Language": "Seleccione el idioma",
Expand Down
2 changes: 2 additions & 0 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"Change Password": "Changer le mot de passe",

"Email Hint": "[email protected]",
"Notification Feature is not installed": "La fonction de notification n'est pas installée",

"Enter your registered Email": "Entrez votre email enregistré",
"Enter your password": "Tapez votre mot de passe",
"Forgot password": "Mot de passe oublié",
Expand Down
1 change: 1 addition & 0 deletions lang/hi.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Select Language": "भाषा का चयन करें",
"Default": "डिफ़ॉल्ट मान",
"Select": "चुनते हैं",
"Notification Feature is not installed": "अधिसूचना सुविधा स्थापित नहीं है",
"Selected Organization": "चयनित संगठन",
"Continue": "जारी रखें",
"Enter Organization URL": "संगठन URL दर्ज करें",
Expand Down
2 changes: 2 additions & 0 deletions lang/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"Login": "ログイン",
"Sit back relax, we'll": "ゆったりとおくつろぎください。",
"Recover": "回復",

"Notification Feature is not installed": "通知機能がインストールされていません",
"your password": "あなたのパスワード",
"Recover Password": "パスワードを回復",
"Select Language": "言語を選択する",
Expand Down
3 changes: 3 additions & 0 deletions lang/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"Forgot password": "Esqueceu sua senha",
"Login": "Conecte-se",
"Sit back relax, we'll": "Sente-se relaxe, vamos",

"Notification Feature is not installed": "O recurso de notificação não está instalado",

"Recover": "Recuperar",
"your password": "sua senha",
"Recover Password": "Recuperar senha",
Expand Down
2 changes: 2 additions & 0 deletions lang/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"Login": "登录",
"Sit back relax, we'll": "高枕无忧, ",
"Recover": "",
"Notification Feature is not installed": "未安装通知功能\n",

"your password": "我们会找回您的密码",
"Recover Password": "恢复你的密码",
"Select Language": "选择语言",
Expand Down
131 changes: 116 additions & 15 deletions lib/services/database_mutation_functions.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// ignore_for_file: talawa_api_doc, avoid_dynamic_calls
// ignore_for_file: talawa_good_doc_comments

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
Expand All @@ -20,34 +17,71 @@ import 'package:talawa/utils/queries.dart';
/// * `refreshAccessToken`
/// * `fetchOrgById`
class DataBaseMutationFunctions {
/// Client Auth for handling non-authenticated request.
late GraphQLClient clientNonAuth;

/// Client Auth for handling authenticated request.
late GraphQLClient clientAuth;

/// Query passed by fucntion calling this function.
late Queries _query;

/// initialization function.
///
/// **params**:
/// None
///
/// **returns**:
/// None
void init() {
clientNonAuth = graphqlConfig.clientToQuery();
clientAuth = graphqlConfig.authClient();
_query = Queries();
}

// initialising default messages for an event.
/// Graphql error for handling.
GraphQLError userNotFound = const GraphQLError(message: 'User not found');

/// Graphql error for handling.
GraphQLError userNotAuthenticated =
const GraphQLError(message: 'User is not authenticated');

/// Graphql error for handling.
GraphQLError emailAccountPresent =
const GraphQLError(message: 'Email address already exists');

/// Graphql error for handling.
GraphQLError wrongCredentials =
const GraphQLError(message: 'Invalid credentials');

/// Graphql error for handling.
GraphQLError organizationNotFound =
const GraphQLError(message: 'Organization not found');

/// Graphql error for handling.
GraphQLError refreshAccessTokenExpiredException = const GraphQLError(
message:
'Access Token has expired. Please refresh session.: Undefined location',
);

/// Graphql error for handling.
GraphQLError memberRequestExist =
const GraphQLError(message: 'Membership Request already exists');

/// Graphql error for handling.
GraphQLError notifFeatureNotInstalled = const GraphQLError(
message:
'Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND',
);

/// This function is used to check if any exceptions or error encountered. The return type is [boolean].
///
/// **params**:
/// * `exception`: OperationException which occur when calling for graphql post request
/// * `showSnackBar`: Tell if the the place where this function is called wants a SnackBar on error
///
/// **returns**:
/// * `bool?`: returns a bool whether or not their is error, can be null
bool? encounteredExceptionOrError(
OperationException exception, {
bool showSnackBar = true,
Expand All @@ -65,7 +99,8 @@ class DataBaseMutationFunctions {
}
return false;
}
// looping through graphQL errors.

/// looping through graphQL errors.
debugPrint(exception.graphqlErrors.toString());
for (int i = 0; i < exception.graphqlErrors.length; i++) {
// if the error message is "Access Token has expired. Please refresh session.: Undefined location"
Expand All @@ -80,7 +115,8 @@ class DataBaseMutationFunctions {
print('client refreshed');
return true;
}
// if the error message is "User is not authenticated"

/// if the error message is "User is not authenticated"
if (exception.graphqlErrors[i].message == userNotAuthenticated.message) {
print('client refreshed');
refreshAccessToken(userConfig.currentUser.refreshToken!).then(
Expand All @@ -90,7 +126,8 @@ class DataBaseMutationFunctions {
);
return true;
}
// if the error message is "User not found"

/// if the error message is "User not found"
if (exception.graphqlErrors[i].message == userNotFound.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
Expand All @@ -102,7 +139,8 @@ class DataBaseMutationFunctions {
}
return false;
}
// if the error message is "Membership Request already exists"

/// if the error message is "Membership Request already exists"
if (exception.graphqlErrors[i].message == memberRequestExist.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
Expand All @@ -114,7 +152,8 @@ class DataBaseMutationFunctions {
}
return false;
}
// if the error message is "Invalid credentials"

/// if the error message is "Invalid credentials"
if (exception.graphqlErrors[i].message == wrongCredentials.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
Expand All @@ -126,7 +165,8 @@ class DataBaseMutationFunctions {
}
return false;
}
// if the error message is "Organization not found"

/// if the error message is "Organization not found"
if (exception.graphqlErrors[i].message == organizationNotFound.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
Expand All @@ -138,7 +178,8 @@ class DataBaseMutationFunctions {
}
return false;
}
// if the error message is "Email address already exists"

/// if the error message is "Email address already exists"
if (exception.graphqlErrors[i].message == emailAccountPresent.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
Expand All @@ -150,6 +191,18 @@ class DataBaseMutationFunctions {
}
return false;
}
if (exception.graphqlErrors[i].message ==
notifFeatureNotInstalled.message) {
if (showSnackBar) {
WidgetsBinding.instance.addPostFrameCallback(
(_) => navigationService.showTalawaErrorDialog(
"Notification Feature is not installed",
MessageType.error,
),
);
}
return false;
}
}
// if the error is unknown

Expand All @@ -163,6 +216,13 @@ class DataBaseMutationFunctions {
}

/// This function is used to run the graph-ql query for authentication.
///
/// **params**:
/// * `query`: query is used to fetch data in graphql, for more info read graphql docs
/// * `variables`: variables to be passed with query
///
/// **returns**:
/// * `Future<dynamic>`: it returns Future of dynamic
Future<dynamic> gqlAuthQuery(
String query, {
Map<String, dynamic>? variables,
Expand All @@ -185,6 +245,13 @@ class DataBaseMutationFunctions {
}

/// This function is used to run the graph-ql mutation for authenticated user.
///
/// **params**:
/// * `mutation`: mutation is used to change/add/delete data in graphql, for more info read graphql docs
/// * `variables`: variables to be passed with mutation
///
/// **returns**:
/// * `Future<dynamic>`: it returns Future of dynamic
Future<dynamic> gqlAuthMutation(
String mutation, {
Map<String, dynamic>? variables,
Expand All @@ -208,6 +275,15 @@ class DataBaseMutationFunctions {
}

/// This function is used to run the graph-ql mutation to authenticate the non signed-in user.
///
///
/// **params**:
/// * `mutation`: mutation is used to change/add/delete data in graphql, for more info read graphql docs
/// * `variables`: variables to be passed with mutation
/// * `reCall`: when not first fetch call
///
/// **returns**:
/// * `Future<dynamic>`: it returns Future of dynamic
Future<dynamic> gqlNonAuthMutation(
String mutation, {
Map<String, dynamic>? variables,
Expand All @@ -232,7 +308,14 @@ class DataBaseMutationFunctions {
}

/// This function is used to run the graph-ql query for the non signed-in user.
Future<QueryResult?> gqlNonAuthQuery(
///
/// **params**:
/// * `query`: query is used to fetch data in graphql, for more info read graphql docs
/// * `variables`: variables to be passed with query
///
/// **returns**:
/// * `Future<QueryResult<Object?>?>`: it returns Future of QueryResult, contains all data
Future<QueryResult<Object?>?> gqlNonAuthQuery(
String query, {
Map<String, dynamic>? variables,
}) async {
Expand All @@ -255,6 +338,12 @@ class DataBaseMutationFunctions {
}

/// This function is used to refresh the Authenication token to access the application.
///
/// **params**:
/// * `refreshToken`: Needed for authentication
///
/// **returns**:
/// * `Future<bool>`: it returns Future of dynamic
Future<bool> refreshAccessToken(String refreshToken) async {
// run the graphQL mutation
final QueryResult result = await clientNonAuth.mutate(
Expand All @@ -274,8 +363,12 @@ class DataBaseMutationFunctions {
}
} else if (result.data != null && result.isConcrete) {
userConfig.updateAccessToken(
refreshToken: result.data!['refreshToken']['refreshToken'].toString(),
accessToken: result.data!['refreshToken']['accessToken'].toString(),
refreshToken: (result.data!['refreshToken']
as Map<String, dynamic>)['refreshToken']
.toString(),
accessToken: (result.data!['refreshToken']
as Map<String, dynamic>)['accessToken']
.toString(),
);
databaseFunctions.init();
return true;
Expand All @@ -284,6 +377,12 @@ class DataBaseMutationFunctions {
}

/// This function fetch the organization using the [id] passed.
///
/// **params**:
/// * `id`: id that identifies a particular org
///
/// **returns**:
/// * `Future<dynamic>`: it returns Future of dynamic
Future<dynamic> fetchOrgById(String id) async {
final QueryResult result = await clientNonAuth
.mutate(MutationOptions(document: gql(_query.fetchOrgById(id))));
Expand All @@ -295,7 +394,9 @@ class DataBaseMutationFunctions {
}
} else if (result.data != null && result.isConcrete) {
return OrgInfo.fromJson(
result.data!['organizations'][0] as Map<String, dynamic>,
// ignore: collection_methods_unrelated_type
(result.data!['organizations'] as Map<String, dynamic>)[0]
as Map<String, dynamic>,
);
}
return false;
Expand Down

0 comments on commit fdc6445

Please sign in to comment.