Skip to content

Commit 0f44e0b

Browse files
committed
Refactor the general error popup
This should make it clearer where the messages and titles come from
1 parent 22924d1 commit 0f44e0b

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

lib/helpers/errors.dart

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,35 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
8787
return;
8888
}
8989

90+
final i18n = AppLocalizations.of(dialogContext);
91+
9092
// If possible, determine the error title and message based on the error type.
91-
bool isNetworkError = false;
92-
String errorTitle = 'An error occurred';
93-
String errorMessage = error.toString();
93+
// (Note that issue titles and error messages are not localized)
94+
bool allowReportIssue = true;
95+
String issueTitle = 'An error occurred';
96+
String issueErrorMessage = error.toString();
97+
String errorTitle = i18n.anErrorOccurred;
98+
String errorDescription = i18n.errorInfoDescription;
99+
var icon = Icons.error;
94100

95101
if (error is TimeoutException) {
96-
errorTitle = 'Network Timeout';
97-
errorMessage =
98-
'The connection to the server timed out. Please check your internet connection and try again.';
102+
issueTitle = 'Network Timeout';
103+
issueErrorMessage = 'The connection to the server timed out. Please check your '
104+
'internet connection and try again.';
99105
} else if (error is FlutterErrorDetails) {
100-
errorTitle = 'Application Error';
101-
errorMessage = error.exceptionAsString();
106+
issueTitle = 'Application Error';
107+
issueErrorMessage = error.exceptionAsString();
102108
} else if (error is MissingRequiredKeysException) {
103-
errorTitle = 'Missing Required Key';
109+
issueTitle = 'Missing Required Key';
104110
} else if (error is SocketException) {
105-
isNetworkError = true;
111+
allowReportIssue = false;
112+
icon = Icons.signal_wifi_connected_no_internet_4_outlined;
113+
errorTitle = i18n.errorCouldNotConnectToServer;
114+
errorDescription = i18n.errorCouldNotConnectToServerDetails;
106115
}
107-
/*
108-
else if (error is PlatformException) {
109-
errorTitle = 'Problem with media';
110-
errorMessage =
111-
'There was a problem loading the media. This can be a e.g. problem with the codec that'
112-
'is not supported by your device. Original error message: ${error.message}';
113-
}
114-
115-
*/
116116

117117
final String fullStackTrace = stackTrace?.toString() ?? 'No stack trace available.';
118118

119-
final i18n = AppLocalizations.of(dialogContext);
120-
121119
showDialog(
122120
context: dialogContext,
123121
barrierDismissible: false,
@@ -128,12 +126,12 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
128126
mainAxisAlignment: MainAxisAlignment.center,
129127
children: [
130128
Icon(
131-
isNetworkError ? Icons.signal_wifi_connected_no_internet_4_outlined : Icons.error,
129+
icon,
132130
color: Theme.of(context).colorScheme.error,
133131
),
134132
Expanded(
135133
child: Text(
136-
isNetworkError ? i18n.errorCouldNotConnectToServer : i18n.anErrorOccurred,
134+
errorTitle,
137135
style: TextStyle(color: Theme.of(context).colorScheme.error),
138136
),
139137
),
@@ -142,11 +140,7 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
142140
content: SingleChildScrollView(
143141
child: ListBody(
144142
children: [
145-
Text(
146-
isNetworkError
147-
? i18n.errorCouldNotConnectToServerDetails
148-
: i18n.errorInfoDescription,
149-
),
143+
Text(errorDescription),
150144
const SizedBox(height: 8),
151145
Text(i18n.errorInfoDescription2),
152146
const SizedBox(height: 10),
@@ -155,7 +149,7 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
155149
title: Text(i18n.errorViewDetails),
156150
children: [
157151
Text(
158-
errorMessage,
152+
issueErrorMessage,
159153
style: const TextStyle(fontWeight: FontWeight.bold),
160154
),
161155
Container(
@@ -178,14 +172,17 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
178172
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
179173
),
180174
onPressed: () {
181-
final String clipboardText =
182-
'Error Title: $errorTitle\nError Message: $errorMessage\n\nStack Trace:\n$fullStackTrace';
175+
final String clipboardText = 'Error Title: $issueTitle\n'
176+
'Error Message: $issueErrorMessage\n\n'
177+
'Stack Trace:\n$fullStackTrace';
183178
Clipboard.setData(ClipboardData(text: clipboardText)).then((_) {
184179
ScaffoldMessenger.of(context).showSnackBar(
185180
const SnackBar(content: Text('Error details copied to clipboard!')),
186181
);
187182
}).catchError((copyError) {
188-
if (kDebugMode) logger.fine('Error copying to clipboard: $copyError');
183+
if (kDebugMode) {
184+
logger.fine('Error copying to clipboard: $copyError');
185+
}
189186
ScaffoldMessenger.of(context).showSnackBar(
190187
const SnackBar(content: Text('Could not copy details.')),
191188
);
@@ -198,28 +195,30 @@ void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext
198195
),
199196
),
200197
actions: [
201-
if (!isNetworkError)
198+
if (allowReportIssue)
202199
TextButton(
203200
child: const Text('Report issue'),
204201
onPressed: () async {
205202
final description = Uri.encodeComponent(
206203
'## Description\n\n'
207204
'[Please describe what you were doing when the error occurred.]\n\n'
208205
'## Error details\n\n'
209-
'Error title: $errorTitle\n'
210-
'Error message: $errorMessage\n'
206+
'Error title: $issueTitle\n'
207+
'Error message: $issueErrorMessage\n'
211208
'Stack trace:\n'
212209
'```\n$stackTrace\n```',
213210
);
214211
final githubIssueUrl = '$GITHUB_ISSUES_BUG_URL'
215-
'&title=$errorTitle'
212+
'&title=$issueTitle'
216213
'&description=$description';
217214
final Uri reportUri = Uri.parse(githubIssueUrl);
218215

219216
try {
220217
await launchUrl(reportUri, mode: LaunchMode.externalApplication);
221218
} catch (e) {
222-
if (kDebugMode) logger.warning('Error launching URL: $e');
219+
if (kDebugMode) {
220+
logger.warning('Error launching URL: $e');
221+
}
223222
ScaffoldMessenger.of(context).showSnackBar(
224223
SnackBar(content: Text('Error opening issue tracker: $e')),
225224
);

pubspec.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -744,26 +744,26 @@ packages:
744744
dependency: transitive
745745
description:
746746
name: leak_tracker
747-
sha256: "8dcda04c3fc16c14f48a7bb586d4be1f0d1572731b6d81d51772ef47c02081e0"
747+
sha256: "6bb818ecbdffe216e81182c2f0714a2e62b593f4a4f13098713ff1685dfb6ab0"
748748
url: "https://pub.dev"
749749
source: hosted
750-
version: "11.0.1"
750+
version: "10.0.9"
751751
leak_tracker_flutter_testing:
752752
dependency: transitive
753753
description:
754754
name: leak_tracker_flutter_testing
755-
sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
755+
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
756756
url: "https://pub.dev"
757757
source: hosted
758-
version: "3.0.10"
758+
version: "3.0.9"
759759
leak_tracker_testing:
760760
dependency: transitive
761761
description:
762762
name: leak_tracker_testing
763-
sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
763+
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
764764
url: "https://pub.dev"
765765
source: hosted
766-
version: "3.0.2"
766+
version: "3.0.1"
767767
lints:
768768
dependency: transitive
769769
description:
@@ -1301,10 +1301,10 @@ packages:
13011301
dependency: transitive
13021302
description:
13031303
name: test_api
1304-
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
1304+
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
13051305
url: "https://pub.dev"
13061306
source: hosted
1307-
version: "0.7.6"
1307+
version: "0.7.4"
13081308
timing:
13091309
dependency: transitive
13101310
description:
@@ -1413,10 +1413,10 @@ packages:
14131413
dependency: transitive
14141414
description:
14151415
name: vector_math
1416-
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
1416+
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
14171417
url: "https://pub.dev"
14181418
source: hosted
1419-
version: "2.2.0"
1419+
version: "2.1.4"
14201420
version:
14211421
dependency: "direct main"
14221422
description:

0 commit comments

Comments
 (0)