/* * This file is part of wger Workout Manager . * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter_html/flutter_html.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:logging/logging.dart'; import 'package:material_ui/material_ui.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:wger/core/build_safety.dart'; import 'package:wger/core/errors.dart'; import 'package:wger/core/exceptions/http_exception.dart'; import 'package:wger/core/keys.dart'; import 'package:wger/core/logs.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/powersync/sync_diagnostics.dart' show collectSyncDiagnostics; /// Whether an error dialog is currently on screen. /// /// Errors can fire in quick succession; this guards against stacking several /// modal dialogs on top of each other. bool _errorDialogVisible = false; /// Shows [dialog] on the next frame, releasing [_errorDialogVisible] when it /// closes (or when it could not be shown at all). /// /// Error handlers can run during build, where pushing a route trips the /// navigator's !_debugLocked assertion; the secondary exception then wedged /// the guard flag shut, silently suppressing every later dialog. Deferring /// to a post-frame callback is legal no matter when the error fired. void _showErrorDialogNextFrame(BuildContext dialogContext, Future Function() dialog) { _errorDialogVisible = true; runAfterFrame(() { if (!dialogContext.mounted) { _errorDialogVisible = false; Logger('showErrorDialog').info('Dropping deferred error dialog: context unmounted'); return; } try { dialog().whenComplete(() => _errorDialogVisible = false); } catch (e) { _errorDialogVisible = false; rethrow; } }); } void showHttpExceptionErrorDialog(WgerHttpException exception, {BuildContext? context}) { final logger = Logger('showHttpExceptionErrorDialog'); // Attempt to get the BuildContext from our global navigatorKey. // This allows us to show a dialog even if the error occurs outside // of a widget's build method. final BuildContext? dialogContext = context ?? navigatorKey.currentContext; if (dialogContext == null) { if (kDebugMode) { logger.warning('Error: Could not error show http error dialog because the context is null.'); } return; } if (_errorDialogVisible) { logger.info('Suppressing error dialog, one is already visible: $exception'); return; } _showErrorDialogNextFrame( dialogContext, () => showDialog( context: dialogContext, builder: (ctx) => AlertDialog( title: Text(AppLocalizations.of(ctx).anErrorOccurred), content: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ if (exception.type == ErrorType.html) ServerHtmlError(data: exception.htmlError) else ...formatApiErrors(extractErrors(exception.errors)), ], ), ), actions: [ TextButton( child: Text(MaterialLocalizations.of(ctx).closeButtonLabel), onPressed: () { Navigator.of(ctx).pop(); }, ), ], ), ), ); } /// Flattens a [WgerHttpException]'s context and error map into a readable /// multi-line string for the diagnostic dialog and bug report. String _formatHttpExceptionDetail(WgerHttpException error) { String lines(Map m) => m.entries.map((e) => '${e.key}: ${e.value}').join('\n'); final ctx = error.context == null || error.context!.isEmpty ? '' : '${lines(error.context!)}\n\n'; return '$ctx${lines(error.errors)}'; } void showGeneralErrorDialog(dynamic error, StackTrace? stackTrace, {BuildContext? context}) { // Attempt to get the BuildContext from our global navigatorKey. // This allows us to show a dialog even if the error occurs outside // of a widget's build method. final BuildContext? dialogContext = context ?? navigatorKey.currentContext; final logger = Logger('showGeneralErrorDialog'); if (dialogContext == null) { if (kDebugMode) { logger.warning('Error: Could not error show dialog because the context is null.'); } return; } if (_errorDialogVisible) { logger.info('Suppressing error dialog, one is already visible: $error'); return; } final i18n = AppLocalizations.of(dialogContext); // If possible, determine the issue title and message based on the error type. // (Note that issue titles and error messages are not localized) String issueTitle = 'An error occurred'; String issueErrorMessage = error.toString(); if (error is FlutterErrorDetails) { issueTitle = 'Application Error'; issueErrorMessage = error.exceptionAsString(); } else if (error is MissingRequiredKeysException) { issueTitle = 'Missing Required Key'; } else if (error is WgerHttpException) { final status = error.statusCode == null ? '' : ' (HTTP ${error.statusCode})'; issueTitle = switch (error.source) { ExceptionSource.powersync => 'Sync upload rejected$status', ExceptionSource.flutter => 'Application error$status', ExceptionSource.api => 'Server error$status', }; issueErrorMessage = _formatHttpExceptionDetail(error); } final String fullStackTrace = stackTrace?.toString() ?? 'No stack trace available.'; final applicationLogs = InMemoryLogStore().getFormattedLogs(); _showErrorDialogNextFrame( dialogContext, () => showDialog( context: dialogContext, barrierDismissible: false, builder: (BuildContext context) { return AlertDialog( title: Row( spacing: 8, mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.error, color: Theme.of(context).colorScheme.error), Expanded( child: Text( i18n.anErrorOccurred, style: TextStyle(color: Theme.of(context).colorScheme.error), ), ), ], ), content: SingleChildScrollView( child: ListBody( children: [ Text(i18n.errorInfoDescription), const SizedBox(height: 8), Text(i18n.errorInfoDescription2), const SizedBox(height: 10), ExpansionTile( tilePadding: EdgeInsets.zero, title: Text(i18n.errorViewDetails), children: [ Text( issueErrorMessage, style: const TextStyle(fontWeight: FontWeight.bold), ), Container( alignment: Alignment.topLeft, padding: const EdgeInsets.symmetric(vertical: 8.0), constraints: const BoxConstraints(maxHeight: 250), child: SingleChildScrollView( child: Text( fullStackTrace, style: TextStyle( fontSize: 12.0, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ), ), CopyToClipboardButton( text: 'Error Title: $issueTitle\n' 'Error Message: $issueErrorMessage\n\n' 'Stack Trace:\n$fullStackTrace', ), const SizedBox(height: 8), Text(i18n.applicationLogs, style: const TextStyle(fontWeight: FontWeight.bold)), Container( alignment: Alignment.topLeft, padding: const EdgeInsets.symmetric(vertical: 8.0), constraints: const BoxConstraints(maxHeight: 250), child: SingleChildScrollView( child: Column( children: [ ...applicationLogs.map( (entry) => Text( entry, style: TextStyle( fontSize: 12.0, color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ), ], ), ), ), CopyToClipboardButton(text: applicationLogs.join('\n')), ], ), ], ), ), actions: [ TextButton( child: const Text('Report issue'), onPressed: () async { final githubIssueUrl = buildGithubIssueUrl( issueTitle: issueTitle, issueErrorMessage: issueErrorMessage, stackTrace: fullStackTrace, applicationLogs: applicationLogs, syncDiagnostics: await collectSyncDiagnostics(), appVersion: await collectAppVersion(), ); final Uri reportUri = Uri.parse(githubIssueUrl); try { await launchUrl(reportUri, mode: LaunchMode.externalApplication); } catch (e) { if (kDebugMode) { logger.warning('Error launching URL: $e'); } if (context.mounted) { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text('Error opening issue tracker: $e'))); } } }, ), FilledButton( child: Text(MaterialLocalizations.of(context).okButtonLabel), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ), ); } /// Routes [error] to the appropriate UI based on its [ErrorSeverity]. /// /// The caller is responsible for logging the error beforehand. void handleError(Object? error, StackTrace? stackTrace) { switch (classifyError(error)) { case ErrorSeverity.cosmetic: break; case ErrorSeverity.transient: showTransientErrorSnackbar(); case ErrorSeverity.fatal: // API errors are the form-validation fallback (clean field errors); // other sources are diagnostic and get the dialog with logs and report. if (error is WgerHttpException && error.source == ExceptionSource.api) { showHttpExceptionErrorDialog(error); } else { showGeneralErrorDialog(error, stackTrace); } } } /// Shows a brief, non-blocking snackbar telling the user about a (hopefully) /// transient error such as network problems, etc. void showTransientErrorSnackbar() { final messenger = scaffoldMessengerKey.currentState; final context = navigatorKey.currentContext; if (messenger == null || context == null) { if (kDebugMode) { Logger( 'showNetworkErrorSnackbar', ).warning('Could not show snackbar: no messenger or context available.'); } return; } // showSnackBar sets state on the messenger, which asserts during build. // Re-resolved in the callback: the captured messenger can be disposed // before the frame fires. final message = AppLocalizations.of(context).errorCouldNotConnectToServer; runAfterFrame(() { scaffoldMessengerKey.currentState ?..clearSnackBars() ..showSnackBar(SnackBar(content: Text(message))); }); } /// Shows a brief, non-blocking snackbar telling the user their session is no /// longer valid and they need to log in again. void showSessionExpiredSnackbar() { final messenger = scaffoldMessengerKey.currentState; final context = navigatorKey.currentContext; if (messenger == null || context == null) { return; } // Deferred like showTransientErrorSnackbar: auth teardown can coincide // with a build. final message = AppLocalizations.of(context).sessionExpired; runAfterFrame(() { scaffoldMessengerKey.currentState ?..clearSnackBars() ..showSnackBar(SnackBar(content: Text(message))); }); } /// A widget to render HTML errors returned by the server /// /// This is a simple wrapper around the `Html` Widget, with some light changes /// to the style. class ServerHtmlError extends StatelessWidget { final logger = Logger('ServerHtml'); final String data; ServerHtmlError({required this.data, super.key}); @override Widget build(BuildContext context) { final theme = Theme.of(context); return Html( data: data, style: { 'h1': Style(fontSize: FontSize(theme.textTheme.bodyLarge?.fontSize ?? 15)), 'h2': Style(fontSize: FontSize(theme.textTheme.bodyMedium?.fontSize ?? 15)), }, doNotRenderTheseTags: const {'a'}, ); } } class CopyToClipboardButton extends StatelessWidget { final logger = Logger('CopyToClipboardButton'); final String text; /// Renders as an icon button instead of a labelled one, for places where /// the button sits in a toolbar or is repeated in a list. final bool iconOnly; CopyToClipboardButton({required this.text, this.iconOnly = false, super.key}); void _copy(BuildContext context) { Clipboard.setData(ClipboardData(text: text)) .then((_) { if (context.mounted) { ScaffoldMessenger.of( context, ).showSnackBar(const SnackBar(content: Text('Details copied to clipboard!'))); } }) .catchError((copyError) { logger.warning('Error copying to clipboard: $copyError'); if (context.mounted) { ScaffoldMessenger.of( context, ).showSnackBar(const SnackBar(content: Text('Could not copy details.'))); } }); } @override Widget build(BuildContext context) { final i18n = AppLocalizations.of(context); if (iconOnly) { return IconButton( icon: const Icon(Icons.copy_all_outlined), tooltip: i18n.copyToClipboard, onPressed: () => _copy(context), ); } return TextButton.icon( icon: const Icon(Icons.copy_all_outlined, size: 18), label: Text(i18n.copyToClipboard), style: TextButton.styleFrom( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), onPressed: () => _copy(context), ); } } /// Processes the error messages from the server and returns a list of widgets List formatApiErrors(List errors, {Color? color}) { final logger = Logger('formatApiErrors'); final List errorList = []; for (final error in errors) { errorList.add( Text( error.key, style: TextStyle(fontWeight: FontWeight.bold, color: color), ), ); logger.warning(error.errorMessages); for (final message in error.errorMessages) { errorList.add(Text(message, style: TextStyle(color: color))); } errorList.add(const SizedBox(height: 8)); } return errorList; } /// Processes the error messages from the server and returns a list of widgets List formatTextErrors(List errors, {String? title, Color? color}) { final List errorList = []; // A null color inherits the surrounding text style (readable in dark mode). if (title != null) { errorList.add( Text( title, style: TextStyle(fontWeight: FontWeight.bold, color: color), ), ); } for (final message in errors) { errorList.add(Text(message, style: TextStyle(color: color))); } errorList.add(const SizedBox(height: 8)); return errorList; } class FormHttpErrorsWidget extends StatelessWidget { final WgerHttpException exception; const FormHttpErrorsWidget(this.exception, {super.key}); @override Widget build(BuildContext context) { final theme = Theme.of(context); // A JSON endpoint answering with HTML often means a proxy or bot wall such // as Anubis intercepted the request. Such pages render near-blank through // flutter_html since their visible content is JS-driven. In these cases we // try pull out the , which usually names the culprit final htmlTitle = exception.type == ErrorType.html ? htmlErrorTitle(exception.htmlError) : null; return Container( constraints: const BoxConstraints(maxHeight: 250), decoration: BoxDecoration( border: Border.all(color: theme.colorScheme.error, width: 1), borderRadius: BorderRadius.circular(6), ), padding: const EdgeInsets.all(10), child: SingleChildScrollView( child: Column( children: [ Icon(Icons.error_outline, color: theme.colorScheme.error), if (exception.type == ErrorType.html) ...[ if (htmlTitle != null) Text( htmlTitle, textAlign: TextAlign.center, style: TextStyle( fontWeight: FontWeight.bold, color: theme.colorScheme.error, ), ), ServerHtmlError(data: exception.htmlError), ] else ...formatApiErrors( extractErrors(exception.errors), color: theme.colorScheme.error, ), ], ), ), ); } } /// Extracts the `<title>` of an HTML error body (e.g. a proxy or bot-wall page /// such as Cloudflare or Anubis), with the few HTML entities that show up in /// such titles unescaped. Returns null when there is no usable title. String? htmlErrorTitle(String html) { final match = RegExp( r'<title[^>]*>(.*?)', caseSensitive: false, dotAll: true, ).firstMatch(html); final raw = match?.group(1)?.trim(); if (raw == null || raw.isEmpty) { return null; } return raw .replaceAll(''', "'") .replaceAll(''', "'") .replaceAll('"', '"') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('&', '&'); }