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: add custom error view #1723

Merged
merged 7 commits into from
Jul 11, 2024
Merged
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
1 change: 1 addition & 0 deletions packages/app_center/assets/error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion packages/app_center/lib/deb/deb_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:app_center/appstream/appstream.dart';
import 'package:app_center/constants.dart';
import 'package:app_center/deb/deb_model.dart';
import 'package:app_center/deb/deb_providers.dart';
import 'package:app_center/error/error.dart';
import 'package:app_center/l10n.dart';
import 'package:app_center/layout.dart';
import 'package:app_center/packagekit/packagekit.dart';
Expand Down Expand Up @@ -59,7 +60,10 @@ class _DebPageState extends ConsumerState<DebPage> {
debModel: debModel,
),
),
error: (error, stackTrace) => ErrorWidget(error),
error: (error, stackTrace) => ErrorView(
error: error,
onRetry: () => ref.invalidate(debModelProvider(widget.id)),
),
loading: () => const Center(child: YaruCircularProgressIndicator()),
);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/app_center/lib/deb/local_deb_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:app_center/constants.dart';
import 'package:app_center/deb/local_deb_model.dart';
import 'package:app_center/error/error.dart';
import 'package:app_center/l10n.dart';
import 'package:app_center/layout.dart';
import 'package:app_center/widgets/widgets.dart';
Expand All @@ -25,7 +26,10 @@ class LocalDebPage extends ConsumerWidget {
return model.when(
data: (debData) => _LocalDebPage(debData: debData),
loading: () => const Center(child: YaruCircularProgressIndicator()),
error: (error, stackTrace) => ErrorWidget(error),
error: (error, stackTrace) => ErrorView(
error: error,
onRetry: () => ref.invalidate(localDebModelProvider(path: path)),
),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/lib/error/error.dart
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export 'error_l10n.dart';
export 'error_observer.dart';
export 'error_view.dart';
82 changes: 82 additions & 0 deletions packages/app_center/lib/error/error_l10n.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:app_center/l10n.dart';
import 'package:snapd/snapd.dart';

enum ErrorAction {
retry,
checkStatus,
}

sealed class ErrorMessage {
const ErrorMessage();

factory ErrorMessage.fromObject(Object? e) {
if (e is! SnapdException) return ErrorMessageUnkown();

switch (e.kind) {
case 'network-timeout':
return ErrorMessageNetwork();
}
for (final patternMap in _patternMaps) {
final match = patternMap.pattern.firstMatch(e.message);
if (match != null) {
return patternMap.message(match);
}
}
return ErrorMessageUnkown();
}

static final _patternMaps =
<({RegExp pattern, ErrorMessage Function(Match) message})>[
(
pattern: RegExp('too many requests'),
message: (_) => ErrorMessageTooManyRequests(),
),
(
pattern: RegExp(
r'cannot refresh "(.*?)": snap "\1" has running apps \((.*?)\)',
),
message: (match) => ErrorMessageRunningApps(match.group(1)!),
),
(
pattern: RegExp('persistent network error'),
message: (_) => ErrorMessageNetwork(),
),
];

String body(AppLocalizations l10n) => switch (this) {
ErrorMessageNetwork() => l10n.errorViewNetworkErrorDescription,
ErrorMessageTooManyRequests() => l10n.errorViewServerErrorDescription,
ErrorMessageRunningApps(snap: final snap) =>
l10n.snapdExceptionRunningApps(snap),
_ => l10n.errorViewUnknownErrorDescription,
};

String title(AppLocalizations l10n) => switch (this) {
ErrorMessageNetwork() => l10n.errorViewNetworkErrorTitle,
_ => l10n.errorViewUnknownErrorTitle,
};

String actionLabel(AppLocalizations l10n) => switch (this) {
ErrorMessageNetwork() => l10n.errorViewNetworkErrorAction,
ErrorMessageTooManyRequests() => l10n.errorViewServerErrorAction,
_ => l10n.errorViewUnknownErrorAction,
};

List<ErrorAction> get actions => switch (this) {
ErrorMessageNetwork() => [ErrorAction.retry],
ErrorMessageTooManyRequests() => [ErrorAction.checkStatus],
ErrorMessageRunningApps() => [],
_ => [ErrorAction.retry, ErrorAction.checkStatus],
};
}

class ErrorMessageNetwork extends ErrorMessage {}

class ErrorMessageTooManyRequests extends ErrorMessage {}

class ErrorMessageRunningApps extends ErrorMessage {
const ErrorMessageRunningApps(this.snap);
final String snap;
}

class ErrorMessageUnkown extends ErrorMessage {}
Loading
Loading