Skip to content

Commit

Permalink
test(app): also add fatal error tests when debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Jan 31, 2025
1 parent 6c88d96 commit a9e0e1e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/lib/common/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const cpicLookupUrl =
const drugInteractionIndicator = '*';
const drugInteractionIndicatorName = 'asterisk';

const testErrorMessage = 'THIS IS A TEST';
const nonFatalTestErrorMessage = 'THIS IS A NON-FATAL TEST';
const fatalTestErrorMessage = 'THIS IS A FATAL TEST';

// For shorter uniqueness check that also does not rely on variant; also format
// HLA-A (which is currently unique) as HLA-B
Expand Down
13 changes: 8 additions & 5 deletions app/lib/common/widgets/error_handler.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flutter/foundation.dart';

import '../module.dart';

class ErrorHandler extends StatefulWidget {
Expand Down Expand Up @@ -39,14 +41,15 @@ class ErrorHandlerState extends State<ErrorHandler> {
}
}

bool _isNonFatalTestError(Object exception) {
return exception.toString().contains(nonFatalTestErrorMessage);
}

bool _needToHandleError(Object exception) {
// Set to false to test that error screen appears (annoying when debugging
// with breakpoints anyways)
const ignoreTestError = true;
final isTestError = exception.toString().contains(testErrorMessage);
final ignoreTestError = kDebugMode && _isNonFatalTestError(exception);
final isOverflowError = exception is FlutterError &&
exception.message.startsWith('A RenderFlex overflowed');
final willIgnoreError = isOverflowError || (isTestError && ignoreTestError);
final willIgnoreError = isOverflowError || ignoreTestError;
return !willIgnoreError;
}

Expand Down
14 changes: 12 additions & 2 deletions app/lib/more/pages/more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,22 @@ class MorePage extends StatelessWidget {
if (kDebugMode) _buildSettingsItem(
title: 'Throw Flutter Error',
style: TextStyle(color: PharMeTheme.errorColor),
onTap: () => throw FlutterError(testErrorMessage),
onTap: () => throw FlutterError(nonFatalTestErrorMessage),
),
if (kDebugMode) _buildSettingsItem(
title: 'Throw Other Error',
style: TextStyle(color: PharMeTheme.errorColor),
onTap: () async => throw Exception(testErrorMessage),
onTap: () async => throw Exception(nonFatalTestErrorMessage),
),
if (kDebugMode) _buildSettingsItem(
title: 'Throw Flutter Error (Fatal)',
style: TextStyle(color: PharMeTheme.errorColor),
onTap: () => throw FlutterError(fatalTestErrorMessage),
),
if (kDebugMode) _buildSettingsItem(
title: 'Throw Other Error (Fatal)',
style: TextStyle(color: PharMeTheme.errorColor),
onTap: () async => throw Exception(fatalTestErrorMessage),
),
]
),
Expand Down

0 comments on commit a9e0e1e

Please sign in to comment.