mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 23:44:02 +01:00
shortens the displayed error message and adds link to doc
This commit is contained in:
committed by
Roland Geider
parent
1cde7f3c81
commit
7fb64b2925
@@ -16,15 +16,13 @@ import 'package:http/http.dart' as http;
|
||||
Future<SanityCheckResult> checkServerPaginationUrls({
|
||||
required String baseUrl,
|
||||
required String token,
|
||||
|
||||
/// Custom client is only used for tests
|
||||
http.Client? client,
|
||||
}) async {
|
||||
final httpClient = client ?? http.Client();
|
||||
|
||||
try {
|
||||
final baseUri = Uri.parse(baseUrl);
|
||||
final expectedHost = baseUri.host;
|
||||
final expectedPort = baseUri.port;
|
||||
final expectedScheme = baseUri.scheme;
|
||||
|
||||
final response = await httpClient.get(
|
||||
@@ -36,10 +34,7 @@ Future<SanityCheckResult> checkServerPaginationUrls({
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return const SanityCheckResult(
|
||||
isValid: false,
|
||||
message: 'Could not reach server endpoint',
|
||||
);
|
||||
return const SanityCheckResult(isValid: false);
|
||||
}
|
||||
|
||||
final data = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
@@ -51,35 +46,13 @@ Future<SanityCheckResult> checkServerPaginationUrls({
|
||||
|
||||
final nextUri = Uri.parse(nextUrl);
|
||||
|
||||
if (nextUri.host != expectedHost) {
|
||||
return SanityCheckResult(
|
||||
isValid: false,
|
||||
message:
|
||||
'Server misconfiguration detected!\n\n'
|
||||
'You are connecting to: $expectedScheme://$expectedHost${expectedPort != 0 ? ":$expectedPort" : ""}\n'
|
||||
'But pagination links point to: ${nextUri.scheme}://${nextUri.host}${nextUri.hasPort ? ":${nextUri.port}" : ""}\n\n'
|
||||
'This usually means your reverse proxy is not configured correctly.',
|
||||
detectedUrl: nextUrl,
|
||||
);
|
||||
}
|
||||
|
||||
if (nextUri.scheme != expectedScheme) {
|
||||
return SanityCheckResult(
|
||||
isValid: false,
|
||||
message:
|
||||
'Protocol mismatch detected!\n\n'
|
||||
'You are using: $expectedScheme\n'
|
||||
'But server returns: ${nextUri.scheme}',
|
||||
detectedUrl: nextUrl,
|
||||
);
|
||||
if (nextUri.host != expectedHost || nextUri.scheme != expectedScheme) {
|
||||
return const SanityCheckResult(isValid: false);
|
||||
}
|
||||
|
||||
return const SanityCheckResult(isValid: true);
|
||||
} catch (e) {
|
||||
return SanityCheckResult(
|
||||
isValid: false,
|
||||
message: 'Error checking server configuration: $e',
|
||||
);
|
||||
return const SanityCheckResult(isValid: false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,11 +60,7 @@ Future<SanityCheckResult> checkServerPaginationUrls({
|
||||
class SanityCheckResult {
|
||||
const SanityCheckResult({
|
||||
required this.isValid,
|
||||
this.message,
|
||||
this.detectedUrl,
|
||||
});
|
||||
|
||||
final bool isValid;
|
||||
final String? message;
|
||||
final String? detectedUrl;
|
||||
}
|
||||
|
||||
@@ -1241,14 +1241,18 @@
|
||||
},
|
||||
"serverConfigIssueTitle": "Server Configuration Issue",
|
||||
"@serverConfigIssueTitle": {
|
||||
"description": "Title of the dialog shown when server misconfiguration is detected"
|
||||
},
|
||||
"serverConfigIssueWarning": "The app may still work in some cases, but pagination and some features might be broken.",
|
||||
"@serverConfigIssueWarning": {
|
||||
"description": "Warning message explaining that the app might not work properly due to server misconfiguration"
|
||||
},
|
||||
"understand": "I understand",
|
||||
"@understand": {
|
||||
"description": "Button label to acknowledge and dismiss the server configuration warning"
|
||||
}
|
||||
"description": "Title of the dialog shown when server misconfiguration is detected"
|
||||
},
|
||||
"serverConfigIssueMessage": "Server misconfiguration detected, headers are not being passed correctly, please consult the documentation.",
|
||||
"@serverConfigIssueMessage": {
|
||||
"description": "Message explaining server misconfiguration and linking to documentation"
|
||||
},
|
||||
"understand": "I understand",
|
||||
"@understand": {
|
||||
"description": "Button label to acknowledge and dismiss the server configuration warning"
|
||||
},
|
||||
"viewDocumentation": "View documentation",
|
||||
"@viewDocumentation": {
|
||||
"description": "Button label to open the documentation about server configuration"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,8 +55,8 @@ class AuthProvider with ChangeNotifier {
|
||||
PackageInfo? applicationVersion;
|
||||
Map<String, String> metadata = {};
|
||||
AuthState state = AuthState.loggedOut;
|
||||
String? _serverConfigWarning;
|
||||
String? get serverConfigWarning => _serverConfigWarning;
|
||||
bool _serverConfigWarning = false;
|
||||
bool get serverConfigWarning => _serverConfigWarning;
|
||||
|
||||
static const MIN_APP_VERSION_URL = 'min-app-version';
|
||||
static const SERVER_VERSION_URL = 'version';
|
||||
@@ -72,7 +72,7 @@ class AuthProvider with ChangeNotifier {
|
||||
|
||||
/// Clear the server config warnings
|
||||
void clearServerConfigWarning() {
|
||||
_serverConfigWarning = null;
|
||||
_serverConfigWarning = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -255,8 +255,9 @@ class AuthProvider with ChangeNotifier {
|
||||
baseUrl: serverUrl,
|
||||
token: token!,
|
||||
);
|
||||
if (!sanityCheck.isValid && sanityCheck.message != null) {
|
||||
_serverConfigWarning = sanityCheck.message;
|
||||
|
||||
if (!sanityCheck.isValid) {
|
||||
_serverConfigWarning = true;
|
||||
notifyListeners();
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -219,9 +219,9 @@ class _AuthCardState extends State<AuthCard> {
|
||||
|
||||
if (context.mounted && res == LoginActions.proceed) {
|
||||
final authProvider = context.read<AuthProvider>();
|
||||
if (authProvider.serverConfigWarning != null) {
|
||||
if (authProvider.serverConfigWarning) {
|
||||
if (context.mounted) {
|
||||
showServerConfigWarning(context, authProvider.serverConfigWarning!);
|
||||
showServerConfigWarning(context);
|
||||
authProvider.clearServerConfigWarning();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import 'package:wger/helpers/misc.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
|
||||
/// Shows a warning dialog when server misconfiguration is detected
|
||||
void showServerConfigWarning(BuildContext context, String message) {
|
||||
void showServerConfigWarning(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
|
||||
showDialog(
|
||||
@@ -26,28 +26,22 @@ void showServerConfigWarning(BuildContext context, String message) {
|
||||
Expanded(child: Text(i18n.serverConfigIssueTitle)),
|
||||
],
|
||||
),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(message),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
i18n.serverConfigIssueWarning,
|
||||
style: const TextStyle(
|
||||
fontStyle: FontStyle.italic,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
content: Text(i18n.serverConfigIssueMessage),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(i18n.understand),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
launchURL(
|
||||
'https://wger.readthedocs.io/en/latest/administration/errors.html#wrong-pagination-links',
|
||||
context,
|
||||
);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: Text(i18n.viewDocumentation),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -47,8 +47,6 @@ void main() {
|
||||
);
|
||||
|
||||
expect(result.isValid, isTrue);
|
||||
expect(result.message, isNull);
|
||||
expect(result.detectedUrl, isNull);
|
||||
});
|
||||
|
||||
test('detects host mismatch (localhost issue)', () async {
|
||||
@@ -71,10 +69,6 @@ void main() {
|
||||
);
|
||||
|
||||
expect(result.isValid, isFalse);
|
||||
expect(result.message, contains('Server misconfiguration detected'));
|
||||
expect(result.message, contains('wger.example.com'));
|
||||
expect(result.message, contains('localhost'));
|
||||
expect(result.detectedUrl, 'http://localhost:8000/api/v2/exercise/?limit=1&offset=1');
|
||||
});
|
||||
|
||||
test('detects protocol mismatch (https vs http)', () async {
|
||||
@@ -97,9 +91,6 @@ void main() {
|
||||
);
|
||||
|
||||
expect(result.isValid, isFalse);
|
||||
expect(result.message, contains('Protocol mismatch detected'));
|
||||
expect(result.message, contains('https'));
|
||||
expect(result.message, contains('http'));
|
||||
});
|
||||
|
||||
test('returns valid when next URL is null (no pagination)', () async {
|
||||
@@ -136,7 +127,6 @@ void main() {
|
||||
);
|
||||
|
||||
expect(result.isValid, isFalse);
|
||||
expect(result.message, contains('Could not reach server endpoint'));
|
||||
});
|
||||
|
||||
test('handles network errors gracefully', () async {
|
||||
@@ -151,7 +141,6 @@ void main() {
|
||||
);
|
||||
|
||||
expect(result.isValid, isFalse);
|
||||
expect(result.message, contains('Error checking server configuration'));
|
||||
});
|
||||
|
||||
test('includes correct headers in request', () async {
|
||||
@@ -200,26 +189,4 @@ void main() {
|
||||
expect(result.isValid, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('SanityCheckResult', () {
|
||||
test('creates valid result correctly', () {
|
||||
const result = SanityCheckResult(isValid: true);
|
||||
|
||||
expect(result.isValid, isTrue);
|
||||
expect(result.message, isNull);
|
||||
expect(result.detectedUrl, isNull);
|
||||
});
|
||||
|
||||
test('creates invalid result with message', () {
|
||||
const result = SanityCheckResult(
|
||||
isValid: false,
|
||||
message: 'Test error',
|
||||
detectedUrl: 'http://localhost',
|
||||
);
|
||||
|
||||
expect(result.isValid, isFalse);
|
||||
expect(result.message, 'Test error');
|
||||
expect(result.detectedUrl, 'http://localhost');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user