From e172d69c3a10a140e0aae46611f318bf411bfb0f Mon Sep 17 00:00:00 2001 From: Dorian HODIN Date: Wed, 23 Nov 2022 20:58:33 +0100 Subject: [PATCH] Error Management in Flutter --- .../lib/services/position/area.dart | 14 ++++++++++++-- Sources/php_script/script/distance.php | 2 +- Sources/php_script/script/insert.php | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Sources/dafl_project_flutter/lib/services/position/area.dart b/Sources/dafl_project_flutter/lib/services/position/area.dart index daced88..5dd257f 100644 --- a/Sources/dafl_project_flutter/lib/services/position/area.dart +++ b/Sources/dafl_project_flutter/lib/services/position/area.dart @@ -36,8 +36,18 @@ class Location { "id": actualUser, }); var data = jsonDecode(response.body); - data.forEach((s)=> spot.putIfAbsent(s['user'], () => s['music'])); - return spot; + if (data.runtimeType==String) { + if (data=="Failed to connect to MySQL, connection timeout"){ + return Future.error("Failed to connect to MySQL, connection timeout"); + }else if (data=="The POST didn't return any values"){ + return Future.error("POST method failed"); + }else{ + return Future.error("Can't find current user in the database"); + } + }else { + data.forEach((s) => spot.putIfAbsent(s['user'], () => s['music'])); + return spot; + } } } diff --git a/Sources/php_script/script/distance.php b/Sources/php_script/script/distance.php index 03148aa..8ad361d 100644 --- a/Sources/php_script/script/distance.php +++ b/Sources/php_script/script/distance.php @@ -16,7 +16,7 @@ function meters($lat1, $lng1, $lat2, $lng2): float $connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error $res=include "config.php"; //$res get the result of the calling of "config.php" if ($res != 1){ //Check if config.php work - print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error + print (json_encode("Failed to connect to MySQL, connection timeout")); //Return a json string, so the dart script can interpret the error } if (!empty($_POST)) { $id = $_POST['id']; //Get the result of the POST method in id diff --git a/Sources/php_script/script/insert.php b/Sources/php_script/script/insert.php index c272129..7cb9fa6 100644 --- a/Sources/php_script/script/insert.php +++ b/Sources/php_script/script/insert.php @@ -2,7 +2,7 @@ $connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error $res=include "config.php"; //$res get the result of the calling of "config.php" if ($res != 1){ //Check if config.php work - print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error + print (json_encode("Failed to connect to MySQL, connection timeout")); //Return a json string, so the dart script can interpret the error } if (!empty($_POST)) { //Check if the method POST return something $id = mysqli_real_escape_string($connect, $_POST['id']); //Get the result of POST method