60 lines
1.5 KiB
Dart
60 lines
1.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/errors/failure.dart';
|
|
import '../../core/network/api_handler.dart';
|
|
import 'app_side_panel.dart';
|
|
import 'app_toast.dart';
|
|
|
|
/// User-facing text from a [Failure] or other thrown error.
|
|
/// Prefer this over [Object.toString] for toasts (never show `Failure.server(...)`).
|
|
String errorDisplayMessage(
|
|
Object error, {
|
|
String fallback = 'Something went wrong. Please try again.',
|
|
}) {
|
|
if (error is Failure) {
|
|
return error is ValidationFailure
|
|
? validationErrorMessage(error)
|
|
: error.message;
|
|
}
|
|
final text = error.toString().trim();
|
|
return text.isEmpty ? fallback : text;
|
|
}
|
|
|
|
void showAccessDeniedSnackBar(BuildContext context, {String? message}) {
|
|
showAppToast(
|
|
context,
|
|
message ?? 'Access denied',
|
|
type: AppToastType.error,
|
|
);
|
|
}
|
|
|
|
void showSidePanelApiError(BuildContext context, Object error) {
|
|
showSidePanelSnackBar(context, errorDisplayMessage(error));
|
|
}
|
|
|
|
void showApiFailureSnackBar(BuildContext context, Failure failure) {
|
|
if (isForbiddenFailure(failure)) {
|
|
showAccessDeniedSnackBar(context, message: failure.message);
|
|
return;
|
|
}
|
|
|
|
if (isConflictFailure(failure)) {
|
|
showAppToast(context, failure.message, type: AppToastType.warning);
|
|
return;
|
|
}
|
|
|
|
showAppToast(
|
|
context,
|
|
errorDisplayMessage(failure),
|
|
type: AppToastType.error,
|
|
);
|
|
}
|
|
|
|
void showApiErrorToast(BuildContext context, Object error) {
|
|
showAppToast(
|
|
context,
|
|
errorDisplayMessage(error),
|
|
type: AppToastType.error,
|
|
);
|
|
}
|