forked from ubuntu/app-center
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ubuntu#1723 from d-loose/error-view
feat: add custom error view
- Loading branch information
Showing
20 changed files
with
304 additions
and
20 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
Oops, something went wrong.