nc-photos/app/lib/exception_util.dart

71 lines
2.7 KiB
Dart
Raw Permalink Normal View History

2021-04-10 06:28:12 +02:00
import 'dart:io';
2024-06-19 08:58:08 +02:00
import 'package:flutter/material.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2024-10-06 19:15:45 +02:00
import 'package:nc_photos/controller/files_controller.dart';
import 'package:nc_photos/entity/file_descriptor.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/navigation_manager.dart';
import 'package:nc_photos/widget/trusted_cert_manager.dart';
2021-04-10 06:28:12 +02:00
/// Convert an exception to a user-facing string
///
/// Typically used with SnackBar to show a proper error message
2022-08-05 19:52:27 +02:00
String toUserString(Object? exception) {
2021-04-10 06:28:12 +02:00
if (exception is ApiException) {
if (exception.response.statusCode == 401) {
return L10n.global().errorUnauthenticated;
2021-10-06 18:56:43 +02:00
} else if (exception.response.statusCode == 404) {
2022-07-21 07:45:49 +02:00
return "HTTP 404 not found";
2021-04-10 08:34:19 +02:00
} else if (exception.response.statusCode == 423) {
return L10n.global().errorLocked;
2021-05-28 06:37:15 +02:00
} else if (exception.response.statusCode == 500) {
return L10n.global().errorServerError;
2021-04-10 06:28:12 +02:00
}
} else if (exception is SocketException) {
return L10n.global().errorDisconnected;
2021-05-06 06:57:20 +02:00
} else if (exception is InvalidBaseUrlException) {
return L10n.global().errorInvalidBaseUrl;
} else if (exception is AlbumDowngradeException) {
return L10n.global().errorAlbumDowngrade;
2021-04-10 06:28:12 +02:00
}
2022-08-05 19:52:27 +02:00
return exception?.toString() ?? "Unknown error";
2021-04-10 06:28:12 +02:00
}
2024-06-19 08:58:08 +02:00
(String text, SnackBarAction? action) exceptionToSnackBarData(
Object? exception) {
if (exception is ApiException) {
if (exception.response.statusCode == 401) {
return (L10n.global().errorUnauthenticated, null);
} else if (exception.response.statusCode == 404) {
return ("HTTP 404 not found", null);
} else if (exception.response.statusCode == 423) {
return (L10n.global().errorLocked, null);
} else if (exception.response.statusCode == 500) {
return (L10n.global().errorServerError, null);
}
} else if (exception is SocketException) {
return (L10n.global().errorDisconnected, null);
} else if (exception is InvalidBaseUrlException) {
return (L10n.global().errorInvalidBaseUrl, null);
} else if (exception is AlbumDowngradeException) {
return (L10n.global().errorAlbumDowngrade, null);
} else if (exception is HandshakeException) {
return (
L10n.global().serverCertErrorDialogTitle,
SnackBarAction(
label: L10n.global().configButtonLabel,
onPressed: () => NavigationManager()
.getNavigator()
?.pushNamed(TrustedCertManager.routeName),
),
);
2024-10-06 19:15:45 +02:00
} else if (exception is UpdatePropertyFailureError) {
return (
"Failed to update files: ${exception.files.map((f) => f.filename).join(", ")}",
null
);
2024-06-19 08:58:08 +02:00
}
return (exception?.toString() ?? "Unknown error", null);
}