-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: match figma designs for error pages
- add `ErrorMessage` structure for localized error title, body and actions - add retry logic where possible - link to snapcraft status page for server errors
- Loading branch information
Showing
12 changed files
with
193 additions
and
56 deletions.
There are no files selected for viewing
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,37 +1,82 @@ | ||
import 'package:app_center/l10n.dart'; | ||
import 'package:snapd/snapd.dart'; | ||
|
||
typedef PatternMap = ({ | ||
RegExp pattern, | ||
String Function(AppLocalizations l10n, RegExpMatch match) message, | ||
}); | ||
|
||
final _patternMaps = <PatternMap>[ | ||
( | ||
pattern: RegExp('too many requests'), | ||
message: (l10n, _) => l10n.snapdExceptionTooManyRequests, | ||
), | ||
( | ||
pattern: | ||
RegExp(r'cannot refresh "(.*?)": snap "\1" has running apps \((.*?)\)'), | ||
message: (l10n, match) => l10n.snapdExceptionRunningApps( | ||
match.group(1).toString(), | ||
), | ||
), | ||
]; | ||
|
||
extension SnapdExceptionL10n on SnapdException { | ||
String prettyFormat(AppLocalizations l10n) { | ||
switch (kind) { | ||
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 l10n.snapdExceptionNetworkTimeout; | ||
return ErrorMessageNetwork(); | ||
} | ||
for (final patternMap in _patternMaps) { | ||
final match = patternMap.pattern.firstMatch(message); | ||
final match = patternMap.pattern.firstMatch(e.message); | ||
if (match != null) { | ||
return patternMap.message(l10n, match); | ||
return patternMap.message(match); | ||
} | ||
} | ||
return message; | ||
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 {} |
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
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
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,10 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
extension WidgetIterableExtension on Iterable<Widget> { | ||
List<Widget> separatedBy(Widget separator) { | ||
return expand((item) sync* { | ||
yield separator; | ||
yield item; | ||
}).skip(1).toList(); | ||
} | ||
} |
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
Oops, something went wrong.