Merge branch 'krille/checkhomeserver' into 'main'
feat: Custom Exception and return type for Change Homeserver See merge request famedly/famedlysdk!649
This commit is contained in:
commit
7c44394f82
|
|
@ -299,7 +299,7 @@ class Client extends MatrixApi {
|
||||||
/// login types. Throws an exception if the server is not compatible with the
|
/// login types. Throws an exception if the server is not compatible with the
|
||||||
/// client and sets [homeserver] to [serverUrl] if it is. Supports the types [Uri]
|
/// client and sets [homeserver] to [serverUrl] if it is. Supports the types [Uri]
|
||||||
/// and [String].
|
/// and [String].
|
||||||
Future<void> checkHomeserver(dynamic homeserverUrl,
|
Future<WellKnownInformations> checkHomeserver(dynamic homeserverUrl,
|
||||||
{bool checkWellKnown = true}) async {
|
{bool checkWellKnown = true}) async {
|
||||||
try {
|
try {
|
||||||
if (homeserverUrl is Uri) {
|
if (homeserverUrl is Uri) {
|
||||||
|
|
@ -317,9 +317,10 @@ class Client extends MatrixApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look up well known
|
// Look up well known
|
||||||
|
WellKnownInformations wellKnown;
|
||||||
if (checkWellKnown) {
|
if (checkWellKnown) {
|
||||||
try {
|
try {
|
||||||
final wellKnown = await requestWellKnownInformations();
|
wellKnown = await requestWellKnownInformations();
|
||||||
homeserverUrl = wellKnown.mHomeserver.baseUrl.trim();
|
homeserverUrl = wellKnown.mHomeserver.baseUrl.trim();
|
||||||
// strip a trailing slash
|
// strip a trailing slash
|
||||||
if (homeserverUrl.endsWith('/')) {
|
if (homeserverUrl.endsWith('/')) {
|
||||||
|
|
@ -336,17 +337,17 @@ class Client extends MatrixApi {
|
||||||
final versions = await requestSupportedVersions();
|
final versions = await requestSupportedVersions();
|
||||||
if (!versions.versions
|
if (!versions.versions
|
||||||
.any((version) => supportedVersions.contains(version))) {
|
.any((version) => supportedVersions.contains(version))) {
|
||||||
throw Exception(
|
throw BadServerVersionsException(
|
||||||
'Server supports the versions: ${versions.versions.toString()} but this application is only compatible with ${supportedVersions.toString()}.');
|
versions.versions.toSet(), supportedVersions);
|
||||||
}
|
}
|
||||||
|
|
||||||
final loginTypes = await requestLoginTypes();
|
final loginTypes = await requestLoginTypes();
|
||||||
if (!loginTypes.flows.any((f) => supportedLoginTypes.contains(f.type))) {
|
if (!loginTypes.flows.any((f) => supportedLoginTypes.contains(f.type))) {
|
||||||
throw Exception(
|
throw BadServerLoginTypesException(
|
||||||
'Server supports the Login Types: ${loginTypes.flows.map((f) => f.toJson).toList().toString()} but this application is only compatible with ${supportedLoginTypes.toString()}.');
|
loginTypes.flows.map((f) => f.type).toSet(), supportedLoginTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return wellKnown;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
homeserver = null;
|
homeserver = null;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
|
@ -1988,3 +1989,21 @@ class SdkError {
|
||||||
|
|
||||||
SdkError({this.exception, this.stackTrace});
|
SdkError({this.exception, this.stackTrace});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class BadServerVersionsException implements Exception {
|
||||||
|
final Set<String> serverVersions, supportedVersions;
|
||||||
|
BadServerVersionsException(this.serverVersions, this.supportedVersions);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() =>
|
||||||
|
'Server supports the versions: ${serverVersions.toString()} but this application is only compatible with ${supportedVersions.toString()}.';
|
||||||
|
}
|
||||||
|
|
||||||
|
class BadServerLoginTypesException implements Exception {
|
||||||
|
final Set<String> serverLoginTypes, supportedLoginTypes;
|
||||||
|
BadServerLoginTypesException(this.serverLoginTypes, this.supportedLoginTypes);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() =>
|
||||||
|
'Server supports the Login Types: ${serverLoginTypes.toString()} but this application is only compatible with ${supportedLoginTypes.toString()}.';
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue