mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 10:09:11 +01:00
Extract title from html error messages
This commit is contained in:
@@ -430,6 +430,12 @@ class FormHttpErrorsWidget extends StatelessWidget {
|
||||
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 <title>, which usually names the culprit
|
||||
final htmlTitle = exception.type == ErrorType.html ? htmlErrorTitle(exception.htmlError) : null;
|
||||
|
||||
return Container(
|
||||
constraints: const BoxConstraints(maxHeight: 250),
|
||||
decoration: BoxDecoration(
|
||||
@@ -441,9 +447,18 @@ class FormHttpErrorsWidget extends StatelessWidget {
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(Icons.error_outline, color: theme.colorScheme.error),
|
||||
if (exception.type == ErrorType.html)
|
||||
ServerHtmlError(data: exception.htmlError)
|
||||
else
|
||||
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,
|
||||
@@ -454,3 +469,25 @@ class FormHttpErrorsWidget extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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[^>]*>(.*?)</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('&', '&');
|
||||
}
|
||||
|
||||
@@ -43,4 +43,28 @@ void main() {
|
||||
expect(texts.every((t) => t.style?.color == Colors.red), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('htmlErrorTitle', () {
|
||||
test('extracts and unescapes the title of a bot-wall page', () {
|
||||
const html =
|
||||
'<!doctype html><html lang="en"><head>'
|
||||
'<title>Making sure you're not a bot!</title>'
|
||||
'</head><body></body></html>';
|
||||
|
||||
expect(htmlErrorTitle(html), "Making sure you're not a bot!");
|
||||
});
|
||||
|
||||
test('matches case-insensitively and ignores attributes on the title tag', () {
|
||||
expect(
|
||||
htmlErrorTitle('<HEAD><TITLE data-x="1">Just a moment...</TITLE></HEAD>'),
|
||||
'Just a moment...',
|
||||
);
|
||||
});
|
||||
|
||||
test('returns null when there is no usable title', () {
|
||||
expect(htmlErrorTitle('<html><body>no title here</body></html>'), isNull);
|
||||
expect(htmlErrorTitle('<html><head><title> </title></head></html>'), isNull);
|
||||
expect(htmlErrorTitle(''), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user