2021-02-13 08:56:16 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2020-12-25 08:58:34 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 16:50:34 +00:00
|
|
|
|
2020-12-25 08:58:34 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2023-11-24 14:36:41 +00:00
|
|
|
import 'package:matrix/encryption.dart';
|
2021-10-26 16:50:34 +00:00
|
|
|
import 'package:matrix/matrix.dart';
|
2020-12-25 08:58:34 +00:00
|
|
|
|
2021-10-30 12:06:10 +00:00
|
|
|
import 'uia_request_manager.dart';
|
|
|
|
|
2020-12-25 08:58:34 +00:00
|
|
|
extension LocalizedExceptionExtension on Object {
|
2023-12-23 08:48:35 +00:00
|
|
|
String toLocalizedString(
|
|
|
|
BuildContext context, [
|
|
|
|
ExceptionContext? exceptionContext,
|
|
|
|
]) {
|
2020-12-25 08:58:34 +00:00
|
|
|
if (this is MatrixException) {
|
|
|
|
switch ((this as MatrixException).error) {
|
|
|
|
case MatrixError.M_FORBIDDEN:
|
2023-12-23 08:48:35 +00:00
|
|
|
if (exceptionContext == ExceptionContext.changePassword) {
|
|
|
|
return L10n.of(context)!.passwordIsWrong;
|
|
|
|
}
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.noPermission;
|
2020-12-25 08:58:34 +00:00
|
|
|
case MatrixError.M_LIMIT_EXCEEDED:
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.tooManyRequestsWarning;
|
2020-12-25 08:58:34 +00:00
|
|
|
default:
|
|
|
|
return (this as MatrixException).errorMessage;
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 14:36:41 +00:00
|
|
|
if (this is InvalidPassphraseException) {
|
|
|
|
return L10n.of(context)!.wrongRecoveryKey;
|
|
|
|
}
|
2023-02-13 14:38:08 +00:00
|
|
|
if (this is FileTooBigMatrixException) {
|
|
|
|
return L10n.of(context)!.fileIsTooBigForServer;
|
|
|
|
}
|
2021-02-13 08:56:16 +00:00
|
|
|
if (this is BadServerVersionsException) {
|
|
|
|
final serverVersions = (this as BadServerVersionsException)
|
|
|
|
.serverVersions
|
|
|
|
.toString()
|
|
|
|
.replaceAll('{', '"')
|
|
|
|
.replaceAll('}', '"');
|
|
|
|
final supportedVersions = (this as BadServerVersionsException)
|
|
|
|
.supportedVersions
|
|
|
|
.toString()
|
|
|
|
.replaceAll('{', '"')
|
|
|
|
.replaceAll('}', '"');
|
2023-01-26 08:47:30 +00:00
|
|
|
return L10n.of(context)!.badServerVersionsException(
|
|
|
|
serverVersions,
|
|
|
|
supportedVersions,
|
|
|
|
serverVersions,
|
|
|
|
supportedVersions,
|
|
|
|
);
|
2021-02-13 08:56:16 +00:00
|
|
|
}
|
|
|
|
if (this is BadServerLoginTypesException) {
|
|
|
|
final serverVersions = (this as BadServerLoginTypesException)
|
|
|
|
.serverLoginTypes
|
|
|
|
.toString()
|
|
|
|
.replaceAll('{', '"')
|
|
|
|
.replaceAll('}', '"');
|
|
|
|
final supportedVersions = (this as BadServerLoginTypesException)
|
|
|
|
.supportedLoginTypes
|
|
|
|
.toString()
|
|
|
|
.replaceAll('{', '"')
|
|
|
|
.replaceAll('}', '"');
|
2023-01-26 08:47:30 +00:00
|
|
|
return L10n.of(context)!.badServerLoginTypesException(
|
|
|
|
serverVersions,
|
|
|
|
supportedVersions,
|
|
|
|
supportedVersions,
|
|
|
|
);
|
2021-02-13 08:56:16 +00:00
|
|
|
}
|
2024-07-03 13:14:10 +00:00
|
|
|
if (this is IOException ||
|
2023-12-26 15:25:11 +00:00
|
|
|
this is SocketException ||
|
|
|
|
this is SyncConnectionException) {
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.noConnectionToTheServer;
|
2020-12-25 08:58:34 +00:00
|
|
|
}
|
2021-11-14 12:56:36 +00:00
|
|
|
if (this is String) return toString();
|
2021-10-30 12:06:10 +00:00
|
|
|
if (this is UiaException) return toString();
|
2021-02-06 19:41:24 +00:00
|
|
|
Logs().w('Something went wrong: ', this);
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.oopsSomethingWentWrong;
|
2020-12-25 08:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-23 08:48:35 +00:00
|
|
|
|
|
|
|
enum ExceptionContext {
|
|
|
|
changePassword,
|
|
|
|
}
|