From 0d1ea5d66feac87d01cb7acc8a2d1ede49af7912 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 20 Sep 2022 23:04:01 +0200 Subject: [PATCH] add error for request get --- covas_mobile/lib/MyHomePage.dart | 71 +++++++++++++++++++++++------ covas_mobile/lib/classes/alert.dart | 3 +- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/covas_mobile/lib/MyHomePage.dart b/covas_mobile/lib/MyHomePage.dart index e3c8a3d..eb0db33 100644 --- a/covas_mobile/lib/MyHomePage.dart +++ b/covas_mobile/lib/MyHomePage.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'dart:convert'; +import 'package:covas_mobile/classes/alert.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; @@ -54,27 +55,71 @@ class MyHomePage extends StatefulWidget { State createState() => _MyHomePageState(); } -class _MyHomePageState extends State { +class _MyHomePageState extends State with ShowErrorDialog { String listUser = ""; Future _incrementCounter() async { SharedPreferences prefs = await SharedPreferences.getInstance(); - var user = prefs.getString("user"); - var jwt = prefs.getString("jwt"); + var user = prefs.getString("user") ?? ""; + var jwt = prefs.getString("jwt") ?? ""; String former = ""; - stderr.writeln("User ${user}"); - stderr.writeln("Jwt ${jwt}"); - var urlGet = Uri.parse("http://localhost:8083/api/users"); + if ((user.isNotEmpty) && (jwt.isNotEmpty)) { + var urlGet = Uri.parse("http://localhost:8083/api/users"); - var responseGet = await http - .get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'}); + var responseGet = await http + .get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'}); - stderr.writeln('Response Get status: ${responseGet.statusCode}'); - stderr.writeln('Response Get body: ${responseGet.body}'); - var json = jsonDecode(responseGet.body); - for (var user in json) { - former = "$former\n ${user['name']}"; + stderr.writeln('Response Get status: ${responseGet.statusCode}'); + if (responseGet.statusCode == 200) { + stderr.writeln('Response Get body: ${responseGet.body}'); + var json = jsonDecode(responseGet.body); + for (var user in json) { + former = "$former\n ${user['name']}"; + } + } else { + var text = ""; + switch (responseGet.statusCode) { + case 400: + { + text = "RequĂȘte mal construite"; + } + break; + case 406: + { + text = "Mot de passe incorrect"; + } + break; + case 404: + { + text = "Utilisateur inconnu"; + } + break; + case 403: + { + text = "Utilisateur desactive"; + } + break; + case 410: + { + text = "Token invalide"; + } + break; + case 500: + { + text = "Probleme interne du serveur"; + } + break; + default: + { + text = "Probleme d'authentification inconnu"; + } + break; + } + showErrorDialog(context, text); + } + } else { + showErrorDialog(context, "Cache invalide"); } setState(() { diff --git a/covas_mobile/lib/classes/alert.dart b/covas_mobile/lib/classes/alert.dart index 0e30411..dfcac3f 100644 --- a/covas_mobile/lib/classes/alert.dart +++ b/covas_mobile/lib/classes/alert.dart @@ -1,7 +1,8 @@ import 'package:flutter/material.dart'; + import '../main.dart'; -mixin ShowErrorDialog on State { +mixin ShowErrorDialog on State { void showErrorDialog(BuildContext context, String text) { // Create AlertDialog AlertDialog dialog = AlertDialog(