Skip to content
Snippets Groups Projects
Commit 92a83c8b authored by doodlezucc's avatar doodlezucc
Browse files

show fetch errors in dialog

parent 9f98ef70
Branches
No related tags found
No related merge requests found
import 'package:flutter/material.dart';
class ErrorDialog extends StatelessWidget {
final String title;
final Object? error;
const ErrorDialog({super.key, required this.title, required this.error});
@override
Widget build(BuildContext context) {
return SimpleDialog(
title: Text(title),
children: [
SimpleDialogOption(
child: Text('An error occurred: ${error.runtimeType}\n$error'),
),
],
);
}
}
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'branding.dart';
import 'context.dart';
import 'dialogs/error_dialog.dart';
import 'drawer/drawer.dart';
import 'timetable/timetable.dart';
import 'toolbar/toolbar.dart';
......@@ -63,7 +65,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
loadProfile();
loadProfile().onError((error, _) {
_showErrorDialog('Failed to connect', error);
});
}
@override
......@@ -74,6 +78,14 @@ class _MyHomePageState extends State<MyHomePage> {
super.dispose();
}
void _showErrorDialog(String title, Object? error) {
stderr.write(error);
showDialog(
context: context,
builder: (context) => ErrorDialog(title: title, error: error),
);
}
Future<void> loadProfile() async {
final profile = await appContext.profile.firstNotNull;
final classes = await untis.allClasses.fetched;
......
......@@ -84,14 +84,21 @@ class UntisFetch {
final responseString = await utf8.decodeStream(response);
final onDownloadEnd = DateTime.now();
final bodyJson = jsonDecode(responseString);
if (response.statusCode != 200) {
throw FetchError(response.statusCode, responseString);
}
try {
final bodyJson = jsonDecode(responseString);
final onDecodeEnd = DateTime.now();
final stats =
FetchStats(onStart, onDownloadStart, onDownloadEnd, onDecodeEnd);
return (bodyJson as Json, resultCookies, stats);
} on FormatException catch (_) {
throw FetchError(response.statusCode, responseString);
}
}
Future<Json> requestOptions(RequestOptions options) async {
......@@ -148,3 +155,13 @@ class FetchStats {
.join(', ');
}
}
class FetchError extends Error {
final int statusCode;
final String body;
FetchError(this.statusCode, this.body);
@override
String toString() => 'Server responded with status code $statusCode\n$body';
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment