Add better debugging for http requests

This commit is contained in:
Reimar 2024-09-13 12:02:05 +02:00
parent c389381b1e
commit 90fccde9e6
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268

View File

@ -13,7 +13,7 @@ enum ApiService {
} }
Future<String?> request(BuildContext? context, ApiService service, String method, String path, dynamic body) async { Future<String?> 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 messenger = context != null ? ScaffoldMessenger.of(context) : null;
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
@ -49,18 +49,22 @@ Future<String?> request(BuildContext? context, ApiService service, String method
); );
} }
} catch (e) { } catch (e) {
debugPrint('Can\'t send requst: ' + e.toString());
messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
debug += 'FAILED\n $e';
debugPrint(debug);
return null; return null;
} }
debug += 'HTTP ${response.statusCode}\n ${response.body}';
debugPrint(debug);
if (response.statusCode < 200 || response.statusCode >= 300) { if (response.statusCode < 200 || response.statusCode >= 300) {
try { try {
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title']))); messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title'])));
debugPrint('API error: ' + json['message']);
} catch (e) { } catch (e) {
debugPrint('Can\'t parse response: ' + response.body);
messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})'))); messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})')));
} }
return null; return null;