Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create custom error screen #297

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions starport_template/lib/entities/error_details.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:equatable/equatable.dart';

class CosmosErrorDetails extends Equatable {
const CosmosErrorDetails({
required this.error,
this.stackTrace,
});

factory CosmosErrorDetails.empty() => const CosmosErrorDetails(error: '');

final dynamic error;
final StackTrace? stackTrace;

@override
List<Object> get props => [
error,
stackTrace ?? '',
];

@override
String toString() => '''
Error[
error: $error,
stackTrace: $stackTrace
]''';
}
10 changes: 7 additions & 3 deletions starport_template/lib/pages/create_account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:cosmos_utils/cosmos_utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:starport_template/entities/error_details.dart';
import 'package:starport_template/pages/assets_portfolio_page.dart';
import 'package:starport_template/pages/back_up_account_page.dart';
import 'package:starport_template/pages/backup_later_bottom_sheet.dart';
Expand Down Expand Up @@ -44,6 +45,8 @@ class _CreateAccountPageState extends State<CreateAccountPage> {

bool get isAccountImportingError => StarportApp.accountsStore.isAccountImportingError;

CosmosErrorDetails get errorDetails => StarportApp.accountsStore.errorDetails;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -122,8 +125,8 @@ class _CreateAccountPageState extends State<CreateAccountPage> {
}

Widget _errorUI() {
return const Center(
child: Text('Error!'),
return ErrorWidget.withDetails(
error: FlutterError(errorDetails.toString()),
);
}

Expand Down Expand Up @@ -193,6 +196,7 @@ class _CreateAccountPageState extends State<CreateAccountPage> {
..add(DiagnosticsProperty<bool>('isLoading', isLoading))
..add(DiagnosticsProperty<bool>('isAuthenticating', isAuthenticating))
..add(DiagnosticsProperty<bool>('isAccountImportingError', isAccountImportingError))
..add(DiagnosticsProperty<bool>('isError', isError));
..add(DiagnosticsProperty<bool>('isError', isError))
..add(DiagnosticsProperty<CosmosErrorDetails>('errorDetails', errorDetails));
}
}
47 changes: 46 additions & 1 deletion starport_template/lib/starport_app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:alan/alan.dart';
import 'package:cosmos_auth/auth/cosmos_auth.dart';
import 'package:cosmos_ui_components/cosmos_theme.dart';
import 'package:cosmos_ui_components/cosmos_ui_components.dart';
import 'package:cosmos_utils/cosmos_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
Expand Down Expand Up @@ -34,6 +34,7 @@ class StarportApp extends StatelessWidget {
brightness: themeStore.isDarkTheme ? Brightness.dark : Brightness.light,
child: Builder(
builder: (context) {
setErrorBuilder(context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really sure why we're setting a variable as part of the build method, and why this variable is static? Looks like flutter team at google was a bit drunk when implemented this O.o. :D Wouldn't it make more sense to have specialized widget that is responsible of building the Error UI ? I think ErrorWidget is more meant for general build errors and altering it's builder field you're updating the way all build errors are being displayed across the whole app.

Copy link
Contributor Author

@Zfinix Zfinix May 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.flutter.dev/testing/errors, yes they were drunk, I figured it would be great if we had our own custom error screen app-wide, gets rid of the red screen of death and allows us to still copy error messages with one click of a button

return MaterialApp(
title: 'Starport template',
theme: CosmosTheme.of(context).buildFlutterTheme(),
Expand All @@ -44,4 +45,48 @@ class StarportApp extends StatelessWidget {
),
);
}

void setErrorBuilder(BuildContext context) {
ErrorWidget.builder = (errorDetails) {
return Material(
color: Colors.white,
child: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: ListView(
children: [
const Text(
'Error!',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Text(
errorDetails.toString(),
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400,
),
),
Row(
children: [
CosmosElevatedButton(
text: 'Take me back',
onTap: () => Navigator.pop(context),
textColor: Colors.black,
),
CosmosTextButton(
text: 'Back',
onTap: () => Navigator.of(context).pop(),
),
],
),
],
),
),
),
);
};
}
}
9 changes: 9 additions & 0 deletions starport_template/lib/stores/accounts_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mobx/mobx.dart';
import 'package:starport_template/app_config.dart';
import 'package:starport_template/entities/account_additional_data.dart';
import 'package:starport_template/entities/balance.dart';
import 'package:starport_template/entities/error_details.dart';
import 'package:starport_template/entities/import_account_form_data.dart';
import 'package:starport_template/utils/cosmos_balances.dart';
import 'package:starport_template/utils/token_sender.dart';
Expand Down Expand Up @@ -32,6 +33,7 @@ class AccountsStore {
final Observable<bool> _isBalancesLoadingError = Observable(false);
final Observable<bool> _isRenamingAccount = Observable(false);
final Observable<bool> _isSendingMoney = Observable(false);
final Observable<CosmosErrorDetails> _errorDetails = Observable(CosmosErrorDetails.empty());

final ObservableList<Balance> balancesList = ObservableList();
final Observable<CredentialsStorageFailure?> loadAccountsFailure = Observable(null);
Expand Down Expand Up @@ -78,6 +80,10 @@ class AccountsStore {

set isMnemonicCreatingError(bool val) => Action(() => _isMnemonicCreatingError.value = val)();

CosmosErrorDetails get errorDetails => _errorDetails.value;

set errorDetails(CosmosErrorDetails val) => Action(() => _errorDetails.value = val)();

bool get isMnemonicCreating => _isMnemonicCreating.value;

set isMnemonicCreating(bool val) => Action(() => _isMnemonicCreating.value = val)();
Expand Down Expand Up @@ -202,6 +208,7 @@ class AccountsStore {
return result.fold(
(fail) {
logError(fail);
errorDetails = CosmosErrorDetails(error: fail);
isAccountImportingError = true;
return null;
},
Expand Down Expand Up @@ -234,6 +241,7 @@ class AccountsStore {
await getBalances(selectedAccount.publicAddress);
} catch (ex, stack) {
logError(ex, stack);
errorDetails = CosmosErrorDetails(error: ex, stackTrace: stack);
isSendMoneyError = true;
}
isSendMoneyLoading = false;
Expand Down Expand Up @@ -271,6 +279,7 @@ class AccountsStore {
mnemonic = await generateMnemonic();
} catch (ex, stack) {
logError(ex, stack);
errorDetails = CosmosErrorDetails(error: ex, stackTrace: stack);
isMnemonicCreatingError = true;
}
isMnemonicCreating = false;
Expand Down