From 90fccde9e6e0ac2c3e3ce164232da6a03982eebe Mon Sep 17 00:00:00 2001 From: Reimar Date: Fri, 13 Sep 2024 12:02:05 +0200 Subject: [PATCH] Add better debugging for http requests --- Mobile/lib/api.dart | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 1ab512a..63b0bfa 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -13,7 +13,7 @@ enum ApiService { } Future request(BuildContext? context, ApiService service, String method, String path, dynamic body) async { - debugPrint('$method $path'); + var debug = '$method $path\n $body\n'; final messenger = context != null ? ScaffoldMessenger.of(context) : null; final prefs = await SharedPreferences.getInstance(); @@ -49,18 +49,22 @@ Future request(BuildContext? context, ApiService service, String method ); } } catch (e) { - debugPrint('Can\'t send requst: ' + e.toString()); messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); + + debug += 'FAILED\n $e'; + debugPrint(debug); + return null; } + debug += 'HTTP ${response.statusCode}\n ${response.body}'; + debugPrint(debug); + if (response.statusCode < 200 || response.statusCode >= 300) { try { final json = jsonDecode(response.body); messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title']))); - debugPrint('API error: ' + json['message']); } catch (e) { - debugPrint('Can\'t parse response: ' + response.body); messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})'))); } return null;