mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 09:42:50 +01:00
Catch and handle WgerHttpException specially
This shows the server errors directly in the form, instead of a modal dialog (which is the default for all the other forms and places that throw the exception)
This commit is contained in:
@@ -327,10 +327,10 @@ List<Widget> formatErrors(List<ApiError> errors, {Color? color}) {
|
||||
return errorList;
|
||||
}
|
||||
|
||||
class FormErrorsWidget extends StatelessWidget {
|
||||
class FormHttpErrorsWidget extends StatelessWidget {
|
||||
final WgerHttpException exception;
|
||||
|
||||
const FormErrorsWidget(this.exception, {super.key});
|
||||
const FormHttpErrorsWidget(this.exception, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
@@ -96,6 +96,11 @@ void main() async {
|
||||
FlutterError.dumpErrorToConsole(details);
|
||||
}
|
||||
|
||||
// Don't show the full error dialog for network image loading errors.
|
||||
if (details.exception is NetworkImageLoadException) {
|
||||
return;
|
||||
}
|
||||
|
||||
showGeneralErrorDialog(details.exception, stack);
|
||||
};
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class AuthScreen extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final deviceSize = MediaQuery.of(context).size;
|
||||
final deviceSize = MediaQuery.sizeOf(context);
|
||||
return Scaffold(
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
body: Stack(
|
||||
@@ -204,18 +204,13 @@ class _AuthCardState extends State<AuthCard> {
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
errorMessage = FormErrorsWidget(error);
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/errors.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
@@ -56,6 +58,8 @@ class ReorderableDaysList extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ReorderableDaysListState extends State<ReorderableDaysList> {
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
@@ -63,6 +67,7 @@ class _ReorderableDaysListState extends State<ReorderableDaysList> {
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
errorMessage,
|
||||
ReorderableListView.builder(
|
||||
buildDefaultDragHandles: false,
|
||||
shrinkWrap: true,
|
||||
@@ -120,7 +125,18 @@ class _ReorderableDaysListState extends State<ReorderableDaysList> {
|
||||
}
|
||||
});
|
||||
|
||||
provider.editDays(widget.days);
|
||||
try {
|
||||
provider.editDays(widget.days);
|
||||
setState(() {
|
||||
errorMessage = const SizedBox.shrink();
|
||||
});
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
Card(
|
||||
@@ -161,6 +177,8 @@ class DayFormWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _DayFormWidgetState extends State<DayFormWidget> {
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
final descriptionController = TextEditingController();
|
||||
final nameController = TextEditingController();
|
||||
late bool isRestDay;
|
||||
@@ -193,6 +211,7 @@ class _DayFormWidgetState extends State<DayFormWidget> {
|
||||
key: _form,
|
||||
child: Column(
|
||||
children: [
|
||||
errorMessage,
|
||||
Text(
|
||||
widget.day.isRest ? i18n.restDay : widget.day.name,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
@@ -292,25 +311,24 @@ class _DayFormWidgetState extends State<DayFormWidget> {
|
||||
try {
|
||||
await Provider.of<RoutinesProvider>(context, listen: false)
|
||||
.editDay(widget.day);
|
||||
} catch (error) {
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('An error occurred!'),
|
||||
content: const Text('Something went wrong.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: const Text('Okay'),
|
||||
onPressed: () {
|
||||
Navigator.of(ctx).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = const SizedBox.shrink();
|
||||
});
|
||||
}
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
isSaving = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setState(() => isSaving = false);
|
||||
},
|
||||
child:
|
||||
isSaving ? const FormProgressIndicator() : Text(AppLocalizations.of(context).save),
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/errors.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
@@ -20,6 +22,7 @@ class RoutineForm extends StatefulWidget {
|
||||
|
||||
class _RoutineFormState extends State<RoutineForm> {
|
||||
final _form = GlobalKey<FormState>();
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
bool isSaving = false;
|
||||
late bool fitInWeek;
|
||||
@@ -50,6 +53,7 @@ class _RoutineFormState extends State<RoutineForm> {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
|
||||
final children = [
|
||||
errorMessage,
|
||||
TextFormField(
|
||||
key: const Key('field-name'),
|
||||
decoration: InputDecoration(labelText: i18n.name),
|
||||
@@ -225,19 +229,22 @@ class _RoutineFormState extends State<RoutineForm> {
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${i18n.anErrorOccurred} $e')),
|
||||
);
|
||||
setState(() {
|
||||
errorMessage = const SizedBox.shrink();
|
||||
});
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => isSaving = false);
|
||||
setState(() {
|
||||
isSaving = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
isSaving = false;
|
||||
});
|
||||
},
|
||||
child: isSaving ? const FormProgressIndicator() : Text(AppLocalizations.of(context).save),
|
||||
),
|
||||
|
||||
@@ -18,7 +18,9 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/errors.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
@@ -89,6 +91,8 @@ class _SlotEntryFormState extends State<SlotEntryForm> {
|
||||
final maxRestController = TextEditingController();
|
||||
final rirController = TextEditingController();
|
||||
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
final _form = GlobalKey<FormState>();
|
||||
|
||||
var _edit = false;
|
||||
@@ -154,6 +158,7 @@ class _SlotEntryFormState extends State<SlotEntryForm> {
|
||||
key: _form,
|
||||
child: Column(
|
||||
children: [
|
||||
errorMessage,
|
||||
ListTile(
|
||||
title: Text(
|
||||
widget.entry.exerciseObj.getTranslation(languageCode).name,
|
||||
@@ -177,9 +182,18 @@ class _SlotEntryFormState extends State<SlotEntryForm> {
|
||||
? null
|
||||
: () async {
|
||||
setState(() => isDeleting = true);
|
||||
await provider.deleteSlotEntry(widget.entry.id!, widget.routineId);
|
||||
if (mounted) {
|
||||
setState(() => isDeleting = false);
|
||||
try {
|
||||
await provider.deleteSlotEntry(widget.entry.id!, widget.routineId);
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => isDeleting = false);
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -385,11 +399,14 @@ class _SlotEntryFormState extends State<SlotEntryForm> {
|
||||
await provider.editSlotEntry(widget.entry, widget.routineId);
|
||||
if (mounted) {
|
||||
setState(() => isSaving = false);
|
||||
errorMessage = const SizedBox.shrink();
|
||||
}
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('${i18n.anErrorOccurred} $e')),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => isSaving = false);
|
||||
@@ -419,6 +436,7 @@ class SlotDetailWidget extends StatefulWidget {
|
||||
|
||||
class _SlotDetailWidgetState extends State<SlotDetailWidget> {
|
||||
bool _showExerciseSearchBox = false;
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -427,6 +445,7 @@ class _SlotDetailWidgetState extends State<SlotDetailWidget> {
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
errorMessage,
|
||||
...widget.slot.entries.map((entry) => entry.hasProgressionRules
|
||||
? ProgressionRulesInfoBox(entry.exerciseObj)
|
||||
: SlotEntryForm(entry, widget.routineId, simpleMode: widget.simpleMode)),
|
||||
@@ -442,7 +461,18 @@ class _SlotDetailWidgetState extends State<SlotDetailWidget> {
|
||||
exercise: exercise,
|
||||
);
|
||||
|
||||
await provider.addSlotEntry(entry, widget.routineId);
|
||||
try {
|
||||
await provider.addSlotEntry(entry, widget.routineId);
|
||||
if (context.mounted) {
|
||||
setState(() => errorMessage = const SizedBox.shrink());
|
||||
}
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
if (widget.slot.entries.isNotEmpty)
|
||||
@@ -473,6 +503,7 @@ class _SlotFormWidgetStateNg extends State<ReorderableSlotList> {
|
||||
bool simpleMode = true;
|
||||
bool isAddingSlot = false;
|
||||
int? isDeletingSlot;
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -482,6 +513,7 @@ class _SlotFormWidgetStateNg extends State<ReorderableSlotList> {
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
errorMessage,
|
||||
if (!widget.day.isRest)
|
||||
SwitchListTile(
|
||||
value: simpleMode,
|
||||
@@ -579,7 +611,18 @@ class _SlotFormWidgetStateNg extends State<ReorderableSlotList> {
|
||||
widget.slots[i].order = i + 1;
|
||||
}
|
||||
|
||||
provider.editSlots(widget.slots, widget.day.routineId);
|
||||
try {
|
||||
provider.editSlots(widget.slots, widget.day.routineId);
|
||||
setState(() {
|
||||
errorMessage = const SizedBox.shrink();
|
||||
});
|
||||
} on WgerHttpException catch (error) {
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
errorMessage = FormHttpErrorsWidget(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
@@ -21,6 +21,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
@@ -74,7 +75,7 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Test the widgets on the workout form', (WidgetTester tester) async {
|
||||
testWidgets('Test the widgets on the routine form', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(renderWidget(existingRoutine));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
@@ -82,19 +83,19 @@ void main() {
|
||||
expect(find.byType(ElevatedButton), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Test editing an existing workout', (WidgetTester tester) async {
|
||||
testWidgets('Test editing an existing routine', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(renderWidget(existingRoutine));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('test 1'),
|
||||
findsOneWidget,
|
||||
reason: 'Name of existing workout plan',
|
||||
reason: 'Name of existing routine',
|
||||
);
|
||||
expect(
|
||||
find.text('description 1'),
|
||||
findsOneWidget,
|
||||
reason: 'Description of existing workout plan',
|
||||
reason: 'Description of existing routine',
|
||||
);
|
||||
await tester.enterText(find.byKey(const Key('field-name')), 'New description');
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
@@ -112,13 +113,28 @@ void main() {
|
||||
//expect(find.text(('New description')), findsOneWidget, reason: 'Workout plan detail page');
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new workout - only name', (WidgetTester tester) async {
|
||||
testWidgets('Test editing an existing routine - server error', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(mockRoutinesProvider.editRoutine(any)).thenThrow(WgerHttpException.fromMap({
|
||||
'name': ['The name is not valid'],
|
||||
}));
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(renderWidget(existingRoutine));
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
expect(find.text('The name is not valid'), findsOneWidget, reason: 'Error message is shown');
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new routine - only name', (WidgetTester tester) async {
|
||||
final editRoutine = Routine(
|
||||
id: 2,
|
||||
created: newRoutine.created,
|
||||
start: DateTime(2024, 11, 1),
|
||||
end: DateTime(2024, 12, 1),
|
||||
name: 'New cool workout',
|
||||
name: 'New cool routine',
|
||||
);
|
||||
|
||||
when(mockRoutinesProvider.addRoutine(any)).thenAnswer((_) => Future.value(editRoutine));
|
||||
@@ -127,7 +143,7 @@ void main() {
|
||||
await tester.pumpWidget(renderWidget(newRoutine));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(''), findsNWidgets(2), reason: 'New workout has no name or description');
|
||||
expect(find.text(''), findsNWidgets(2), reason: 'New routine has no name or description');
|
||||
await tester.enterText(find.byKey(const Key('field-name')), editRoutine.name);
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
|
||||
@@ -136,16 +152,16 @@ void main() {
|
||||
|
||||
// Detail page
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('New cool workout'), findsWidgets, reason: 'Workout plan detail page');
|
||||
expect(find.text('New cool routine'), findsWidgets, reason: 'routine detail page');
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new workout - name and description', (WidgetTester tester) async {
|
||||
testWidgets('Test creating a new routine - name and description', (WidgetTester tester) async {
|
||||
final editRoutine = Routine(
|
||||
id: 2,
|
||||
created: newRoutine.created,
|
||||
start: DateTime(2024, 11, 1),
|
||||
end: DateTime(2024, 12, 1),
|
||||
name: 'My workout',
|
||||
name: 'My routine',
|
||||
description: 'Get yuuuge',
|
||||
);
|
||||
when(mockRoutinesProvider.addRoutine(any)).thenAnswer((_) => Future.value(editRoutine));
|
||||
@@ -154,7 +170,7 @@ void main() {
|
||||
await tester.pumpWidget(renderWidget(newRoutine));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(''), findsNWidgets(2), reason: 'New workout has no name or description');
|
||||
expect(find.text(''), findsNWidgets(2), reason: 'New routine has no name or description');
|
||||
await tester.enterText(find.byKey(const Key('field-name')), editRoutine.name);
|
||||
await tester.enterText(find.byKey(const Key('field-description')), editRoutine.description);
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
@@ -164,6 +180,22 @@ void main() {
|
||||
|
||||
// Detail page
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('My workout'), findsWidgets, reason: 'Workout plan detail page');
|
||||
expect(find.text('My routine'), findsWidgets, reason: 'routine detail page');
|
||||
});
|
||||
|
||||
testWidgets('Test creating a new routine - server error', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
when(mockRoutinesProvider.addRoutine(any)).thenThrow(WgerHttpException.fromMap({
|
||||
'name': ['The name is not valid'],
|
||||
}));
|
||||
|
||||
// Act
|
||||
await tester.pumpWidget(renderWidget(newRoutine));
|
||||
await tester.enterText(find.byKey(const Key('field-name')), 'test 1234');
|
||||
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
expect(find.text('The name is not valid'), findsOneWidget, reason: 'Error message is shown');
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user