54 lines
1.7 KiB
Dart
54 lines
1.7 KiB
Dart
import '../../internal/application/Severity.dart';
|
|
|
|
/// The single error shape every failure is normalised into before it reaches
|
|
/// the user. SSH, build and archive failures are all mapped onto this by
|
|
/// `ParentViewModel.handleError`, so the error overlay only ever renders one
|
|
/// kind of object.
|
|
class PeachError {
|
|
final double code;
|
|
final String message;
|
|
final String helper;
|
|
final String title;
|
|
final String severity;
|
|
|
|
PeachError({
|
|
required this.code,
|
|
required this.message,
|
|
required this.helper,
|
|
required this.title,
|
|
required this.severity,
|
|
});
|
|
|
|
factory PeachError.fromJsonMap(Map<String, dynamic> json) => PeachError(
|
|
code: (json['code'] as num?)?.toDouble() ?? 0,
|
|
message: json['message'] as String? ?? '',
|
|
helper: json['helper'] as String? ?? '',
|
|
title: json['title'] as String? ?? 'Peach Error',
|
|
severity: json['severity'] as String? ?? Severity.message.name,
|
|
);
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'code': code,
|
|
'message': message,
|
|
'helper': helper,
|
|
'title': title,
|
|
'severity': severity,
|
|
};
|
|
}
|
|
|
|
/// Best-effort decode of an error payload into a [PeachError]. Anything that
|
|
/// isn't a recognisable map is surfaced as a generic error rather than thrown,
|
|
/// so the overlay always has something to show.
|
|
PeachError getPeachError(dynamic data) {
|
|
if (data is Map<String, dynamic>) return PeachError.fromJsonMap(data);
|
|
return PeachError(
|
|
code: 9000.01,
|
|
message: data?.toString() ?? 'An unknown error occurred.',
|
|
helper:
|
|
'Retry the operation, and if the problem persists check the '
|
|
'server and your connection details.',
|
|
title: 'Peach Error',
|
|
severity: Severity.message.name,
|
|
);
|
|
}
|